【发布时间】:2011-12-06 12:31:55
【问题描述】:
我正在尝试将两个图像与 Android 混合在一起,使用类似 Multiply 的混合模式。
// Prepare -------------------------------
// Create source images
Bitmap img1 = ...
Bitmap img2 = ...
// Create result image
Bitmap result = ...
Canvas canvas = new Canvas();
canvas.setBitmap(result);
// Get proper display reference
BitmapDrawable drawable = new BitmapDrawable(getResources(), result);
ImageView imageView = (ImageView)findViewById(R.id.imageBlend1);
imageView.setImageDrawable(drawable);
// Apply -------------------------------
// Draw base
canvas.drawBitmap(img1, 0, 0, null);
// Draw overlay
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));
paint.setShader(new BitmapShader(img2, TileMode.CLAMP, TileMode.CLAMP));
canvas.drawRect(0, 0, img2.getWidth(), img2.getHeight(), paint);
这可行,但我无法控制完成的乘法“数量” - 它始终是完整的乘法传输。理想情况下,0% 的乘法将与基本图像 (img1) 相同,没有任何更改,但 100% 的乘法将是我使用上述代码得到的结果。
paint.setAlpha() 似乎对此不起作用。
还有其他方法可以设置新“层”的不透明度百分比吗?
附:我猜有一些方法可以通过预乘并将颜色偏移为白色来使乘法工作(使用LightingColorFilter),但它非常特定于乘法模式..我正在尝试找到一种方法来应用opacity/% 也适用于所有其他传输模式。
【问题讨论】:
标签: android blending porter-duff