Android Selector Drawable with VectorDrawables srcCompat

后端 未结 5 1105
轻奢々
轻奢々 2020-12-04 08:50

I\'m facing a problem with the new backward compatibility with VectorDrawables. In the Support Library 23.2 was a new feature for backward compatibility with Android VectorD

5条回答
  •  醉梦人生
    2020-12-04 09:20

    After watching What's new in the support library - Google I/O 2016 I noticed one useful method in the AppCompatResources class. This is AppCompatResources#getColorStateList(Context context, int resId). With a help of this method I've implemented selector with vector drawables. Here is my color selector file icon_selector:

    
        
        
        
    
    

    And there is java method that returns tinted drawable:

    private Drawable getTintedDrawable(@DrawableRes int drawableId) {
        Drawable drawable;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            drawable = getResources().getDrawable(drawableId, getTheme());
        } else {
            drawable = getResources().getDrawable(drawableId);
        }
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(drawable.mutate(), AppCompatResources.getColorStateList(this, R.color.selector_nav_bar_item_ico));
        return drawable;
    }
    

    You can use it like shown below

    yourImageView.setImageDrawable(getTintedDrawable(R.drawable.ic_vector_image));
    

提交回复
热议问题