6

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.

Alan
  • 1,509
  • 1
  • 16
  • 21

1 Answers1

18

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

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Talha
  • 12,673
  • 5
  • 49
  • 68
  • Thanks for the answer, but i tryed and it still happens the same thing. – Alan Dec 21 '12 at 14:56
  • For the other people that will read this just pay attetion that the smaller circle is actualy the bigger circle. I was using setPadding to the smaler circle and should be the other way around. Thanks a lot for the answer. – Alan Dec 21 '12 at 15:20
  • @Alan Can you please update the code and add explanation of why smallerCircle is actually the large one? I mean, it has padding, so shouldn't it be smaller? Also, why when using setImageDrawable on ImageView, it shows just a single color (blue)? – android developer Aug 10 '17 at 07:29