Open Source Working with NinePatch Stretchable Graphics in Android

Working with NinePatch Stretchable Graphics in Android

Android devices come in many shapes and sizes, often making it very difficult for developers to design applications that look great on all the various screen sizes and orientations. For addressing this challenge, NinePatch graphics can be indispensable. In this article, we discuss what the NinePatch graphics format is, how it works, and when to use it.

What Is NinePatch?

Android supports NinePatch Stretchable Graphics for the controlled scaling of images. NinePatch graphics are simply PNG graphics that have patches, or regions of the image, defined to scale and stretch in the appropriate areas instead of scaling the entire image as one unit, which often obscures the details of the graphic. 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.

Figure 1 illustrates how an image (in this case, a square) is divided into nine patches.

When to Use NinePatch Graphics

Stretchable graphics can be used anywhere in your application where the size of the graphic may vary. They are especially useful for:

  • Button backgrounds (where the text strings are various sizes)
  • Frames (where the inner picture comes in various sizes and aspect ratios)
  • Any time you’re using a Drawable with at least one dimension set to wrap_content

 

Latest Posts

Related Stories