简体   繁体   中英

Change color of resource

I have a resource png of a house linked if you want to view it. It's basically just a dark home button.

I have it included and imported as an image asset as a drawable.

I'm attempting to set it as a button, but change it to be a white color programmatically.

Here's how I'm attempting to change the color to be white:

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

Any idea where I'm going wrong?

What you want is called tinting. Have a look at DrawableCompat.setTint(Drawable, int) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM