How to mask alpha color

Working in Android (1.5), I have hundreds of grayscale images as arrays of bytes. I want to use images as alpha masks for drawing solid colors in a canvas. Images are fixed, but colors may vary. I can create Bitmap objects for every combination of images / colors, but this 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 combination of images / colors.)

+4
source share
3 answers

I think I found the answer I was looking for:

  • Create a bitmap ARGB_8888 where each pixel color is set (gray <24) | 0xFFFFFF.
  • For each color, create a new PorterDuffColorFilter (color, PorterDuff.Mode.MULTIPLY).
  • To render, create a Paint object and call setColorFilter () with a filter matching the color that will be used. Then call canvas.drawBitmap using the Bitmap and Paint objects.

For one color, this is probably not as fast as building exactly the bitmap that I want and drawing without a Paint object, but it is much more space than a bitmap for each image / color combination.

+4
source

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); 
+1
source

I would use drawImage to split the image into canvas, getImageData() to access the pixels, and then scroll through the .data of the image data, setting the RGB values ​​for each pixel to a constant and the fourth (alpha) value you got from the image. Then you can overlay this translucent canvas on everything you need.

Change I posted a working example of this on my website. Only works with Chrome / Safari.

0
source

Source: https://habr.com/ru/post/1332193/


All Articles