【问题标题】:How to draw a smaller ShapeDrawable inside another shapeDrawable programmatically如何以编程方式在另一个 shapeDrawable 内绘制较小的 ShapeDrawable
【发布时间】:2012-12-21 14:40:35
【问题描述】:

我试图在另一个圆圈内画一个较小的圆圈。 这看起来很简单,但我遇到了麻烦,找不到答案。 我使用的代码是:

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

但这不起作用,发生的事情是较小的圆圈与较大的圆圈一样大。所以唯一显示的是更大圆圈大小的黑色圆圈。 如果有人可以提供帮助,我会很高兴。提前致谢。

【问题讨论】:

    标签: android dynamic drawing drawable shapes


    【解决方案1】:

    改变顺序,

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

    【讨论】:

    • 感谢您的回答,但我试过了,它仍然发生同样的事情。
    • 对于阅读本文的其他人,请注意小圆圈实际上是大圆圈。我将 setPadding 用于较小的圆圈,应该反过来。非常感谢您的回答。
    • @Alan 你能否更新代码并解释为什么 smallCircle 实际上是大的?我的意思是,它有填充,所以它不应该更小吗?另外,为什么在 ImageView 上使用 setImageDrawable 时,它​​只显示一种颜色(蓝色)?
    猜你喜欢
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    相关资源
    最近更新 更多