Working with Images

The most common drawable resources used in applications are bitmap-style image files, such as PNG and JPG files. These files are often used as application icons and button graphics but may be used for a number of user interface components.

As shown in Table 4.4, Android supports many common image formats.

TABLE 4.4 Image Formats Supported in Android

Supported Image Description Required

Format Extension

Portable Network Graphics Preferred format .png

Nine-Patch Stretchable Images Preferred format .9.png

TABLE 4.4 Continued

Supported Image Format


Required Extension

Joint Photographic Experts Group (JPEG/JPG)

Graphics Interchange Format (GIF)

Acceptable format .jpg (lossy)

Discouraged but .gif supported

Using Image Resources Programmatically

Images resources are encapsulated in the class BitmapDrawable. To access a graphic resource file called /res/drawable/logo.png, you would use the getDrawable() method, as follows:

BitmapDrawable logoBitmap =


Most of the time, however, you don't need to load a graphic directly. Instead, you can use the resource identifier as an attribute on a control such as an ImageView control. The following code, for example, sets and loads the logo.png graphic into an ImageView control named LogolmageView, which must be defined within the layout:

ImageView logoView = (ImageView)findViewByld(; logoView.setlmageResource(R.drawable.iogo);

Was this article helpful?

0 0

Post a comment