角半径和项目可绘制代码

时间:2013-04-22 08:43:10

标签: android drawable

我有这个xml;

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:drawable="@drawable/back" />
    <item>
        <shape>
            <solid/>
            <stroke android:width="1dip" android:color="#25786" />
            <corners android:radius="10dip"/>
            <padding android:left="0dip" android:top="0dip" 
                     android:right="0dip" android:bottom="0dip" />
        </shape>
    </item> 
</layer-list> 

现在我通过代码来做这件事:

Drawable[] layers = new Drawable[2];

    ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
    sd1.getPaint().setColor(0xFFFFFFFF);
    sd1.getPaint().setStyle(Style.STROKE);
    sd1.getPaint().setStrokeWidth(1);

    layers[0] = sd1;

    LayerDrawable composite = new LayerDrawable(layers);

但我无法为<corners android:radius="10dip"/><item android:drawable="@drawable/back" />

编写代码

怎么做?

编辑1

Drawable[] layers = new Drawable[2];

ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
sd1.getPaint().setColor(0xFFFFFFFF);
sd1.getPaint().setStyle(Style.STROKE);
sd1.getPaint().setStrokeWidth(1);
sd1.getPaint().setPathEffect(new CornerPathEffect(10));

layers[1] = sd1;
layers[0] = getResources().getDrawable(R.drawable.pie_chart_back);

LayerDrawable composite = new LayerDrawable(layers);

2 个答案:

答案 0 :(得分:5)

就像你的XML一样,你需要三个Drawable,而不是一个。一个是包含其他人的LayerDrawable。另一个缺失的可能是BitmapDrawable,具体取决于@drawable/back

您正在寻找的角落效果位于Drawable Paint的{​​{3}},请参阅CornerPathEffect

答案 1 :(得分:0)

试试这个......!

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#e1e1e1" />

    <stroke
        android:width="2dp"
        android:color="#808080" />

    <corners android:radius="10dp" />

    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />

</shape>