How to change colors of a Drawable in Android?

前端 未结 21 1859
野性不改
野性不改 2020-11-22 13:50

I\'m working on an android application, and I have a drawable that I\'m loading up from a source image. On this image, I\'d like to convert all of the white pixels to a dif

相关标签:
21条回答
  • 2020-11-22 14:30

    I also use ImageView for icons (in ListView or settings screen). But I think there is much simpler way to do that.

    Use tint to change the color overlay on your selected icon.

    In xml,

    android:tint="@color/accent"
    android:src="@drawable/ic_event" 
    

    works fine since it comes from AppCompat

    0 讨论(0)
  • 2020-11-22 14:32

    Give this code a try:

    ImageView lineColorCode = (ImageView)convertView.findViewById(R.id.line_color_code);
    int color = Color.parseColor("#AE6118"); //The color u want             
    lineColorCode.setColorFilter(color);
    
    0 讨论(0)
  • 2020-11-22 14:33

    I know this question was ask way before Lollipop but I would like to add a nice way to do this on Android 5.+. You make an xml drawable that references the original one and set tint on it like such:

    <?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"/>
    
    0 讨论(0)
提交回复
热议问题