2011-03-11 38 views
5

我正在嘗試爲我的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

回答

5

我剛剛遇到了同樣的問題。看看LayerDrawable上的setLayerInset方法。它將圖層級別作爲參數,然後將修改器用於繪圖的左側,頂部,右側和底部邊界。

+0

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