Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill bitmap object with color in Android

Tags:

android

bitmap

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

like image 738
Minh Khac Avatar asked Jul 17 '10 18:07

Minh Khac


People also ask

How do I change the bitmap color in Android?

BitmapDrawable BD = (BitmapDrawable) I. getDrawable(); bitmap = BD. getBitmap(); Then when ever user clicks the button we set the second bitmap variable with the first variable and change the color of the ImageView like this: O = Bitmap.

How do I know if my Android BMP is empty?

You can do a check when you want to return the BitMap look to see if the ArrayList of Paths is bigger than 0 and return the BitMap if so, or else return null.

What is recycled bitmap?

If you're displaying large amounts of bitmap data in your app, you're likely to run into OutOfMemoryError errors. The recycle() method allows an app to reclaim memory as soon as possible. Caution: You should use recycle() only when you are sure that the bitmap is no longer being used.


2 Answers

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

like image 111
Peter Avatar answered Sep 20 '22 11:09

Peter


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);
like image 27
Quentin S. Avatar answered Sep 22 '22 11:09

Quentin S.