2010-01-09 86 views
5

我有一些.png圖標是alpha蒙版。我需要使用Android SDK將它們渲染爲可繪製的圖像。如何使用alpha蒙版剪輯和填充畫布

在iPhone上,我使用以下方法來得到這樣的結果,採用了黑色的「圖像」阿爾法遮掩轉換爲「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); ??? 

我沒有找到如何使用圖像做剪裁上imageMasked。

+0

我可能沒有得到這個問題,但如果你與阿爾法圖像視圖分配圖像 - 阿爾法將在那裏。 – 2010-01-09 03:25:09

+0

我在這裏要做的是將alpha位圖轉換爲剪切區域並將其用於填充。基本上應用一個alpha蒙版。 任何其他採取答案? – 2010-01-19 21:51:36

+0

我想要做的是沿着這些線: final int width = icon.getMinimumWidth(); final int height = icon.getMinimumHeight(); \t \t \t \t 位圖iconMasked = Bitmap.createBitmap(width, height,Config.ARGB_8888); Canvas canvas = new Canvas(iconMasked); final Region region = icon.getTransparentRegion(); canvas.clipRegion(region); canvas.drawARGB(255,0,0,0); 這種方法的問題是我的圖標是從getTransparentRegion()返回null。沒想到,所以我還在尋找答案。 – 2010-01-19 22:46:21

回答

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