How to use grayscale values as alpha for a color fill?

Problems with Canvas, OpenGL, etc...

How to use grayscale values as alpha for a color fill?

Postby Ted Hopp » Wed Dec 15, 2010 9:16 pm

I'm given large number of grayscale images, each as an array of grayscale values. For each image, I'd like to use the values as the alpha for drawing a single-color image of the same size in an arbitrary color. I can create a Bitmap where the RGB for each pixel is the solid color and the A is the corresponding grayscale value, and this works. However, I need to support multiple colors and creating a new Bitmap for each image/color combination seems terribly inefficient. Is there a more efficient approach in terms of memory and/or rendering speed?
Ted Hopp
Ted Hopp
Freshman
Freshman
 
Posts: 5
Joined: Fri Jan 18, 2008 5:56 pm

Top

Re: How to use grayscale values as alpha for a color fill?

Postby Ted Hopp » Thu Dec 16, 2010 12:23 am

Well, I answered my own question. :)

  • Create an ARGB_8888 Bitmap where each pixel color is set to (gray << 24) | 0xFFFFFF.
  • For each color, create a new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY).
  • To render, create a Paint object and call setColorFilter() with the filter corresponding to the color to be used. Then call canvas.drawBitmap using the Bitmap and Paint objects.
Ted Hopp
Ted Hopp
Freshman
Freshman
 
Posts: 5
Joined: Fri Jan 18, 2008 5:56 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: Google [Bot] and 1 guest