Goofy
Goofy

Reputation: 6128

Corner Radius and item drawable in code

I have this 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> 

Now i am doing it via code:

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);

But i am not able to code it for <corners android:radius="10dip"/> and <item android:drawable="@drawable/back" />

How to do it?

EDIT 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);

Upvotes: 4

Views: 5163

Answers (2)

jigar
jigar

Reputation: 1591

try this...!

<?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>

Upvotes: 0

class stacker
class stacker

Reputation: 5347

Just like in your XML, you'll need three Drawables, not one. One is a LayerDrawable containing the others. The other one missing could be a BitmapDrawable depending on @drawable/back.

The corner effect you're looking for is in the CornerPathEffect of your Drawable's Paint, see Paint.setPathEffect.

Upvotes: 5

Related Questions