改变资源的颜色

时间:2017-04-02 20:41:53

标签: java android drawable android-drawable android-resources

我想要查看资源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);

知道我哪里出错了吗?

1 个答案:

答案 0 :(得分:2)

你想要的是所谓的着色。看看DrawableCompat.setTint(Drawable, int)