是否可以在Drawable中“反转”颜色?你有什么消极的,你知道吗?
我知道可以将Drawable改为黑白......反转颜色怎么样?
谢谢!
答案 0 :(得分:45)
这是:
/**
* 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));
答案 1 :(得分:1)
执行此操作的最佳方法是将drawable转换为位图:
Bitmap fromDrawable = BitmapFactory.decodeResource(context.getResources(),R.drawable.drawable_resource);
然后按照以下方式将其反转:
https://stackoverflow.com/a/4625618/1154026
如果你需要,那么回到drawable:
Drawable invertedDrawable = new BitmapDrawable(getResources(),fromDrawable);
答案 2 :(得分:0)
根据您的回答,我为Drawable创建了简短的Kotlin扩展
private val negative = floatArrayOf(
-1.0f, .0f, .0f, .0f, 255.0f,
.0f, -1.0f, .0f, .0f, 255.0f,
.0f, .0f, -1.0f, .0f, 255.0f,
.0f, .0f, .0f, 1.0f, .0f
)
fun Drawable.toNegative() {
this.colorFilter = ColorMatrixColorFilter(negative)
}