VectorDrawable Backwards Compatibility And Installing Unofficial Support Libraries

后端 未结 4 1571
一向
一向 2021-02-10 03:54

Bear with me, I\'m new!
I want to use vectors in my android app, and I want my app to be backwards compatible. I found this support library that looks pretty cool!*
So

4条回答
  •  一生所求
    2021-02-10 04:23

    Here is a option that worked for me Use this library - https://github.com/wnafee/vector-compat (api 14+)

    android {
        // use version 22 or higher
        buildToolsVersion "22.0.1"
        ...
    }
    dependencies {
        compile 'com.wnafee:vector-compat:1.0.5'
        ...
    }
    

    And create a custom ImageView class that uses vector compat class -

    public class SvgImageView extends ImageView {        
        private Drawable icon;
    
    
        public SvgImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
            TypedArray ta = context.obtainStyledAttributes(attrs,
                    R.styleable.button_left, 0, 0);
    
            try {
                int resId = ta.getResourceId(R.styleable.button_left_b_icon, -1);            
                if (resId != -1) {
                    icon = ResourcesCompat.getDrawable(this.getContext(), resId);
    
                }
    
    
            } finally {
                ta.recycle();
            }
    
    
            if (icon != null) {
                setImage(icon);
            }
    
        }   
    
        public void setImage(Drawable icon) {
            SvgImageView.this.setImageDrawable(icon);
    
        }
    
    
    
    }
    

    Vector image example -

    
    
        
    
            
    
        
    
    

    Example -

    
    

提交回复
热议问题