How to change color of vector drawable path on button click

后端 未结 9 1784
北荒
北荒 2020-12-01 00:33

With the new android support update, vector drawables get backward compatibility. I have a vector image with various paths. I want the color of the paths to change on click

9条回答
  •  一向
    一向 (楼主)
    2020-12-01 01:15

    The color of the whole vector can be changed using setTint.

    You have to set up your ImageView in your layout file as this:

    
    

    Then to change the color of your image:

    DrawableCompat.setTint(myImageView.getDrawable(), ContextCompat.getColor(context, R.color.another_nice_color));
    

    Note: myImageView.getDrawable() gives nullpointerexception if the vector drawable is set to the imageView as background.

提交回复
热议问题