Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a smaller ShapeDrawable inside another shapeDrawable programmatically

Im trying to draw a smaller circle within another circle. It seems pretty simple but Im having trouble with this and couldnt find an answer. The code im using is:

    ShapeDrawable biggerCircle= new ShapeDrawable( new OvalShape());
    biggerCircle.setIntrinsicHeight( 60 );
    biggerCircle.setIntrinsicWidth( 60);
    biggerCircle.setBounds(new Rect(0, 0, 60, 60));
    biggerCircle.getPaint().setColor(Color.BLUE);

    ShapeDrawable smallerCircle= new ShapeDrawable( new OvalShape());
    smallerCircle.setIntrinsicHeight( 10 );
    smallerCircle.setIntrinsicWidth( 10);
    smallerCircle.setBounds(new Rect(0, 0, 10, 10));
    smallerCircle.getPaint().setColor(Color.BLACK);
    smallerCircle.setPadding(50,50,50,50);

    LayerDrawable composite1 = new LayerDrawable(new Drawable[] biggerCircle,smallerCircle,});

But that didnt work, what happens is that the smaller circle get as big as the the bigger circle. So the only thing showing is as black circle with the size of the biggerCircle. I would apriciate if someone could help. Thanks in advance.

like image 412
Alan Avatar asked Dec 21 '12 14:12

Alan


1 Answers

Change the order,

Drawable[] d = {smallerCircle,biggerCircle};

LayerDrawable composite1 = new LayerDrawable(d);

try like this

        ShapeDrawable biggerCircle= new ShapeDrawable( new OvalShape());
        biggerCircle.setIntrinsicHeight( 60 );
        biggerCircle.setIntrinsicWidth( 60);
        biggerCircle.setBounds(new Rect(0, 0, 60, 60));
        biggerCircle.getPaint().setColor(Color.BLUE);

        ShapeDrawable smallerCircle= new ShapeDrawable( new OvalShape());
        smallerCircle.setIntrinsicHeight( 10 );
        smallerCircle.setIntrinsicWidth( 10);
        smallerCircle.setBounds(new Rect(0, 0, 10, 10));
        smallerCircle.getPaint().setColor(Color.BLACK);
        smallerCircle.setPadding(50,50,50,50);
        Drawable[] d = {smallerCircle,biggerCircle};

        LayerDrawable composite1 = new LayerDrawable(d);

        btn.setBackgroundDrawable(composite1);  

enter image description here

like image 51
Talha Avatar answered Sep 28 '22 09:09

Talha