Android Drawables – Tutorial

Android Drawables. This tutorial describes the usage of Drawables in Android.

by

Android Drawables. This tutorial describes the usage of Drawables in Android.

 

1. What are Drawables?

A Drawable resource is a general concept for a graphic which can be drawn. The simplest case is a graphical file (bitmap), which would be represented in Android via a BitmapDrawable class.

Every Drawable is stored as individual files in one of the res/drawable folders. Typically you would store bitmaps for different resolutions in the -mdpi, -hdpi, -xhdpi, -xxhdpi subfolders of res/drawable. The ADT project creation wizard creates these folders by default. If these bitmaps are provided in different folder, the Android system selects the correct one automatically based on the device configuration.

If you do not provide bitmaps for all supported resolutions, the Android system scales the closest fit up or down. This is typically undesired as the bitmap might get blury.

In addition to graphical files, Android supports XML drawables and 9-patch graphics. XML drawables are used to describe shapes (color, border, gradient), state, transitions and more.

9-patch graphics are used to define which part of a graphic should be stretched if the view which uses this graphic is larger than the graphic.

Drawables can also be written in Java code. Every object which implements Drawable can be used as a Drawable in code.

2. Using drawables for views

Drawables are referred to in XML via @drawable/filename whereby filename filename is the filename without the file extension. For example to access the res/drawable/hello.png Drawable, you would use @drawable/hello as demonstrated in the following snippet.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/hello"
    android:text="@string/hello_world" />

In code you can also assign drawables to views. Most views accept an resource ID as input parameter. For example the following code shows how to set a drawables as background to an ImageView.

ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageResource(R.drawable.hello);

3. Loading Bitmaps and Drawables

Android allows you to use the Bitmap class for working with bitmaps. This section explain how to create Bitmap objects via Java code and how to convert Bitmap into Drawable objects and vice versa.

If required you can load any accessible bitmap file in your code and convert them into Drawables objects.

The following example code shows how to create an Bitmap object for the assets folder and assign it to an ImageView.

                AssetManager manager = getAssets();

                // read a Bitmap from Assets
                InputStream open = null;
                try {
                        open = manager.open("logo.png");
                        Bitmap bitmap = BitmapFactory.decodeStream(open);
                        // Assign the bitmap to an ImageView in this layout
                        ImageView view = (ImageView) findViewById(R.id.imageView1);
                        view.setImageBitmap(bitmap);
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        if (open != null) {
                                try {
                                        open.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                }

You can also access the Drawables from your res/drawable folder as Bitmap objects in your source code. The following code demonstrates that.

Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_search);

You can create a scale bitmap based on a new weight and height definition in pixel.

Bitmap originalBitmap = <initial setup>;

Bitmap resizedBitmap =
   Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, false);

You may also like

Development

Android Widgets – Tutorial

The following description assume that you already have experience in building standard Android application. Please see http://www.vogella.com/tutorials/Android/article.html – Android Tutorial. It also partly uses Android services. You find an introduction into Android Services in http://www.vogella.com/tutorials/AndroidServices/article.html – Android Service Tutorial.