How to fill bitmap object with color in Android

19,123

Solution 1

If you are wanting to fill a bitmap with a solid colour you could try using

Bitmap b = /*however you get a bmp*/
b.eraseColor(color)

It clears the bitmap by filling all pixels with a colour.

Might be the effect you want

Peter

Solution 2

This question may be old, but there is a much simpler solution for this. Instead of applying a filter on the Bitmap image, you can apply it on the ImageView directly:

imageView.setColorFilter(tintColor, PorterDuff.Mode.MULTIPLY);

You can try different color filter modes (ADD, CLEAR, DARKEN, MULTIPLY, ...) depending on your needs and the bitmap supplied to your ImageView.

If it doesn't work, try removing the mode:

imageView.setColorFilter(tintColor);

Solution 3

Get a mutable copy of the Bitmap by the copy() method, and modify it's pixels.

Share:
19,123

Related videos on Youtube

Minh Khac
Author by

Minh Khac

Updated on April 28, 2022

Comments

  • Minh Khac
    Minh Khac about 2 years

    I'm working on Android game and there are some problem appear I want to fill a color on bitmap object but can not I tried bitmap.setPixel but my Image is PNG format (like a circle or unsharp, surrounded with transparent color) and android can not getHeight() or getWidth(), ie

    ImageView i = new ImageView(mContext);
    Bitmap bMap = BitmapFactory.decodeResource(this.mContext.getResources(), mImageIds[position]);

    // for(int i1 = 0; i1 < bMap.getHeight();i1++) // for(int j = 0; j < bMap.getWidth(); j ++) // bMap.setPixel(i1, j, Color.RED); //can not set

    i.setImageBitmap(bMap); i.setLayoutParams(new Gallery.LayoutParams(150, 100)); i.setScaleType(ImageView.ScaleType.FIT_XY); i.setBackgroundResource(mGalleryItemBackground); i.setBackgroundColor(Color.TRANSPARENT);