2017-04-02 215 views
1

如果要查看資源,我有資源png of a house已鏈接。它基本上只是一個黑暗的主頁按鈕。更改資源顏色

我已將它包含並作爲圖像資源導入爲可繪製。

我試圖將其設置爲按鈕,但通過編程將其更改爲白色。

下面是我試圖改變顏色爲白色:

 ImageButton txtbtnAccept = new ImageButton(this); 
     this._surveyHomeButton = txtbtnAccept; 
     txtbtnAccept.setId(this.generateViewId()); 
     txtbtnAccept.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
     layoutParams = (RelativeLayout.LayoutParams) txtbtnAccept.getLayoutParams(); 
     layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); 
     layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
     layoutParams.height = 200; 
     layoutParams.width = 200; 
     txtbtnAccept.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     txtbtnAccept.setAdjustViewBounds(true); 

     txtbtnAccept.setLayoutParams(layoutParams); 

     txtbtnAccept.setBackgroundResource(0); 
     txtbtnAccept.setVisibility(View.GONE); 

     Bitmap homeImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_home); 
     Drawable whiteImg = new BitmapDrawable(getResources(), homeImage); 

     whiteImg.setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY); 
     txtbtnAccept.setImageDrawable(whiteImg); 
     txtbtnAccept.setImageBitmap(homeImage); 

任何想法,我要去哪裏錯了嗎?

回答

2

你想要什麼叫做着色。看看DrawableCompat.setTint(Drawable, int)

+0

帶我走了正確的道路 - 我結束了在這裏使用一個例子 - http://stackoverflow.com/questions/20121938/how-to-set-tint-for-an-image-view-programmatically-in-安卓 –