Introduction

A Drawable Resource is a general concept of drawing to the screen for a graphic, you can use Android predefined graphics and retrieve them with APIs or create custom XML drawable. There are several different types of drawable:
Here I am explain the first two types of Drawable resources, Bitmap File and Nine-Patch File.
Bitmap File
This drawable resource simply wraps a bitmap file. Further in this, you can reference a bitmap file directly, using the filename as the resource ID that can support three types of bitmap files: PNG (.png), JPEG (.jpg) and GIF (.gif) files, or create an alias resource ID in XML where an XML bitmap is defined that points to a bitmap file.
Pass the reference of the resources:
While creating an alias resource ID in XML you can set any of the following attributes:
Let's take an example of both types of Bitmap Files.
Example1: To reference a bitmap file directly.
Step 1: Create a simple Android application and run it to see the default output of an Android application.
image1.jpg
Step 2: Create a new folder in the resources folder and import one image (more if you want to) in it.
image2.jpg
Step 3: Create a new ImageView in activity_main.xml file and give the resource a reference in the android:src Attribute
  1. <ImageView
  2. android:id="@+id/ImageView1"
  3. android:layout_height="50dp"
  4. android:layout_width="50dp"
  5. android:scaleType="fitXY"
  6. android:src="@drawable/image1"
  7. />
Step 4: Now run the application and see the output
image3.jpg
Example2: Creates an alias resource ID in XML.
Step 1: Create a new XML file in the drawable folder then create and define a bitmap and its required attributes.
image4.jpg
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:src="@drawable/image1"/>
Step 2: Now go to the activity_main.xml file and chnage the android:src Attribute value to the XML file name
  1. <ImageView
  2. android:id="@+id/ImageView1"
  3. android:layout_height="50dp"
  4. android:layout_width="50dp"
  5. android:scaleType="fitXY"
  6. android:src="@drawable/backround"
  7. />
Step 3: Now run the application and see the output. The output is the same as above but the way of using the Bitmap Drawable Resources is different.
image3.jpg
Nine-Patch File
NinePatch resources are simply another kind of Drawable subclass called NinePatchDrawable. Most of the time, you'll need only the resource ID of the image, to be set as an attribute of the view or layout. Here you can define stretchable regions that Android scales when content within the view exceeds the normal image bounds. Generally, the center patch is transparent or solid, with parts of the edges set to remain fixed, other edge parts set to stretch, and the corners set to remain fixed.
In the same way, as with a bitmap, you can reference a Nine-Patch file directly or from a resource defined by XML.
Example
Step 1: Import a .png image in the drawable folder
Step 2: Create a new button in the activity_main.xml file and provide the resource reference in the android:background Attribute
  1. <Button android:id="@+id/Button02"
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:background="@drawable/buttonimage"
  5. android:textColor="#ffcc00" android:padding="30dp"
  6. android:text="CLICK HERE"
  7. android:layout_gravity="center"></Button>
Step 3: Now run the application and see the output
image5.jpg
If you need to act on the resource programmatically, first you need to import the following namespace:
  1. import android.graphics.drawable.NinePatchDrawable;
Then call it by the following code:
  1. NinePatchDrawable myNinePatchDrawable = (NinePatchDrawable) getResources().getDrawable(R.drawable.[xml resource file]);
Thank you, that's all. I hope you get some idea of Bitmap and Nine-Patch Drawable Resources through this brief article.