1

I try to integrate in my android apps nine patch capabilities to make stretch looks better. It seems that nine-patch files are basic png files with 1 pixel border. However, when is try to build nine-patch file by myself, both android and eclipse don't reconized files as a drawable. You can see at the end of the post the script I use for DIY nine-patch using ImageMagick.

If I use DIY ninepatch as is, it doesn't work. If I open it using draw9patch and just save it and quit, it works ! Is there any specific needed to make DIY nine-patch ?

I hope you can help me !

Thanks a lot.

# make a rounded square background with 1 px border
convert -size 100x100 xc:lightblue ninepatch.9.png
convert ninepatch.9.png -format 'roundrectangle 1,1 %[fx:w],%[fx:h] 15,15' info: > tmp.mvg
convert ninepatch.9.png -border 1 -alpha transparent -background none -fill white -stroke none -strokewidth 0 -draw "@tmp.mvg" tmp.png
convert ninepatch.9.png -matte -bordercolor none -border 1 tmp.png -compose DstIn -composite ninepatch.9.png

# draw ninepatch info
convert ninepatch.9.png -format 'stroke-dasharray 1 1 line %[fx:w*0.25],0 %[fx:w*0.75],0' info: > tmp.mvg
convert ninepatch.9.png -format 'line 0,%[fx:h*0.25] 0,%[fx:h*0.75]' info: >> tmp.mvg
convert ninepatch.9.png -format 'fill black line %[fx:w*0.25],%[fx:h-1] %[fx:w*0.75],%[fx:h-1]' info: >> tmp.mvg
convert ninepatch.9.png -format 'line %[fx:w-1],%[fx:h*0.25] %[fx:w-1],%[fx:h*0.75]' info: >> tmp.mvg
convert ninepatch.9.png -fill none -stroke black -strokewidth 0.5 -draw "@tmp.mvg" ninepatch.9.png

Note the difference in size between these two files :

  • nine-patch with this method

http://dl.dropbox.com/u/16984035/ninepatch.9.png

  • the same nine-patch, opened and saved using draw9patch

http://dl.dropbox.com/u/16984035/ninepatch-draw9patch.9.png

(Sorry I can't post image yet !)

2
  • 1
    Please perform a project clean in Eclipse and then post the errors the show up in console. Also post the final 9png so we can see what has actually been drawn.
    – Theo
    Mar 29, 2012 at 17:35
  • Post the 9-patch image before & after saving in draw9patch. Mar 29, 2012 at 18:26

1 Answer 1

1

The first nine patch you linked is invalid. Every pixel in the one pixel border needs to be either completely transparent or completely black. When I zoom in on the bottom border, there is a gray pixel between the transparent ones to the left and the black ones to the right. That is not valid.

When you generated the border using ImageMagick, maybe the position of the black in the border ended up being a half pixel like 25.5, so ImageMagick colored the pixel gray. It needs to be either transparent or full black, however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.