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

问题描述:

我正在尝试为我的android应用程序创建一个动态图标菜单。 该图标由2个可绘制的9位补丁背景图像和图标图像叠加而成。Android:设置叠加图像背景中的填充

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

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

希望在这里得到一些启示,在此先感谢。

+0

嗨,我仍然坚持这个问题。有没有其他的地方可以帮助我? – InSheNaiDe 2011-03-17 03:50:44

我刚刚遇到了同样的问题。看看LayerDrawable上的setLayerInset方法。它将图层级别作为参数,然后将修改器用于绘图的左侧,顶部,右侧和底部边界。

+0

非常感谢。这行得通!! – InSheNaiDe 2011-08-16 06:53:34