[英]How to change the color of a drawable on every click?
我有一個背景設置為可繪制的按鈕,可以在其上獲得圓角。 我試圖讓按鈕在每次單擊時更改顏色,而不必使我要使用的每種顏色都可繪制。
目前,我正在嘗試使用PorterDuff在可繪制對象的白色背景上應用濾色器。
Drawable mDrawable = ResourcesCompat.getDrawable(getResources(),
R.drawable.rounded_button, null);
mDrawable.setColorFilter(new PorterDuffColorFilter(0x800000ff,
PorterDuff.Mode.MULTIPLY));
當我加載應用程序時,按鈕保持白色。 對我做錯了什么或解決此問題的更好方法有任何想法嗎?
嘗試這個
Drawable myIcon = getResources().getDrawable( R.drawable.button );
ColorFilter filter = new LightingColorFilter( Color.BLACK, Color.BLACK);
myIcon.setColorFilter(filter);
要么
ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setColorFilter(getString(R.color.your_color));
要么
int iColor = Color.parseColor(color);
int red = (iColor & 0xFF0000) / 0xFFFF;
int green = (iColor & 0xFF00) / 0xFF;
int blue = iColor & 0xFF;
float[] matrix = { 0, 0, 0, 0, red,
0, 0, 0, 0, green,
0, 0, 0, 0, blue,
0, 0, 0, 1, 0 };
ColorFilter colorFilter = new ColorMatrixColorFilter(matrix);
drawable.setColorFilter(colorFilter);
要么
ImageView lineColorCode = (ImageView)convertView.findViewById(R.id.line_color_code);
int color = Color.parseColor("#AE6118"); //The color u want
lineColorCode.setColorFilter(color);
請參考此鏈接。您可以獲得一些想法。 單擊時如何更改Android中按鈕的顏色?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.