Reputation: 234795
Working in Android (1.5), I have a hundred or so grayscale images in the form of byte arrays. I want to use the images as alpha masks for drawing solid colors in a Canvas. The images are fixed but the colors can change. I can create Bitmap objects for each image/color combination, but that seems terribly inefficient. What would be a good way to approach this problem, in terms of both memory and speed? (I need to do this many times for each image/color combo.)
Upvotes: 4
Views: 5313
Reputation: 29
float contrast = 100/ 180.f; float scale = contrast + 1.f;
cm.set(new float[] {
scale, 0, 0, 0, 0,//Red
0, 1.5f, 0, 0, 0,//Green
0, 0, 1.5f, 0, 0,//Blue
0, 0, 0, 1, 0 });//alpha
bmpGrayscale = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bitmap, 0, 0, paint);
/*BitmapDrawable bmd = new BitmapDrawable(bmpGrayscale);
photo_view.setBackgroundDrawable(bmd);*/
photo_view.setImageBitmap(bmpGrayscale);
Upvotes: 1
Reputation: 303224
I would use drawImage
to blit the image to a canvas, getImageData()
to access the pixels, and then loop over the .data
of the image data, setting the RGB values for each pixel to your constant and the fourth (alpha) value to what you got from the image. You can then layer this semi-transparent canvas over whatever you need.
Edit: I've put a working example of this on my website. Only works on Chrome/Safari.
Upvotes: 0
Reputation: 234795
I think I found the answer I was looking for:
For a single color, this probably isn't as fast as building exactly the Bitmap I want and drawing without a Paint object, but it's much more space efficient than a Bitmap for each image/color combo.
Upvotes: 4