Android:在叠加图像背景中设置填充

我正在尝试为我的Android应用程序创建一个动态图标菜单.
该图标由2个可绘制的9补丁背景图像组成,图标图像覆盖在其上.

参考overlay two images in android to set an imageview,我有以下代码很好地覆盖了.

        Resources res = parent.getResources();
        Drawable icon_bg = res.getDrawable(R.drawable.menu_icon_bg);
        Drawable icon = res.getDrawable(R.drawable.menu_icon);

        // Not working
        int int_icon_height = icon.getIntrinsicHeight();
        int int_icon_width = icon.getIntrinsicWidth();
        Rect rect_icon_bg_bound = new Rect();
        rect_icon_bg_bound.left = 0;//(int) Math.round(int_icon_width*-1.5);
        rect_icon_bg_bound.right = (int) Math.round(int_icon_width*1.5);
        rect_icon_bg_bound.top = 0;//(int)Math.round(int_icon_height*-1.5);
        rect_icon_bg_bound.bottom = (int)Math.round(int_icon_height*1.5);
        icon_bg.setBounds(rect_icon_bg_bound);

        Drawable[] layers = new Drawable[2];
        layers[0] = icon_bg;
        layers[1] = icon;

        LayerDrawable layerDrawable = new LayerDrawable(layers);
        ImageView iv_icon_combined = new ImageView(getApplicationContext());
        iv_icon_combined.setImageDrawable(layerDrawable);

我现在面临的问题是添加填充,以便图标在背景中有一些间距.

希望能在这里得到一些启发,先谢谢.

解决方法:

我刚遇到同样的问题.看一下LayerDrawable上的setLayerInset方法.它将图层级别作为参数,然后是drawable的左侧,顶部,右侧,底部边界的修改器.

上一篇:docker 通过overlay 构建跨主机联网通信


下一篇:android – Overlay.draw()调用很多次