改变资源的颜色
Change color of resource
如果你想查看,我有一个资源 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);
知道我哪里出错了吗?
你要的叫调色。看看 DrawableCompat.setTint(Drawable, int).
如果你想查看,我有一个资源 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);
知道我哪里出错了吗?
你要的叫调色。看看 DrawableCompat.setTint(Drawable, int).