Điều gì sẽ là đúng cách để biến một màu Có thể vẽ thành một màu xám (để biểu thị trạng thái bị vô hiệu hóa)?Drawable => grayscale
EDIT:
B/W => màu xám
Điều gì sẽ là đúng cách để biến một màu Có thể vẽ thành một màu xám (để biểu thị trạng thái bị vô hiệu hóa)?Drawable => grayscale
EDIT:
B/W => màu xám
Tôi biết câu hỏi này đã được hỏi một thời gian trước, nhưng tôi đi qua một giải pháp đơn giản mà làm việc nếu bạn có thể vẽ và bạn chỉ muốn hiển thị rằng drawable cùng màu xám. Không cần phải có canvas hoặc họa sĩ ...
protected Drawable convertToGrayscale(Drawable drawable)
{
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
drawable.setColorFilter(filter);
return drawable;
}
Hy vọng điều này cũng hữu ích!
Rõ ràng bạn có thể sử dụng lớp ColorMatrix
để làm bất kỳ loại biến đổi màu sắc trong không gian. Nó có một phương pháp setSaturation()
dễ dàng tạo ra một chuyển đổi màu sắc sang màu xám (zeroes saturation) cho bạn.
Vì vậy, bạn có thể sử dụng bộ lọc đó để vẽ bản sao mới của hình ảnh. Tôi đã không thử điều này, nhưng nó sẽ làm việc:
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);
Bạn đặc biệt muốn thực hiện điều này theo chương trình chứ không chỉ với các phiên bản bị tắt của hình ảnh? Bạn có thể tham chiếu một XML có thể vẽ được, chẳng hạn như:
<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>
Vâng, tôi muốn thực hiện trên cơ sở từng trường hợp. Cảm ơn cho đoạn trích mặc dù. – yanchenko
Một số coments cho câu trả lời của @intgr.
1. Bitmap.Config.ARGB_8888
để bảo vệ kênh alpha.
2. Một ít mã thêm:
//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);
Small Typo: Bitmap.Config.RGB_8888 phải là Bitmap.Config.ARGB_8888. Hoạt động tốt sau đó, cảm ơn. – Minsky
wow, thật đơn giản, tuyệt vời! – tacone
Người đàn ông, bạn là một người hướng dẫn, cảm ơn một cái bánh! – Sipty
Mẹo hay! Một điều thú vị là nếu bạn chạy phương thức này bằng cách chuyển ContextCompat.getDrawable (context, R.drawable.myId); lần tiếp theo bạn cần có cùng độ phân giải, bộ lọc màu đã được đặt. Đối với phần của tôi, tôi cần phải loại bỏ các bộ lọc màu sắc để có được màu sắc trở lại trên drawable của tôi :) – Tobliug