Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1
For the More Curious: 9-Patch Images

In the solutions for this chapter (see the section called Adding an Icon in Chapter 2), you can find
this image along with a pressed version in the xxhdpi drawable folder. Copy these two images into
your project’s drawable-xxhdpi folder and apply them as your button background by modifying
button_beat_box.xml.


Listing 23.9  Applying the new button background images
(res/drawable/button_beat_box.xml)



android:state_pressed="true"/>

Run BeatBox, and you will see the new button background (Figure 23.8).


Figure 23.8  BeastBox


Whoa. That looks... bad.


Why does it look like that? Android is uniformly stretching ic_button_beat_box_default.png,
including the dog-eared edge and the rounded corners. It would look better if you could specify which
parts of the image to stretch and which parts not to stretch. This is where 9-patch images come in.


A 9-patch image file is specially formatted so that Android knows which portions can and cannot be
scaled. Done properly, this ensures that the edges and corners of your background remain consistent
with the image as it was created.

Free download pdf