Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Vector Graphics in Android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 589
    Comment on it

    Hello Friends,
    In this blog we will learn about VectorDrawables and change icon color on Runtime.

    From Android Studio 1.4 Google release new feature to support Design library.

    VectorDrawable has introduced in API 21(has support for lower version also).
     

    What is benefits  for Vector drawable  in  ?  

    •  Vector graphics helps us to reduce the size of the drawable which are density dependent. We just  have to create one SVG file for drawable.
    • We can change the color of Vector Image at Runtime. So no need to add 1 drawable with multiple colors.
    • Reduced designer work load.

    Add SVG into drawable.

    To add SVG into project below steps help you:-

    1. Right Click on app directory from Project window.
    2. Select New--> Vector asset . New window open like below image.

       
    3. Android Studio gives some Material design vector images to use or you can select your own SVG vector from Local SVG.
    4. You can override the size and Opacity of SVG icons from same window.
    5. Click Next and Finish.
    6. xml file will be created on drawable with same name. It will adjust with all Screen sizes.
    7. Now you can use as normal drawable.

     

    Change color on Runtime

    From API v21 Android Support VectorDrawable class so You can modify drawable properties from Java code.

    Here is code to change color of drawable.

     VectorDrawable drawable = (VectorDrawable) getResources().getDrawable(drawable, getTheme());
            drawable.setTint(getResources().getColor(color, getTheme()));
    
    

    now set this drawable to your View.

    NOTE:- Android support VectorDrawable from v21. and did not have officially support for lower version. But there is some 3rd Party library for Lower version support.
     

    Thanks,

    Happy Coding!!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: