2017-02-04 4 views
0

배경에 둥근 모서리를 얻기 위해 드로어 블 (drawable)로 설정된 버튼이 있습니다. 내가 사용하려는 모든 색상의 드로어 블을 만들 필요없이 버튼을 클릭 할 때마다 색상을 변경하려고합니다.클릭 할 때마다 드로어 블의 색상을 변경하는 방법은 무엇입니까?

현재 드로어 블의 흰색 배경 위에 컬러 필터를 적용하려면 PorterDuff을 사용하려고합니다.

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

앱을로드하면 버튼이 흰색으로 유지됩니다. 내가 잘못하고있는 일이나 이것에 관해 더 좋은 방법에 대해 알고 싶습니다.

+0

RGB를 생성하기 위해 랜덤 메소드를 사용하십시오. –

+0

@RajeshKushvaha 문제는 색상을 선택하는 방법이 아니라 버튼에 표시하는 방법입니다. 현재 drawable xml 파일을 직접 편집하여 색상을 변경할 수 있습니다. 내가 찾고있는 솔루션은 drawable의 색상을 프로그래밍 방식으로 변경하는 방법입니다. – 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