如何将位图着色为纯色

发布于 2024-11-15 16:54:50 字数 187 浏览 2 评论 0原文

如何将位图着色为纯色,有效地替换具有 alpha > 1 的所有像素。 0 到给定的 RGB 值?另外如何做同样的事情,但保持每个像素的阿尔法?我不是在寻找每像素操作,因为它们往往很慢。

我尝试使用 ColorMatrixColorFilter 和 ColorFilter,它们确实对位图进行着色,但它们是着色而不是执行 100% 着色。

How would one go about tinting a Bitmap to a solid color, effectively replacing all pixels that have an alpha > 0 to a given RGB value? In addition how to do the same thing, but keeping the alpha for every pixel? I'm not looking for per-pixel operations as they tend to be slow.

I tried using a ColorMatrixColorFilter and a ColorFilter, which do tint the Bitmap, but they colorize instead of performing a 100% tint.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那一片橙海, 2024-11-22 16:54:50

我通过使用 PorterDuffColorFilter 解决了这个问题

Paint paint = new Paint();
paint.setColorFilter(new PorterDuffColorFilter(targetColor, PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(resource, matrix, paint);

I solved this by using a PorterDuffColorFilter

Paint paint = new Paint();
paint.setColorFilter(new PorterDuffColorFilter(targetColor, PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(resource, matrix, paint);
浅忆流年 2024-11-22 16:54:50

只是为了给出更完整的答案。

这将获取一个位图并输出一个新的着色位图:

public static Bitmap tintImage(Bitmap bitmap, int color) {
    Paint paint = new Paint();
    paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
    Bitmap bitmapResult = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmapResult);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return bitmapResult;
}

Just to give a more complete answer.

This will take a bitmap and output a new tinted bitmap:

public static Bitmap tintImage(Bitmap bitmap, int color) {
    Paint paint = new Paint();
    paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
    Bitmap bitmapResult = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmapResult);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return bitmapResult;
}
金兰素衣 2024-11-22 16:54:50

如果您的位图是要在布局中使用的可绘制对象,则可以创建一个引用原始可绘制对象(例如 .png)的新可绘制对象 (.xml)。

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/plus"
     android:tint="#2c53e5" />

If your bitmap is a drawable that you want to use in a layout, then you can make a new drawable (.xml) that references your original drawable (e.g .png).

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/plus"
     android:tint="#2c53e5" />
醉生梦死 2024-11-22 16:54:50

Kotlin 解决方案,因为由于某种原因转换其他解决方案导致位图结果被剪切:

fun getTintedBitmap(inputBitmap: Bitmap, @ColorInt color: Int): Bitmap {
    val paint = Paint()
    paint.colorFilter = PorterDuffColorFilter(color, Mode.SRC_IN)
    val width = inputBitmap.width
    val height = inputBitmap.height
    val result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(result)
    canvas.drawBitmap(inputBitmap, null, Rect(0, 0, width, height), paint)
    return result
}

Kotlin solution, because for some reason converting the other solutions caused a cut in the bitmap result:

fun getTintedBitmap(inputBitmap: Bitmap, @ColorInt color: Int): Bitmap {
    val paint = Paint()
    paint.colorFilter = PorterDuffColorFilter(color, Mode.SRC_IN)
    val width = inputBitmap.width
    val height = inputBitmap.height
    val result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
    val canvas = Canvas(result)
    canvas.drawBitmap(inputBitmap, null, Rect(0, 0, width, height), paint)
    return result
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文