2012-03-12 55 views
1

我需要從位圖2中剪切Bitmap1 .. 例如,我有Bitmap1(從資源drawable中解碼)和Bitmap2(從Resources中解碼也可繪製)。如何從其他位圖中剪切一個位圖

Bitmap1:

| | 
> < 
| | 

Bitmap2:

|xxx| 
|xxx| 
|xxx| 

我需要的結果是:

|xxx| 
>x< 
|xxx| 

有人能給我的示例代碼?

Android。

回答

3

你可以像這樣同時加載的位圖和使用PorterDuffXfermodeDST_IN掩蓋「Bitmap2」,排序的:

Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap2); 
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap1); 
Bitmap bitmap2MaskedByBitmap1 = Bitmap.createBitmap(bitmap2.getWidth(), bitmap2.getHeight(), bitmap2.getConfig()); 
Canvas canvas = new Canvas(bitmap2MaskedByBitmap1); 

Paint paint = new Paint(); 
paint.setFilterBitmap(false); 

canvas.drawBitmap(bitmap2, 0, 0, paint); 
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 
canvas.drawBitmap(bitmap1, 0, 0, paint); 
bitmap2.recycle(); 
bitmap1.recycle(); 

// bitmap2MaskedByBitmap1 should now contain the desired image 
// as long as your Bitmap1 mask isn't sh-t. 
+0

!大!謝謝!但現在我有黑色背景的圖像...我可以用空(alpha 0)背景來做到嗎? – user868395 2012-03-12 20:37:07

+1

ok)任務完成)Mode.XOR – user868395 2012-03-12 21:22:47

相關問題