Button and Image Borders in Android with Nine Patch Files

Source: Internet
Author: User

When designing a User Interface your may want-to-change the default View backgrounds-give an App it own look. In most cases the backgrounds must is able to scale correctly for different size screens on a variety of devices. Android uses Nine Patch files to provide support for scaling of backgrounds as View sizes change.

The word Text has a background a rounded rectangle (a black border with a grey background ). The rectangle have then been uniformly scaled to fit in longer Text. As a result of the scaling the corners and vertical edges has distorted to give the rounded rectangle a unbalanced look. Compare that to the second longer Text where the background have maintained its balance.

To correctly scale the background selected parts of the image is scaled in a particular direction or not scaled at all. Which parts is scaled and in which direction is shown in this diagram. The Xindicates that corners is not scaled, the vertical edges is scaled vertically, the horizontal edges is SC Aled horizontally and the central area are scaled in both directions. This is probably what it is called a Nine Patch file, a.k.a 9patch .

    • 4 Corners +
    • 2 Vertical Edges +
    • 2 Horizontal Edges +
    • 1 Central Area
    • = 9 areas (patches) in total

In the example project this follows the default black border and grey gradient background of anEditTextis replaced with a solid turquoise background with black border. The required rounded rectangle is drawn in a graphics program (such as gimp,http://www.gimp.org, or Paint.NET, http://www. getpaint.net/). The rectangle is drawn as small as possible (here almost resembling a circle) to support small views. There is a one pixel border and transparent background. An orange version of the rectangle are drawn to support focus indication used with keypad navigation. Android needs to know which proportion of the vertical and horizontal edges need to be scaled, and where the View content sits in relation to the background. These factors is determined from indicators drawn within the image. To apply these indicators theDraw9patchProgram supplied in the Android SDK Tools folder is used. Start the program and open the background image (drag and drop onto theDraw9patchdialog). The program would expand the image by one pixel all around. It's on the extra one pixel edging that indicator lines is drawn. Enlarge the image using theZoomSlider. In the left hand and top edges draw the indicator lines to mark which of the vertical and horizontal pixels can be duplica Ted for scaling. In the right hand and bottom edges draw the indicator lines to show where content can be positioned.

The following diagram shows the right and bottom markers for content placement. If content does not fit in the indicated rectangle then the background image was stretched using the area shown by the and top markers. Save the marked file in theres/drawablefolder for a project. Android determines if an image is scaled using Nine Patch scaling instead of uniform scaling via the file name, it must ha Ve. 9Before the. PNGFile extension. For example a image file namedTurquoise.pngwould be namedTurquoise.9.png. To use the background image reference it using thebackgroundattribute of aViewIn a layout file, for exampleandroid:background= "@drawable/turquoise". If using another image to indicate aViewHave focus use a selector file, for example save this XML file in thedrawableFolder asSelector.xml(See Copying Code from the articles for tips):

Reference it as android:background= "@drawable/selector" (see the tutorial Adding State Graphics to an ImageButton With Inkscape for another selector file example). Edittexts with default backgrounds and the nine patch background create above is shown. Notice that the new View background was using a little less space than the default (useful to know if a project needs a lit Tle bit more screen area).

Nine Patch files is not restricted to simple View backgrounds. This Nine Patch file was used to frame a photograph. Notice how the left and top scaling indicators is broken where detail that must isn't be scaled (because it would distort) is located.

Download Some code that covers this article the ready for importing to an Android project. The code can also be accessed via the Android Example projectspage. See the article Move Android Code between PCs Running Eclipse on how to import example Code into an Eclipse project. A version of this article is produced for the Android Cookbook.

The pink heart frame used in this article came from the Open Clip Art Library athttp://openclipart.org/detail/91075/pink-2 -frame-by-inky2010.

From:http://tekeye.biz/2012/android-9patch-files

Button and Image Borders in Android with Nine Patch Files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.