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);
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
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);
Get a mutable copy of the Bitmap by the copy() method, and modify it's pixels.
精彩评论