In this tutorial you will work with scalable vector graphics (SVG) on Android to build a program that creates divisible images using baby-faced emojis that can be scaled and moved on a background image. graphics graphics is an extremely powerful image format that both reduces apk size and gives a more dynamic visual appearance that scales across the various screen densities in the Android ecosystem. You will learn about this format through standard SVG pathspec commands available that will help you understand the theory behind drawing them. You will also learn by using the corresponding APIs in Android.
In addition, you will learn how to use them on widgets and whether the support Android provides SVGs on the lower API level. Finally, you will unpack the tutorial by learning how to animate a vector drag to create a cool load animation on the app screen.
Download project materials using Download the materials button at the top or bottom of this article. Then, imports the project into Android Studio to get started.
Build and run the app. The first screen you will see is one where you can place baby-faced emojis on a background. Try saving one.
By clicking on History in the toolbar, you can see your creation.
Great! Time to continue.
What is a Vector Drawable
VectorDrawables is the SVG equivalent of Android. They are defined in XML using resource files, and can be dynamically changed by driving. The SVG format is an open standard image format defined by the World Wide Web Consortium (W3C) to create scalable, interactive and animated images through XML. Although the API on Android is not a one-to-one match with the SVG format provided by the web, it allows you to draw individual paths, groups of paths, and create cut masks to compose complex graphical assets for your applications. You can think of this process as drawing a picture on paper using a pen or pencil.
Why do you need this type of image format, but if Android supports the ability to operate different resources using density resources?
Well, to understand more, you can compare to the older format, raster images.
Vector vs. Raster Graphics
Raster image formats (.BMP, .TIF, .JPG, .GIF and .PNG) are what you probably have most experience with before. These are the grid of pixels that only have a fixed size that contains color information. You can imagine how limitation this is compared to the vector format describing the images as a series of dots and paths that compose shapes on any grid size, which can also be dyed dynamically. Raster images can also be colored, but the real difference comes when they are scaled up. They become pixelated as they grow larger and lose their sharpness compared to SVG. Look at the example below to see the difference in scaling.
Creating assets before vector pulls
In Android Studio you create assets before the vector features are required to work with designers to create a fixed-line baseline image, and then duplicate the image that scales it up for the different screen density buckets.
For example, to draw a partial icon of 24 × 24 density-independent pixels (dp), a designer would make a canvas on this pixel size and then draw the artwork in it. This would be the baseline principle for mdpi (160dpi) display devices. Then the designer had to create an extra canvas of 36 × 36 (1.5 x for hdpi), 48 × 48 (2 x for xhdpi), 72 × 72 (3x for xxhdpi) and 96 × 96 (4x for xxxhdpi) and scale or redraw the artwork to fit these limits.
However, this was only necessary for icons that Android Studio did not provide by default in its icon collection in Asset Studio (more on this a bit). As you can conclude, this process of delivering assets for each screen density will inflate your app and take up more space on the user's devices and increase the download time from the Play Store. This is not the best experience, but it cannot always be avoided with Vector drawables, as they are not silver balls.
When to Use Vector Drawables
Vector drawables can be used in different locations in your Android application where raster graphics are used as standard. For simple assets across the application like icons, they are the perfect solution to scalable rich images across different densities with minimal apk bloating. They can also be used to replace illustrations throughout your application. However, they cannot be used to replace things like images. As mentioned earlier, VectorDrawables uses XML to describe paths, shapes, and fillings for a resource that is inflated when driving, so complex shapes can take some time to render on the screen and score a result on your application if they have many trajectories to draw.
Enough theory and information already. How to create these
Creating a Vector Drawable by Hand
Creating a vector by hand is not all that is complicated for simple graphics, so that's what you should start with here. As mentioned earlier, paths are drawn using a number of commands from SVG pathspec as drawing with pencil on paper.
Consider creating the following asset:
<img src = "https://koenig-media.raywenderlich.com/uploads/2019/06/full_image_no_points-320×320.png" alt = "smiling face vector graphic You can achieve this through Android by specifying it as a vector feature.
Begin with create a file named ic_temp.xml in res ‣ drawable ] pastes the following XML into the file:
Here you have defined a vector bar with a virtual screen of 24 × 24 pixels and a physically active size of 24x24dp
Trails can be considered as the main component for creating a vector active.When drawing them, consider the pencil analog again, move your pencil to a point and lay it down on the paper and draw a line or bezier curve to another point that repeats this process and until you compose your artwork, this is exactly how Vectors are created only when you enter them using the different path commands. So what are the available commands that Android supports to inflate these vector images?
Now you must first add
android: pathData = "" attribute in XML.
Copy the following XML only during the opening
Here you have defined your first path with an empty
android: pathData value.
Before moving on, it is important to note that you will not see any paths without adding a stretch width and stroke or stroke, so take time to do the next.
Fill and stroke
As you can imagine, simply fill in the information about the color to be provided for the inner part of the path. This is added using the format
android: fillColor = "@ color / fill_color" or
android: fillColor = "# FF777777" in XML. Add the
fillColor attribute to your
path so it now looks like:
Here you have added a gray fill color to your path.
android XML attributes, you can give a stroke or outline to the vector graph, which styles its width and color, respectively:
Copy [strokeWidth="" and
] android: strokeColor = and paste following attributes lines inside the
path mark for
android: strokeWidth = "1" android: strokeColor = "# FF000000"
Here you have:
- Added a stretch width of 1 to your
- Added an impact color to the
Next you'll see how to actually draw a vector using the built-in functionality of Android that follows the scalable vector graphics path specification.
The available drawing commands that you want to learn next are:
- M : Move to.
- L : Line to.
- C : Bezier curve to or curve line.
- Z : Final line.
Move to (M)
The move to command is denoted by a capital M and means within the grid moving to an x, y coordinate.
pathData . It would now look like this:
Here you move the pencil to 2 points from the left edge (x-axis) and 12 points from the top edge (y-axis)
This is because the canvas starts at the top left at coordinate 0.0 so you move to the left or right decreases and increases the X position and moves down or down, or increases the Y position, respectively.
Note : The X and Y coordinates may contain decimal places for precise placement in the grid.
Line to (L)
The line to command is denoted by a capital L and means drawing a line from the previous point to this X, the Y coordinate. For example, in the same 24 × 24 grid above
L22,12 would draw a line completely over the canvas to 2 points from the right edge.
L22.12 L22.12 ] into the path data right after
pathData now looks like this:
android: pathData = "M2.12 L22,12"
Here you have drawn a horizontal line from 2.12 to 22.12 within the 24 × 24 grid.
Bezier Curve to (C)
Trails can also support curved lines using the curve of command that draws a bezier curve from the previous point to an end point using 2 control coordinates. A bezier curve is a smooth curve that can be created mathematically or from a variety of instructions. It is denoted by Cx1, y1 x2, y2 finalX, finalY where x1, y1 is the control point of the curve start, x2, y2 is the control point of the curve end and finalX, finally the point you want the curve to end is . The control points help to describe how the curve will bend. You can learn more here.
For example (again using 24 × 24 grid), you can write
C22.22 12.22 12.22 C12.22 1.22 2.13 and this would draw a curved line from the right edge back to the left edge creates a semicircular shape on the lower half of the grid. Place that command on your path. The full
pathData should now look like this:
android: pathData = "M2.12 L22.12 C22.22 12.22 12.22 C12.22 1.22 2.13"
If you break down this command, you can see that you draw 2 bezier curves:
C22,22 12,22 12,22– draws a curve from the previous point to 12,22 in the middle using of 22.22 as control point one and 12.22 as control point 2.
C12.22 1.22 2.13– draws a curve from the previous point to 2.13 using 12.22 as the control point one and 1.22 as the control point 2.
Finally, the step of completing a vector's path data is to close the path.
Closing a path (Z)
Finally, to close a path, use only the command
Z which closes the path draws a straight line from the current position to the starting point.
For example, to stop drawing at this point
Z to the end of
pathData so that it looks like this:
android: pathData = "M2.13 L22.13 C22.22 12.22 12.22 C12.22 1.22 2.13Z"
Now you have made the path data for the semicircle on the lower part of the screen. Note that the red dots along the bottom line of this image are the control points of your curves.
This is pretty cool, but what if you wanted to add another path or combine two paths so you can do something about them? This is where the groups come into play.
Groups are just that, groups of paths. They can be used to combine paths in the same layer that allow you to keep track and do other things like rotating, scaling, translating or animating multiple components simultaneously.
You will do this a little. For now, consider the following XML group that builds on the previous example and draws a couple of square eyes in a group.
Copy the following code and paste it over the previously defined ] path code :