In this tutorial, we’ll be discussing Android Vector Drawable. Furthermore, we’ll be implementing them in our Android Application.
Android Vector Drawable
Often we use PNG as our drawable images. In order for the PNG images to work for different screen sizes, we create multiple PNG assets with different sizes and densities. Subsequently, PNG images take up extra space and lead to large APK sizes of the Android Apps.
This is where Vector Drawable comes to our rescue! They are your replacement for PNG images.
A VectorDrawable is a vector graphic defined in an XML file as a set of points, lines, and curves along with its associated color information.
They can be scaled according to the screen size without loss in quality. They are rendered quickly onto the screen too. VectorDrawable are an XML file.
You can add a new Vector Asset in your drawable folder using New | Vector Asset.
Thus we can create Vector drawables of Material Design icons. The code for the VectorDrawable looks like this:
They are set in the vector
tag. android:viewportWidth
and android:viewportHeight
are used in setting the width and height of the drawable bounds. Within these dimensions, the vector drawable is drawn on the canvas.
path
is the tag that creates the drawable. Inside the path we create lines, curves, arcs and set the border, background color. We do so path commands in the pathData
.
Vector Drawables were introduced since Android Lollipop and higher but thanks to backward compatibility, they are compatible with earlier versions too.
Creating Path for Vector Assets
The path commands consist of an alphabet followed by coordinates. Imagine creating paths as doing a painting. Uppercase alphabets represent absolute position. Lowercase represent relative position.
- M – moveto command. It’s a way of saying move your pencil to the given coordinate on the view. Example M 100 100 moves an imaginary pencil to the 100, 100 coordinate on the canvas.
- L – line to command. Is used to draw a line from the current position to the position specified.
Example :M 100 100, L 120 100
draws a horizontal line. z
– This is used to close a path. It draws a line from the last position to the first position. ExampleM 100 100 L 120 100 L 120 120 L 100 120 Z
creates a square.C
– This is used to create a bezier curve. We need to specify three sets of coordinates after this. The first and second would the two control points between the initial point and the end point.A
– is used to draw an arc. After a you need to specify the following format: (rx ry x-axis-rotation large-arc-flag sweep-flag x y). rx ry and x axis are the two radii specified.
We can specify more than one paths in a vector by setting the path tags inside a group
tag.
Let’s create some random interesting Vector Drawable in our Android Application using the above knowledge.
Android Vector Drawable Example Project Structure
Android Vector Drawable Code
The code for the activity_main.xml layout is given below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/ic_rectangle" android:gravity="center" android:orientation="vertical"> <ImageView android:layout_width="32dp" android:layout_height="32dp" android:src="@drawable/ic_w" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" android:src="@drawable/ic_w_fill" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:layout_width="16dp" android:layout_gravity="center_vertical" android:layout_height="16dp" android:src="@drawable/ic_a" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" android:src="@drawable/ic_c" /> </LinearLayout> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" android:src="@drawable/ic_0" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" android:src="@drawable/ic_jd" /> </LinearLayout> |
Let’s look at each of the Vector Drawables implementation one a time.
ic_rectangle.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <vector xmlns:android="https://schemas.android.com/apk/res/android" android:width="100dp" android:height="100dp" android:viewportHeight="100" android:viewportWidth="100"> <path android:name="light_triangle" android:fillColor="@color/colorPrimary" android:pathData="M 100,0 L 0,100 100,100 z" /> <path android:name="dark_triangle" android:fillColor="@color/colorPrimaryDark" android:pathData="M 0,0 L 100,0 0,100 z" /> </vector> |
In the above code, we’ve created two paths. Each a right-angled triangle.
Setting this on our LinearLayout would make the background look like:
ic_w.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0" encoding="utf-8"?> <vector xmlns:android="https://schemas.android.com/apk/res/android" android:width="50dp" android:height="50dp" android:viewportHeight="50.0" android:viewportWidth="50.0"> <group android:name="w"> <path android:name="one" android:pathData="M 25 0 L 10 40" android:strokeColor="#FFF" android:strokeWidth="1" /> <path android:name="two" android:pathData="M 25 0 L 40 40" android:strokeColor="#FFF" android:strokeWidth="1" /> <path android:name="three" android:pathData="M 10 40 L 0 0 " android:strokeColor="#FFF" android:strokeWidth="1" /> <path android:name="four" android:pathData="M 40 40 L 50 0" android:strokeColor="#FFF" android:strokeWidth="1" /> </group> </vector> |
In the above code, we’re trying to create a W symbol using lines.
Note: Instead of a separate path, we can merge all in a single path command. Though you can use multiple paths for getting a clear understanding of what each path does.
1 2 |
Finally, our application’s output looks like this:
This brings an end to this tutorial. You can download the final Android VectorDrawable project from the link below: