PocketMagic

PocketMagic

Where Technology meets magic


Android
45 Posts
BlackBerry
3 Posts
Electronics
68 Posts
Hardware
120 Posts
High Voltage
49 Posts
iPhone
4 Posts
Linux
2 Posts
Nuclear
20 Posts
Optics
11 Posts
Photography
7 Posts
Photoshop
3 Posts
Research
19 Posts
Reviews
18 Posts
Robotics
8 Posts
Security
7 Posts
Software
72 Posts
Symbian
2 Posts
Tubes
18 Posts
Windows
3 Posts
Windows Mobile
11 Posts

Top Articles!       See PocketMagic on Facebook


uRADMonitor - Online Radiation monitoring station | 14742 Views | Rate 70.54
uRADMonitor - Online Radiation monitoring station
NMEA GPS Library for AVR Microcontrollers | 4736 Views | Rate 57.76
NMEA GPS Library for AVR Microcontrollers
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 17520 Views | Rate 57.25
Bluetooth and iOS - Use Bluetooth in your iPhone apps
Repairing a Victoreen CDV-700 6B Dosimeter  | 105 Views | Rate 52.5
Repairing a Victoreen CDV-700 6B Dosimeter
Building a robot – Part 2 | 4478 Views | Rate 44.78
Building a robot – Part 2
Programmatically Injecting Events on Android - Part 2 | 4788 Views | Rate 44.33
Programmatically Injecting Events on Android - Part 2
Simple Switched power Supplies | 15710 Views | Rate 41.13
Simple Switched power Supplies
CDV-742 Dosimeter and CDV-750 Dosimeter Charger | 73 Views | Rate 36.5
CDV-742 Dosimeter and CDV-750 Dosimeter Charger

 
  

Android 9 Patch images for custom controls


By Radu Motisan Posted on March 19th, 2011 , 1060 Views (Rate 1.34)



  




Given the high flexibility of interface controls in Android OS, where textviews can stretch to fit the screen or fit a given small area, designing a background for them can be problematic: a given image can fit the initial coordinates, but if the screen changes from portrait to landscape and the target control stretches, the aesthetics would be affected.
As a solution, we can design special images, called 9 patch images, that come with predefined "Stretching" areas that help keeping the same look. For an example, see the image to the left, where we have pairs of textviews+editviews in an attempt to create a labeled editview. The first pair is the default controls. Designing a background for them helps, but looks bad when the background image gets stretched, as for the pair number two. The last pair uses the same background, but made as a 9 patch image, and the appearance is as expected.

The 9 patch image is easy to make. First create your image as usual, it can have transparent areas as well. When ready, enlarge the image with 2 pixels on width and two on height, so you get a perimeter border of 1 pixel size. Use black and white to define areas that should not be stretched (white) and stretchable areas (black):

See the pictures above, they represent the editview background. You can see the corners are marked with white and the rest with black, so when stretching the corners will keep their aspect while the rectangular areas will stretch unaffected, and the aspect will be perfect . The second picture uses blue and red to highlight the stretchable area (red) and the non-stretchable area (blue).

Please note, that when saving a 9 patch file, you'll need to place it in the resource folder and name it like this: filename.9.png . When using it you will refer to it as filename.

See this sample for a better understanding:
Android9Patch






  

More on PocketMagic:

Tube: USN-5J29 | 281 Views | Rate 28.1
Tube: USN-5J29
Programmatically Injecting Events on Android - Part 1 | 9886 Views | Rate 25.74
Programmatically Injecting Events on Android - Part 1
Tube: 1B40 | 226 Views | Rate 25.11
Tube: 1B40
Atmega8 and enc28J60 for ethernet support | 7052 Views | Rate 24.15
Atmega8 and enc28J60 for ethernet support
How to set the AVR Fusebits | 1614 Views | Rate 23.74
How to set the AVR Fusebits
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164 | 1981 Views | Rate 23.03
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164

4 Responses to “Android 9 Patch images for custom controls”

  1. 1
    Richard:

    Great article.

    Here is a link to my blog that provides free 9 patch PNGs for your android projects:

    http://android9patch.blogspot.com

  2. 2
    Radu Motisan:

    Great job, Richard, thanks for providing those images. Hope you’ll find the time to make more.

  3. 3
    Richard:

    Radu,

    I have uploaded a few more packs, you will find the last one to be excellent IMO. I’m getting the hang of this slowly :)

    Enjoy!!!

  4. 4
    Radu:

    Looks good, keep up the good work.

Thank you for commenting. Your comment won't show until approved, which can take some time.

Please copy the string tly40S to the field below: