코드로 흰색 마커 이미지의 색상을 변경하려고합니다. 아래 코드가 색상을 변경해야한다는 것을 읽었지만 마커는 흰색으로 유지됩니다.
Drawable.setColorFilter( 0xffff0000, Mode.MULTIPLY )
내가 뭐 놓친 거 없니? res 폴더에있는 드로어 블에서 색상을 변경하는 다른 방법이 있습니까?
코드로 흰색 마커 이미지의 색상을 변경하려고합니다. 아래 코드가 색상을 변경해야한다는 것을 읽었지만 마커는 흰색으로 유지됩니다.
Drawable.setColorFilter( 0xffff0000, Mode.MULTIPLY )
내가 뭐 놓친 거 없니? res 폴더에있는 드로어 블에서 색상을 변경하는 다른 방법이 있습니까?
답변:
이 시도:
Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, R.drawable.my_drawable);
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, Color.RED);
DrawableCompatAPI 22 디바이스 및 이전 버전에서 이전 버전과의 호환성 및 버그 수정을 제공하므로 사용 이 중요합니다.
OverlayItems문제를 일으킬 수있는 hello mapview 클래스와 관련이있을 수 있습니까? 내 res 폴더에서 일반 드로어 블, 특별한 아무것도 ...
svg 벡터 드로어 블에 대해 이것을 시도 할 수 있습니다
DrawableCompat.setTint(
DrawableCompat.wrap(myImageView.getDrawable()),
ContextCompat.getColor(context, R.color.another_nice_color)
);
null을Drawable#setTintList(ColorStateList)
드로어 블에서 mutate ()를 호출해야합니다. 그렇지 않으면 모든 아이콘이 영향을받습니다.
Drawable icon = ContextCompat.getDrawable(getContext(), R.drawable.ic_my_icon).mutate();
TypedValue typedValue = new TypedValue();
getContext().getTheme().resolveAttribute(R.attr.colorIcon, typedValue, true);
icon.setColorFilter(typedValue.data, PorterDuff.Mode.SRC_ATOP);
Lollipop, android 5. +에서이 작업을 수행하는 또 다른 방법은 비트 맵 드로어 블에 색조를 설정하는 것입니다.
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/ic_back"
android:tint="@color/red_tint"/>
드로어 블에 사용하려는 색상 수가 제한되어 있으면 효과가 있습니다. 자세한 내용은 내 블로그 게시물을 확인하십시오 .
minSdkVersion 16Android 4.1.1 기기에서만 테스트했습니다 .)
android:background="...". 꽤 이상하다!
당신은 이것을 시도 할 수 있습니다 ImageView. 사용하여 setColorFilter().
imageViewIcon.setColorFilter(ContextCompat.getColor(context, R.color.colorWhite));
컨텍스트를 전달할 수있는 일반 함수를 작성했습니다. 아이콘은 id drawable / mipmap 이미지 아이콘이며 해당 아이콘에 필요한 새로운 색상입니다.
이 함수는 드로어 블을 반환합니다.
public static Drawable changeDrawableColor(Context context,int icon, int newColor) {
Drawable mDrawable = ContextCompat.getDrawable(context, icon).mutate();
mDrawable.setColorFilter(new PorterDuffColorFilter(newColor, PorterDuff.Mode.SRC_IN));
return mDrawable;
}
changeDrawableColor(getContext(),R.mipmap.ic_action_tune, Color.WHITE);
키 색상이 흰색이므로 ColorMatrixColorFilter를 사용해 볼 수 있습니다.
// Assuming "color" is your target color
float r = Color.red(color) / 255f;
float g = Color.green(color) / 255f;
float b = Color.blue(color) / 255f;
ColorMatrix cm = new ColorMatrix(new float[] {
// Change red channel
r, 0, 0, 0, 0,
// Change green channel
0, g, 0, 0, 0,
// Change blue channel
0, 0, b, 0, 0,
// Keep alpha channel
0, 0, 0, 1, 0,
});
ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);
myDrawable.setColorFilter(cf);
이것은 나를 위해 일했습니다. 0x와 색상 코드 사이에 "ff"를 입력하십시오. 이 0xff2196F3처럼
Drawable mDrawable = ContextCompat.getDrawable(MainActivity.this,R.drawable.ic_vector_home);
mDrawable.setColorFilter(new
PorterDuffColorFilter(0xff2196F3,PorterDuff.Mode.SRC_IN));
Mode.LIGHTEN또는 을 시도 할 수 있습니다 Mode.DARKEN. 안드로이드 Javadocs는 PorterDuff Modes가 무엇을하는지 설명하는데 끔찍합니다. 여기서 볼 수 있습니다 : PorterDuff | 기계적 인조 인간
모질라 사이트의 컴 포지 팅을 살펴 보는 것이 좋습니다 . (그들은 안드로이드가하는 모든 모드를 가지고 있지는 않지만 많은 모드를 가지고 있습니다)
통사론
"your image name".setColorFilter("your context".getResources().getColor("color name"));
예
myImage.setColorFilter(mContext.getResources().getColor(R.color.deep_blue_new));
이것이 내가 한 일입니다.
public static Drawable changeDrawableColor(int drawableRes, int colorRes, Context context) {
//Convert drawable res to bitmap
final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawableRes);
final Bitmap resultBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth() - 1, bitmap.getHeight() - 1);
final Paint p = new Paint();
final Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, p);
//Create new drawable based on bitmap
final Drawable drawable = new BitmapDrawable(context.getResources(), resultBitmap);
drawable.setColorFilter(new
PorterDuffColorFilter(context.getResources().getColor(colorRes), PorterDuff.Mode.MULTIPLY));
return drawable;
}
다음과 같은 메소드를 작성하십시오.
//CHANGE ICON COLOR
private void changeIconColor(Context context ,int drawable){
Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, drawable);
assert unwrappedDrawable != null;
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, getResources().getColor(R.color.colorAccent));
}
그것을처럼 사용하십시오 :
changeIconColor(this,R.drawable.ic_home);
가장 쉬운 방법 :
imageView.setColorFilter(Color.rgb(r, g b));
또는
imageView.setColorFilter(Color.argb(a, r, g, b));
a, r, g, b : 컬러 argb 값.