PocketMagic

PocketMagic

Where Technology meets magic


Android
47 Posts
BlackBerry
4 Posts
Electronics
69 Posts
Hardware
123 Posts
High Voltage
49 Posts
Image processing
2 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
76 Posts
Symbian
2 Posts
Tubes
20 Posts
Windows
3 Posts
Windows Mobile
11 Posts

Top Articles!       See PocketMagic on Facebook


Building a robot – Part 2 | 8852 Views | Rate 67.06
Building a robot – Part 2
uRADMonitor - Online Radiation monitoring station | 16098 Views | Rate 66.8
uRADMonitor - Online Radiation monitoring station
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 22049 Views | Rate 65.23
Bluetooth and iOS - Use Bluetooth in your iPhone apps
NMEA GPS Library for AVR Microcontrollers | 5815 Views | Rate 51.01
NMEA GPS Library for AVR Microcontrollers
Programmatically Injecting Events on Android - Part 2 | 6532 Views | Rate 46.66
Programmatically Injecting Events on Android - Part 2
Simple Switched power Supplies | 19042 Views | Rate 46
Simple Switched power Supplies
Capacitor Discharge Microspot Welder / Cutter | 13189 Views | Rate 38.91
Capacitor Discharge Microspot Welder / Cutter
Coil Winding machine counter with Atmega8 and Reed relay | 964 Views | Rate 38.56
Coil Winding machine counter with Atmega8 and Reed relay

 
  

Android 9 Patch images for custom controls


By Radu Motisan Posted on March 19th, 2011 , 1321 Views (Rate 1.61)



  
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:

Atmega8 and enc28J60 for ethernet support | 8830 Views | Rate 27.25
Atmega8 and enc28J60 for ethernet support
How to make metallic sodium | 1416 Views | Rate 26.72
How to make metallic sodium
Tube: USN-5J29 | 1079 Views | Rate 25.69
Tube: USN-5J29
How to set the AVR Fusebits | 2532 Views | Rate 25.32
How to set the AVR Fusebits
Tube: GP-5 (ГП-5) | 627 Views | Rate 25.08
Tube: GP-5 (ГП-5)
Dual H-Bridge for controlling two motors | 2095 Views | Rate 24.94
Dual H-Bridge for controlling two motors

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 dk8WNO to the field below: