如何在程序中绘制一个较小的ShapeDrawable,放置在另一个ShapeDrawable内部

6

我想在一个圆内画一个较小的圆。这似乎很简单,但我遇到了麻烦,找不到答案。我正在使用的代码是:

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

但是那样并不起作用,小圆圈会变得和大圆圈一样大。所以唯一显示的是一个大小为biggerCircle的黑色圆圈。如果有人能帮忙,我将不胜感激。先谢谢了。
1个回答

18

更改顺序,

Drawable[] d = {smallerCircle,biggerCircle};

LayerDrawable composite1 = new LayerDrawable(d);

试试这样做

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

在这里输入图片描述


谢谢你的回答,但我尝试了,仍然发生同样的事情。 - Alan
对于其他人看到这个问题时,请注意小圆实际上是大圆。我之前对小圆使用了setPadding,但应该反过来。非常感谢您的回答。 - Alan
@Alan,你能否更新代码并解释一下为什么smallerCircle实际上是大圆吗?我的意思是,它有填充,所以不应该更小吗?另外,为什么在ImageView上使用setImageDrawable时,它只显示单一颜色(蓝色)? - android developer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接