反转可绘制Android的颜色

Car*_*ira 19 android colors drawable

可以在Drawable中"反转"颜色吗?你有什么消极的,你知道吗?

我知道可以将Drawable改为黑白......反转颜色怎么样?

谢谢!

Car*_*ira 50

经过一些研究,我发现解决方案比我想象的要简单得多.

这里是:

  /**
   * Color matrix that flips the components (<code>-1.0f * c + 255 = 255 - c</code>)
   * and keeps the alpha intact.
   */
  private static final float[] NEGATIVE = { 
    -1.0f,     0,     0,    0, 255, // red
        0, -1.0f,     0,    0, 255, // green
        0,     0, -1.0f,    0, 255, // blue
        0,     0,     0, 1.0f,   0  // alpha  
  };

  drawable.setColorFilter(new ColorMatrixColorFilter(NEGATIVE));
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。此外,它还适用于任何源(可绘制、位图等)的图像视图。只需执行相同的``imageView.setColorFilter(new ColorMatrixColorFilter(NEGATIVE))```` (2认同)

Vic*_*cVu 2

最好的方法是将您的可绘制对象转换为位图:

Bitmap fromDrawable = BitmapFactory.decodeResource(context.getResources(),R.drawable.drawable_resource);
Run Code Online (Sandbox Code Playgroud)

然后将其反转如下:

/sf/answers/323793291/

如果需要,然后返回到可绘制对象:

Drawable invertedDrawable = new BitmapDrawable(getResources(),fromDrawable);
Run Code Online (Sandbox Code Playgroud)