0

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

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

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

可绘制的

4

1 回答 1

0

这是我用于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

参见PorterDuff , PorterDuff.Mode, PorterDuffXfermode, ColorFilter, ColorMatrix , ColorMatrixColorFilter , PorterDuffColorFilter , Canvas, Color

于 2018-06-01T10:20:07.070 回答