非透明像素上的 ImageView ColorFilter。剪辑

问题描述 投票:0回答:3

我有一个带有位图的 ImageView。此位图具有 alpha 通道和透明像素。 当我尝试将 ColorFiter 与 Mode.OVERLAY(自蜂窝)一起使用时 - 提供颜色覆盖整个图像视图(整个矩形),但我只想覆盖非透明像素。我如何剪辑图像视图的画布以在我想要的地方执行过滤器?

已更新

我在 png 中有灰色图像:

enter image description here

当我尝试使用 MODE_ATOP 时,我得到:

enter image description here

当我使用 OVERLAY 时,我得到:

enter image description here

我想得到的:

enter image description here

android image mask clip
3个回答
4
投票

可能有一种更有效的方法来做到这一点(也许通过创建一个

ColorMatrixColorFilter
来近似它),但是由于
Mode.OVERLAY
似乎 hard to simplify otherwise,这里有一些示例代码应该实现什么你想要:

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final ImageView imageView = new ImageView(this);
        setContentView(imageView);

        final Paint paint = new Paint();
        Canvas c;

        final Bitmap src = BitmapFactory.decodeResource(getResources(),
                android.R.drawable.sym_def_app_icon);
        final int overlayColor = Color.RED;

        final Bitmap bm1 = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Config.ARGB_8888);
        c = new Canvas(bm1);
        paint.setColorFilter(new PorterDuffColorFilter(overlayColor, PorterDuff.Mode.OVERLAY));
        c.drawBitmap(src, 0, 0, paint);

        final Bitmap bm2 = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Config.ARGB_8888);
        c = new Canvas(bm2);
        paint.setColorFilter(new PorterDuffColorFilter(overlayColor, PorterDuff.Mode.SRC_ATOP));
        c.drawBitmap(src, 0, 0, paint);

        paint.setColorFilter(null);
        paint.setXfermode(new AvoidXfermode(overlayColor, 0, Mode.TARGET));
        c.drawBitmap(bm1, 0, 0, paint);

        imageView.setImageBitmap(bm2);
    }

}

简而言之,我们使用

OVERLAY
模式绘制源位图和颜色,然后使用辅助位图(使用
SRC_ATOP
模式合成),我们使用
AvoidXfermode
组合它以不绘制透明像素。

原图:

original image

结果:

result


1
投票

您可以使用Overlay模式,然后使用DST_ATOP xFerMode用相同的位图裁剪出以前透明的区域。 https://developer.android.com/reference/android/graphics/PorterDuff.Mode

private fun applyFilterToImage() {

    val bitmapCopy = Bitmap.createBitmap(originalImage.width, originalImage.height, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(bitmapCopy)

    val rnd = java.util.Random()
    val randomColor = Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256))

    val paint = Paint()
    val porterDuffMode = PorterDuff.Mode.OVERLAY
    paint.colorFilter = PorterDuffColorFilter(randomColor, porterDuffMode)

    val maskPaint = Paint()
    maskPaint. xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_ATOP)

    canvas.drawBitmap(originalImage, 0f, 0f, paint)

    canvas.drawBitmap(originalImage, 0f, 0f, maskPaint) //clips out the background that used to be transparent.

    imageView.setImageBitmap(bitmapCopy)
}

MarkerColorChangeGIF


0
投票

我的案例,合并 2 个位图层:

    final Paint paint = new Paint();
    final Bitmap dst_bmp = Bitmap.createBitmap(front_bmp.getWidth(), front_bmp.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dst_bmp);
    paint.setColorFilter(new PorterDuffColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP));
    canvas.drawBitmap(black_bmp, 0, 0, null);
    canvas.drawBitmap(front_bmp, 0, 0, paint);
© www.soinside.com 2019 - 2024. All rights reserved.