按钮的自定义背景形状通过代码

时间:2015-12-12 03:24:16

标签: android android-custom-view android-custom-drawable

我已设法通过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代码是什么等同于什么?请问任何指南?

2 个答案:

答案 0 :(得分:1)

在Java中使用它会更加冗长,但这是您需要做的事情。

  • 创建new StateListDrawable()
  • 对于每个州:
    • 创建new ShapeDrawable(new RoundRectShape(...))。我不记得构造函数arg是如何工作的,但你可以进行实验。
    • 使用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);

答案 1 :(得分:0)

你可以像这样处理按钮状态:

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;

}

请参阅此示例以供参考:

{{1}}

检查一次Defining Drawable Shape with in JAVA code