如何使用alpha蒙版剪辑和填充画布

时间:2010-01-09 02:49:21

标签: android

我有一些alpha蒙版的.png图标。我需要使用Android SDK将它们渲染为可绘制的图像。

在iPhone上,我使用以下内容获得此结果,使用黑色作为填充将“图像”alpha蒙版转换为“imageMasked”图像:

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);

如何在Android SDK中完成上述操作?

我已经开始撰写以下内容:

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); ???

我没有找到如何使用image对imageMasked进行剪裁。

1 个答案:

答案 0 :(得分:2)

解决:

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);