【问题标题】:ColorFilter Add but ignore transparent partsColorFilter 添加但忽略透明部分
【发布时间】:2018-03-15 10:17:32
【问题描述】:

在我的应用程序中,我正在尝试更改标记图像的颜色。但它并不能完全按照我想要的方式工作。

Drawable drawable = getDrawable(R.drawable.ic_marker);
drawable.setColorFilter(item.getColor()), PorterDuff.Mode.ADD);

drawable 看起来像这样(下图),当我添加 ColorFilter 时,标记会获得正确的颜色并且白色保持白色,但图像的透明部分也会获得颜色。我只希望黑色改变,而白色和透明的部分必须保持这种状态。

【问题讨论】:

  • 尝试乘法,而不是加法,或者ColorMatrixColorFilter 来制作一些其他花哨的东西
  • 这样黑色保持黑色,白色获得新颜色。透明仍然是透明的。
  • 所以试试ColorMatrixColorFilter
  • 任何提示如何?我以前从未使用过 ColorMatrixColorFilter。我已将标记更改为不同的 RGB 代码。
  • 好吧,你为什么不使用LayerDrawable?使用LayerDrawable,您可以为您想要的任何图层着色

标签: android porter-duff


【解决方案1】:

这是我用于setColorFilterColorMatrixColorMatrixColorFilter 的实验:

drawable.setColorFilter(item.getColorMatrix());

drawable.setColorFilter(new ColorMatrixColorFilter(getColorMatrix5()));//custom 5

//custom 5
private ColorMatrix getColorMatrix5() 
{
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0);//make it greyscale
    ColorMatrix blueMatrix = new ColorMatrix(new float[] {
        0, 0, 0, 0, 0, // red
        0, 0, 0, 0, 0, // green
        1, 1, 1, 1, 1, // blue
        1, 1, 1, 1, 1  // alpha
    });
    // Convert, then scale and clamp
    colorMatrix.postConcat(blueMatrix);
    return colorMatrix;
}//getColorMatrix5

PorterDuffPorterDuff.ModePorterDuffXfermodeColorFilterColorMatrixColorMatrixColorFilterPorterDuffColorFilterCanvasColor

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-26
    • 2016-08-12
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多