gpt4 book ai didi

android - 如何从另一个位图中剪切一个位图

转载 作者:行者123 更新时间:2023-11-29 18:13:01 25 4
gpt4 key购买 nike

我需要从 Bitmap2 中剪切 Bitmap1..例如,我有 Bitmap1(从 Resources drawable 解码)和 Bitmap2(也从 Resources drawable 解码)。

位图 1:

|   |
> <
| |

位图2:

|xxx|
|xxx|
|xxx|

我需要结果:

|xxx|
>x<
|xxx|

谁能给我示例代码?

安卓系统。

最佳答案

您可以加载两个位图并使用 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.

关于android - 如何从另一个位图中剪切一个位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9669685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com