Android开发:LayerDrawable setBounds无效解决方法

设计效果是这样的:
circle
两个圆,底下是蓝色,上面的是白色,用LayerDrawable做,两个ShapeDrawable最合适不过,因为要动态改白圆的大小,所以直接写在xml里就不太合适,用Java代码写.
但是发现在白色的ShapeDrawable上setBounds没有缩进的效果,直接把下面的蓝圆盖掉了。其实这里应该用LayerDrawable的setLayerInset方法:

        OvalShape shape1 = new OvalShape();
        ShapeDrawable drawable1 = new ShapeDrawable(shape1);
        drawable1.getPaint().setColor(colorBlue);
        Drawable[] drawables = new Drawable[2];
        drawables[0] = drawable1;
        OvalShape shape2 = new OvalShape();
        ShapeDrawable drawable2 = new ShapeDrawable(shape2);
        //下面一行无效
//        drawable2.setBounds(10,10,10,10);
        drawable2.getPaint().setColor(Color.WHITE);
        drawables[1] = drawable2;
        LayerDrawable layerDrawable = new LayerDrawable(drawables);
        //需要使用setLayerInset,第一个参数是drawable索引,后面是左上右下
        layerDrawable.setLayerInset(1, 10,10,10,10);
        ViewHelper.setBackgroundForView(viewCircle,layerDrawable);

© 2016, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记