What are drawable in applications and how can they be designed?
6 minute(s) read
Published on: Apr 28, 2021
Updated on: Dec 14, 2021
Bitmaps used to specify the resolution of different types of images can be stored under the -mdpi, -hdpi, -xhdpi, -xxhdpi folders, whose parent folder is also res / drawable. Each Drawable is stored as a separate file inside the res / drawable folders. In this tutorial, we will talk about what Drawables are used for and how they can be used in Android.
To access and use Drawables, we can act as a @drawable / filename. In this case, instead of a filename, we must write the name of the file we want to access. It should be noted that the file name must be entered without an extension. If the file we want to access has an extension, the extension must be removed. For example, suppose we wish to access the res / drawable / test.png file, in which case we have to do this @ drawable / test and delete the last extension, which is png. Like the:
It is also worth mentioning that by using codes and through coding, Drawable can be assigned and connected to Views. Because all views accept the resource ID in front of each input parameter. Below is the code that can show how to draw Drawables as a background to ImageView:
ImageView = (ImageView) findViewById (R.id. image);
This code specifies how to connect the Drawable as a background or ImageView.
How to load Bitmap
Android offers the Bitmap class to be able to work with bitmaps. So it can be said that the Bitmap class was created to use bitmaps. In this part of the tutorial, we want to teach you how to convert Bitmap objects to Drawable or vice versa.
Note that you can even load bitmap files and convert them to Drawable objects.
Below is a code that shows how to create a Bitmap object in the assets folder and assign it to ImageView. The code below can create the Bitmap object inside the assets folder and assign it to ImageView.
Another way is to access Drawables through the res / drawable folder. In fact, in this method of accessing Drawables, they can be used as Bitmaps within the source code. The code below can clearly show this process:
To be able to convert the Bitmap object to Drawable, you can use the following code:
The code below can be used to convert Bitmap objects to Drawable. So when you want to do this and convert Bitmap objects to Drawable, you can use the code listed above.
What is Shape Drawable?
Shape Drawables are XML files that allow programmers and developers to use geometric objects, colors, gradients, and other elements to draw a shape and place it in Views. The advantage of using drawable is that it can be automatically adjusted to the defined size. Below is a code that is an example of a drawable:
It should be noted that you have this possibility, and you can assign the desired drawable to the background property of your page or layout. In other words, drawables can be placed on pages as background property.
What is State Drawable?
State Drawable can help the programmer by describing the situation. This way can explain the desired situation to the programmer and inform him of these situations. Note that each position has a separate drawable inside the View. Below is the code that can define the unique drawable button according to the situation:
You can easily activate the Transition effect and use it by entering this code.
The code is inserted to show Vector drawables, which can calculate the scale of drawables based on the device's density.
It should also be noted that higher versions of Android, such as version 5.0, also have the AnimatedVectorDrawable feature, which allows you to combine vector drawable with animations.
As explained above, we can also design Animation Drawables and import Animations into Vector drawables. In this case, using the setBa methodckgroundResource (), We can call the designed Animation Drawables put them in the View, and assign them.
The code listed above designs an Animation Drawables, calls it using the setBackgroundResource () method, and finally assigns it to the Views.
Patch Drawables are bitmaps that can be used to measure images or otherwise can be considered to have some stretchable areas, in which case it can be easily resized, small Kurd, or magnified. If the size and scale of the image are not equal to the size and scale of the Drawable, you can change the size of the image to the Drawable size using the stretchable areas from the top and left.
The question may be asked why it can only be resized, enlarged, or reduced from the top left? The answer is that the other parts, the bottom, and the right, can also be used to write and insert text. This is the case where you want to write text inside the Drawable and display it in the View.
A set of programming tools that can use the ADT process in Android are located in the android-SDK / tools installation folder.
How to create a custom Drawable?
In addition to Drawables, custom Drawables can also be used. Drawables that are proprietary can be used with Canvas and customized to suit your needs.
1- Create a project based on the Empty Activity template.
2- Project names have been selected in this tutorial com. vogella. android. drawables. custom.
3- Then, create the Drawable custom class.
4- Now, to be able to use the class you created above, you need to configure your layout file as below
If you want to use the class you created above, you have to make the layout settings below. In this case, you need to make sure that the settings you made in the layout section are as follows or not.
Set the MainActivity class.
To use the code listed below, you must create a file called dog.png inside the drawable folder and put the code listed below inside the file.
In this part of the tutorial, you will learn what drawables are and can be used for. Did you know that drawables for images and their scales can be very effective and used to adjust them?About our Android and iOS app development services