动态创建一个Shape

Ali*_*jad 7 android android-drawable

我有一个用XML定义的形状对象,如下所示:

<shape android:shape="rectangle">
    <gradient
        android:startColor="#333"
        android:centerColor="#DDD"
        android:endColor="#333"/>
    <stroke android:width="1dp" android:color="#FF333333" />
</shape>
Run Code Online (Sandbox Code Playgroud)

我想在我的代码中创建一个相等的对象.我创建了GradientDrawable如下:

gradientDrawable1.setColors(new int[] { 0x333, 0xDDD, 0x333 });
gradientDrawable1.setOrientation(Orientation.TOP_BOTTOM);
Run Code Online (Sandbox Code Playgroud)

但我不知道如何创建一个行程(?),然后分配笔触及GradientDrawableShape

任何的想法?

S.D*_*.D. 7

例:

import android.graphics.drawable.GradientDrawable;

public class SomeDrawable extends GradientDrawable {

    public SomeDrawable(int pStartColor, int pCenterColor, int pEndColor, int pStrokeWidth, int pStrokeColor, float cornerRadius) {
        super(Orientation.BOTTOM_TOP,new int[]{pStartColor,pCenterColor,pEndColor});
        setStroke(pStrokeWidth,pStrokeColor);
        setShape(GradientDrawable.RECTANGLE);
        setCornerRadius(cornerRadius);
    }

}
Run Code Online (Sandbox Code Playgroud)

用法:

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SomeDrawable vDrawable = new SomeDrawable(Color.BLACK,Color.GREEN,Color.LTGRAY,2,Color.RED,50);
        View vView = new View(this);
        vView.setBackgroundDrawable(vDrawable);
        setContentView(vView);
    }


}
Run Code Online (Sandbox Code Playgroud)

结果:

可绘制的结果图像