2017-02-04 66 views
0

我有一个背景设置为可绘制的按钮,以获得圆角。我试图让按钮在每次点击时改变颜色,而不必绘制我想要使用的每种颜色的可绘图。如何在每次点击时更改绘图的颜色?

目前我正在尝试使用PorterDuff在可绘制的白色背景上应用颜色过滤器。

Drawable mDrawable = ResourcesCompat.getDrawable(getResources(), 
    R.drawable.rounded_button, null); 
mDrawable.setColorFilter(new PorterDuffColorFilter(0x800000ff, 
    PorterDuff.Mode.MULTIPLY)); 

当我加载应用程序时,按钮保持白色。任何想法我做错了什么或更好的方式去做这件事?

+0

使用'随机'方法来生成RGB –

+0

@RajeshKushvaha问题不在于我如何选择颜色,而是如何让它们出现在按钮上。目前我只能通过直接编辑可绘制的xml文件来让它们改变颜色。我正在寻找的解决方案是如何以编程方式更改绘图的颜色。 – Vinnie

回答

0

试试这个

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); 

或使用此库 https://github.com/TakeoffAndroid/IconColorChanger/blob/master/app/src/main/java/com/takeoffandroid/iconcolorchanger/IconChangerActivity.java

相关问题