如何在按下按鈕時更改矩形按鈕的描邊寬度?如何以編程方式更改形狀矩形的描邊寬度
我在我的自定義按鈕的可繪製文件夾中創建了一個xml文件。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="9dp"/>
<gradient
android:angle="45"
android:centerX="9%"
android:centerColor="#84edb2"
android:startColor="#07e7cb"
android:endColor="#b0ede3"
android:type="linear"
/>
<size
android:width="40dp"
android:height="30dp"
/>
<stroke
android:width="2dp" // I want to change this programmatically.
android:color="#2f7E87"
/>
</shape>
以下是我的java代碼。 我想在按下按鈕時更改按鈕的筆觸寬度。
public static void buttonEffect(View button){
button.setOnTouchListener(new View.OnTouchListener() {
ShapeDrawable circle = new ShapeDrawable(new OvalShape());
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
Button view = (Button) v;
int col = Color.parseColor("#40E0D0");
view.getBackground().setColorFilter(col, PorterDuff.Mode.SCREEN);
v.invalidate();
break;
}
case MotionEvent.ACTION_UP:
// Your action here on button click
case MotionEvent.ACTION_CANCEL: {
Button view = (Button) v;
view.getBackground().clearColorFilter();
view.invalidate();
break;
}
}
return true;
}
});
}
我是新到Android,你能告訴我怎麼做,我不知道 –