PocketMagic

PocketMagic

Where Technology meets magic


Android
45 Posts
BlackBerry
4 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
73 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 | 15030 Views | Rate 69.91
uRADMonitor - Online Radiation monitoring station
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 18612 Views | Rate 59.65
Bluetooth and iOS - Use Bluetooth in your iPhone apps
NMEA GPS Library for AVR Microcontrollers | 4908 Views | Rate 55.77
NMEA GPS Library for AVR Microcontrollers
Programmatically Injecting Events on Android - Part 2 | 5163 Views | Rate 44.9
Programmatically Injecting Events on Android - Part 2
Building a robot – Part 2 | 4744 Views | Rate 44.34
Building a robot – Part 2
Simple Switched power Supplies | 16387 Views | Rate 42.23
Simple Switched power Supplies
Capacitor Discharge Microspot Welder / Cutter | 11499 Views | Rate 36.62
Capacitor Discharge Microspot Welder / Cutter
Atmega8 and Nokia 5110 LCD  | 1607 Views | Rate 34.19
Atmega8 and Nokia 5110 LCD

 
  

Android 9 Patch images for custom controls


By Radu Motisan Posted on March 19th, 2011 , 1113 Views (Rate 1.4)



  




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:

How to set the AVR Fusebits | 1775 Views | Rate 23.67
How to set the AVR Fusebits
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164 | 2113 Views | Rate 22.72
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164
Dual H-Bridge for controlling two motors | 1272 Views | Rate 21.56
Dual H-Bridge for controlling two motors
USBAsp -  AVR USB Programmer  | 8226 Views | Rate 21.2
USBAsp - AVR USB Programmer
Repairing a Victoreen CDV-700 6B Dosimeter  | 184 Views | Rate 20.44
Repairing a Victoreen CDV-700 6B Dosimeter
Detecting an ultrasonic beacon | 926 Views | Rate 19.29
Detecting an ultrasonic beacon

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