如何将彩色Drawable转换为灰度Drawable(以指示禁用状态)?
编辑:
B / W => 灰度
我知道这个问题早就有人问过了,但是我找到了一个更简单的解决方案。如果你有一个Drawable并且只想在灰度下显示同样的drawable,那么不需要画布或者绘制器...
protected Drawable convertToGrayscale(Drawable drawable)
{
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
drawable.setColorFilter(filter);
return drawable;
}
显然你可以使用ColorMatrix
类来进行任何形式的颜色空间转换。它有一个setSaturation()
方法,可以轻松地为你创建彩色到灰度的转换(将饱和度置为零)。
因此,你可以使用该滤镜对图像进行重新绘制。我没有尝试过这个方法,但它应该有效:
Bitmap grayscaleBitmap = Bitmap.createBitmap(
colorBitmap.getWidth(), colorBitmap.getHeight(),
Bitmap.Config.RGB_565);
Canvas c = new Canvas(grayscaleBitmap);
Paint p = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(cm);
p.setColorFilter(filter);
c.drawBitmap(colorBitmap, 0, 0, p);
针对 @intgr 的回答,我有一些评论。
1. 使用Bitmap.Config.ARGB_8888
来保留 alpha 通道。
2. 可以加入一些额外的代码:
//remember, you are converting a .png image, as opposed to a Drawable defined in .xml
Bitmap colorBitmap = ((BitmapDrawable)drawable).getBitmap();
// the code by intgr
Drawable grayscaleDrawable = new BitmapDrawable(grayscaleBitmap);
你是否特别想要程序化地实现,而不仅仅是使用被禁用的图像版本? 你可以引用一个XML drawable,类似于:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/btn_default_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/btn_default_normal_disable" />
<item android:state_pressed="true"
android:drawable="@drawable/btn_default_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="@drawable/btn_default_selected" />
<item android:state_enabled="true"
android:drawable="@drawable/btn_default_normal" />
<item android:state_focused="true"
android:drawable="@drawable/btn_default_normal_disable_focused" />
<item
android:drawable="@drawable/btn_default_normal_disable" />
</selector>