Android ColorFilter - Porter-Duff 模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10245681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android ColorFilter - Porter-Duff Modes
提问by Alexander Taran
I'm trying to solve a problem with android ColorFilters
. Documentation is very poor, so the main method is trying different variants.
我正在尝试解决 android 的问题ColorFilters
。文档很差,所以主要方法是尝试不同的变体。
The problem:
问题:
There is a Bitmap. Some pixels have alpha=255, others have alpha=0. I'm trying to draw a circle with a specific color. I want alpha channel unchanged in the bitmap, but while drawing I want to multiply a color to the bitmap-alpha.
有一个位图。一些像素的 alpha=255,其他像素的 alpha=0。我正在尝试用特定颜色绘制一个圆圈。我希望位图中的 alpha 通道保持不变,但是在绘制时我想将一种颜色与位图 alpha 相乘。
So, while drawing a circle I want pixels with alpha=0 to be not painted, but pixels with alpha=255 to be painted in color which I want. Alpha channel shouldn't change.
因此,在绘制圆时,我希望不绘制 alpha=0 的像素,而是绘制 alpha=255 的像素,以我想要的颜色。Alpha 通道不应该改变。
I'm trying to use porter-duff ColorFilter (PorterDuffColorFilter class in android sdk).
我正在尝试使用 porter-duff ColorFilter(android sdk 中的 PorterDuffColorFilter 类)。
there are too many modes and no-understandable description on official site here : http://developer.android.com/reference/android/graphics/PorterDuff.Mode.html
这里的官方网站上有太多模式和无法理解的描述:http: //developer.android.com/reference/android/graphics/PorterDuff.Mode.html
I think I should use DST_ATOP
or SRC_ATOP
, but they don't work as I described.
我想我应该使用DST_ATOP
or SRC_ATOP
,但它们不像我描述的那样工作。
Also, there is a strange parameter srcColor
in constructor of porter-duff colorfilter.
此外,srcColor
porter-duff colorfilter 的构造函数中有一个奇怪的参数。
I can't understand what "Sa" and "Sc" means in formulas [Da, Sc * Da + (1 - Sa) * Dc]
. It can be from color which was passed into colorfilter constructor and also it can be color set by "paint.setColor".
我无法理解公式中的“Sa”和“Sc”是什么意思[Da, Sc * Da + (1 - Sa) * Dc]
。它可以来自传递给 colorfilter 构造函数的颜色,也可以由“paint.setColor”设置颜色。
Anybody knows, how it works?
有谁知道,它是如何工作的?
回答by Jave
Sa
and Sc
are shorts for "source alpha" and "source color", respectively. The srcColor
parameter in the PorterDuffColorFilter
constructor is the color used for these values.
Sa
和Sc
分别是“源alpha”和“源颜色”的缩写。构造函数中的srcColor
参数PorterDuffColorFilter
是用于这些值的颜色。
For your case the Mode.MULTIPLY
would probably work best.
对于您的情况,Mode.MULTIPLY
这可能效果最好。
回答by Lawrence D'Oliveiro
Note that Porter-Duff modes are only defined to work properly with premultiplied alpha. That means that none of the R, G or B components can exceed the alpha value.
请注意,Porter-Duff 模式仅被定义为与预乘 alpha 一起正常工作。这意味着 R、G 或 B 分量都不能超过 alpha 值。
GitHub projectfor the Android project which shows off all the Porter-Duff modes. The Android Appis also available on Playstore.
用于展示所有 Porter-Duff 模式的 Android 项目的GitHub 项目。该Android应用程序也可在Play商店中。
回答by om252345
After some research I came to following conclusions regarding PorterDuff Color modes in Android,
经过一些研究,我得出以下关于 Android 中 PorterDuff 颜色模式的结论,
- Sa and Sc are Source Alpha and Source Color, e.g. if I am setting ColorFilter to a drawable like myDrawable.setColorFilter(desiredColor,Mode.SRC_ATOP) the Sa and Sc values will be taken from desiredColor not from myDrawable. Da and Dc will be original alpha and color values of myDrawable.
I had an Image and I want to apply certain color with alpha values from user input. I tried to use PorterDuff Modes, My original myDrawable was a nine patch drawable and it had transparent background and trangular corners like messaging bubble triangles. When I used
public static final PorterDuff.Mode SRC [Sa, Sc]
- Sa 和 Sc 是源 Alpha 和源颜色,例如,如果我将 ColorFilter 设置为像 myDrawable.setColorFilter(desiredColor,Mode.SRC_ATOP) 这样的可绘制对象,则 Sa 和 Sc 值将从所需颜色中获取,而不是从 myDrawable 中获取。Da 和 Dc 将是 myDrawable 的原始 alpha 和颜色值。
我有一个图像,我想使用来自用户输入的 alpha 值应用某些颜色。我尝试使用 PorterDuff 模式,我原来的 myDrawable 是一个九块可绘制的,它有透明的背景和像消息气泡三角形一样的三角形角。当我使用
公共静态最终 PorterDuff.Mode SRC [Sa, Sc]
which applies Source color and Source Alpha, which I supposed Color and Alpha values of above setColorFilter method desiredColor, I got color values correct i.e. Color and alpha was correctly applied to new Drawable but triangle on that image was completely washed/removed. So I had to use ColorMatrixFilter and setup matrix and set alpha value separately to drawable.
它应用了源颜色和源 Alpha,我认为上面 setColorFilter 方法所需的颜色和 Alpha 值是正确的,即颜色和 alpha 已正确应用于新的 Drawable,但该图像上的三角形已被完全清洗/删除。所以我不得不使用 ColorMatrixFilter 和设置矩阵并将 alpha 值单独设置为可绘制。