Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clip and fill a canvas using an alpha mask

Tags:

android

I have some .png icons that are alpha masks. I need to render them as an drawable image using the Android SDK.

On the iPhone, I use the following to get this result, converting the "image" alpha mask to the 'imageMasked' image using black as a fill:

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, thumbWidth, 
    thumbHeight, 8, 4*thumbWidth, colorSpace, kCGImageAlphaPremultipliedFirst);
CGRect frame = CGRectMake(0,0,thumbWidth,thumbHeight);
CGContextClipToMask(context, frame, [image CGImage]);
CGContextFillRect(context, frame);

CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);

How do I accomplish the above in Android SDK?

I've started to write the following:

Drawable image = myPngImage;

final int width = image.getMinimumWidth();
final int height = image.getMinimumHeight();

Bitmap imageMasked = Bitmap.createBitmap(width,
    height, Config.ARGB_8888);
Canvas canvas = new Canvas(iconMasked);
image.draw(canvas); ???

I'm not finding how to do the clipping on imageMasked using image.

like image 837
Jay Koutavas Avatar asked Nov 05 '22 17:11

Jay Koutavas


1 Answers

Solved:

Drawable icon = An_Icon_That_Is_An_Alpha_Mask;
int width = icon.getIntrinsicWidth();
int height = icon.getIntrinsicHeight();
Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
Canvas canvas = new Canvas(bm);
icon.setBounds(new Rect(0,0,width,height));
icon.draw(canvas);
like image 102
Jay Koutavas Avatar answered Nov 15 '22 08:11

Jay Koutavas