通过代码自定义按钮的背景形状
Custom Background Shape of Button via Code
我已经通过 XML 代码
创建了自定义按钮背景形状
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="24dp" />
<stroke android:width="2dp" android:color="@color/colorWhite" />
<solid android:color="@color/colorPrimary" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="24dp" />
<stroke android:width="2dp" android:color="@color/colorWhite" />
<solid android:color="@color/colorPrimary" />
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="24dp" />
<stroke android:width="2dp" android:color="@color/colorWhite" />
<solid android:color="@color/colorWhite" />
</shape>
</item>
</selector>
但我想知道与此等效的 Java 代码是什么?请问有什么攻略吗?
在 Java 中执行此操作要冗长得多,但以下是您需要执行的操作。
- 创建
new StateListDrawable()
- 对于每个州:
- 创建
new ShapeDrawable(new RoundRectShape(...))
。我不记得构造函数参数是如何工作的,但你可以试验一下。
- 使用
shapeDrawable.getPaint()
获取其Paint对象并进行修改。您可能会使用 setColor()
、setStyle()
和 setStrokeWidth()
.
- 构建状态集。这是一个由各种 android 状态属性组成的整数数组,例如
android.R.attr.state_pressed
,用于您想要的状态。
- 呼叫
stateListDrawable.addState(stateSet, shapeDrawable)
。您可以使用 StateSet.NOTHING
(或空 int[])作为默认状态。确保按照它们在 XML. 中出现的顺序添加它们
像这样:
StateListDrawable stateListDrawable = new StateListDrawable();
Shape roundRect = new RoundRectShape(...);
// Add states in order. I'll just demonstrate one.
ShapeDrawable pressed = new ShapeDrawable(roundRect);
Paint paint = pressed.getPaint();
paint.setColor(Color.BLUE);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(10f); // this is in pixels, you'll have to convert to dp yourself
int[] pressedState = { android.R.attr.state_pressed };
stateListDrawable.addState(pressedState, pressed);
您可以像这样处理按钮状态:
StateListDrawable states = new StateListDrawable();
states.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(R.drawable.img_pressed));
states.addState(new int[] { android.R.attr.state_focused }, getResources().getDrawable(R.drawable.img_focused));
states.addState(new int[] {}, getResources().getDrawable(R.drawable.img_normal));
button.setBackgroundDrawable(states);
参见此示例以供参考:
StateListDrawable states = new StateListDrawable();
states.addState(new int[] { android.R.attr.state_pressed }, getSelectedBackground());
states.addState(new int[] { android.R.attr.state_focused }, getSelectedBackground());
states.addState(new int[] {}, getNormalBackground());
button.setBackgroundDrawable(states);
private ShapeDrawable getNormalBackground() {
int r = 10;
float[] outerR = new float[] { r, r, r, r, r, r, r, r };
RoundRectShape rr = new RoundRectShape(outerR, null, null);
ShapeDrawable drawable = new ShapeDrawable(rr);
drawable.getPaint().setColor(Color.GRAY);
return drawable;
}
private ShapeDrawable getSelectedBackground() {
float[] outerR2 = new float[] { 10, 10, 10, 10, 10, 10, 10, 10 };
RectF inset2 = new RectF(3, 3, 3, 3);
float[] innerR2 = new float[] { 9, 9, 0, 0, 0, 5, 0, 0 };
ShapeDrawable sh2 = new ShapeDrawable(new RoundRectShape(outerR2, inset2, innerR2));
return sh2;
}
检查一次Defining Drawable Shape with in JAVA code
我已经通过 XML 代码
创建了自定义按钮背景形状<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape android:shape="rectangle" >
<corners android:radius="24dp" />
<stroke android:width="2dp" android:color="@color/colorWhite" />
<solid android:color="@color/colorPrimary" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="24dp" />
<stroke android:width="2dp" android:color="@color/colorWhite" />
<solid android:color="@color/colorPrimary" />
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="24dp" />
<stroke android:width="2dp" android:color="@color/colorWhite" />
<solid android:color="@color/colorWhite" />
</shape>
</item>
</selector>
但我想知道与此等效的 Java 代码是什么?请问有什么攻略吗?
在 Java 中执行此操作要冗长得多,但以下是您需要执行的操作。
- 创建
new StateListDrawable()
- 对于每个州:
- 创建
new ShapeDrawable(new RoundRectShape(...))
。我不记得构造函数参数是如何工作的,但你可以试验一下。 - 使用
shapeDrawable.getPaint()
获取其Paint对象并进行修改。您可能会使用setColor()
、setStyle()
和setStrokeWidth()
. - 构建状态集。这是一个由各种 android 状态属性组成的整数数组,例如
android.R.attr.state_pressed
,用于您想要的状态。 - 呼叫
stateListDrawable.addState(stateSet, shapeDrawable)
。您可以使用StateSet.NOTHING
(或空 int[])作为默认状态。确保按照它们在 XML. 中出现的顺序添加它们
- 创建
像这样:
StateListDrawable stateListDrawable = new StateListDrawable();
Shape roundRect = new RoundRectShape(...);
// Add states in order. I'll just demonstrate one.
ShapeDrawable pressed = new ShapeDrawable(roundRect);
Paint paint = pressed.getPaint();
paint.setColor(Color.BLUE);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(10f); // this is in pixels, you'll have to convert to dp yourself
int[] pressedState = { android.R.attr.state_pressed };
stateListDrawable.addState(pressedState, pressed);
您可以像这样处理按钮状态:
StateListDrawable states = new StateListDrawable();
states.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(R.drawable.img_pressed));
states.addState(new int[] { android.R.attr.state_focused }, getResources().getDrawable(R.drawable.img_focused));
states.addState(new int[] {}, getResources().getDrawable(R.drawable.img_normal));
button.setBackgroundDrawable(states);
参见此示例以供参考:
StateListDrawable states = new StateListDrawable();
states.addState(new int[] { android.R.attr.state_pressed }, getSelectedBackground());
states.addState(new int[] { android.R.attr.state_focused }, getSelectedBackground());
states.addState(new int[] {}, getNormalBackground());
button.setBackgroundDrawable(states);
private ShapeDrawable getNormalBackground() {
int r = 10;
float[] outerR = new float[] { r, r, r, r, r, r, r, r };
RoundRectShape rr = new RoundRectShape(outerR, null, null);
ShapeDrawable drawable = new ShapeDrawable(rr);
drawable.getPaint().setColor(Color.GRAY);
return drawable;
}
private ShapeDrawable getSelectedBackground() {
float[] outerR2 = new float[] { 10, 10, 10, 10, 10, 10, 10, 10 };
RectF inset2 = new RectF(3, 3, 3, 3);
float[] innerR2 = new float[] { 9, 9, 0, 0, 0, 5, 0, 0 };
ShapeDrawable sh2 = new ShapeDrawable(new RoundRectShape(outerR2, inset2, innerR2));
return sh2;
}
检查一次Defining Drawable Shape with in JAVA code