Open In App

Android Vector Drawables

Last Updated : 31 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

With new, exciting mobile devices coming out every day with new screen sizes and resolutions, it can be difficult making sure that the used images look best on every new device that hits the stores. So what many developers end up doing is creating multiple versions of each image for different display resolutions. This means more time to create extra access, and more space is taken up by the APK and on the device. But with vector images, we only need to create an image once, as an XML file. And it can scale beautifully for all different DPIs for different devices. This not only saves space but also simplifies maintenance. We can also use vector images for animation. Instead of having multiple series of images for all different DPIs, now all we need is just several XML files.

Note: Vector Graphic support was added for the first time in Android 5.0, i.e. Lollipop. Prior to this release, if the user ever wanted to draw anything using vector representation, it had to be done manually in Java code, which was cumbersome.

A normal bitmap represents images as a set of pixels in a grid. But with vector graphics, we can represent an image by describing the object geometrically. This means we can describe an image as a set of points, lines, and curves, along with all the associated color information. 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. The major advantage of using a vector drawable is image scalability. It can be scaled without loss of display quality, which means the same file is resized for different screen densities without loss of image quality. This results in smaller APK files and less developer maintenance. You can also use vector images for animation by using multiple XML files instead of multiple images for each display resolution.

How to Create a VectorDrawable in Android Studio?

Step 1: Create a New Project in Android Studio

To create a new project in Android Project just refer to this article on How to Create New Project in Android Studio

Step 2: Create a new vector asset

Open Project files and select Android files. Navigate to the app > res > drawable. Right-click on the Drawable folder and select new Vector Asset.

Choose Vector Asset from here

 

A dialog box will pop up as shown below:

Select the asset type from the dialog box

 

Step 3: Setting vector attributes

Choose whether you desire to create a pre-designed vector or a custom vector by selecting from the given two options: Clip Art and Local File.

  • For creating a pre-designed vector, choose your desired logo from the already existing over 1000 clip art. Select appropriate pixel size, color, background, and other required properties.
  • For creating a custom vector art, choose your bitmap file from the local file and set all the required parameters as mentioned in step a. 

Click Next after setting the attributes. Then click Finish Your new vector file will get saved in the drawable folder.

Step 4: Using the created vector

Add the newly created vector to your android application in the form of ImageView, background, etc. by accessing the Drawable folder. 

Output:

Vector added in App layout

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads