Patch Nine Not at Compile Time


If you aren’t familiar patch nines are really cool png files that have defined stretch areas as well as padding areas. I had experimented before and found that it was possible to generate patch nines at runtime in android. I posted some sample code on stack overflow here.

I found a cool post about loading nine patches from the web. What I didn’t know, was that it was possible to use the apt tool to generate the nine patch by itself. Turns out that as long as you run the apt tool over the image you can host the resulting file elsewhere and just use the BitmapFactory and NinePatch related classes to load the file at runtime.

There are a few limits on how many stretchable areas that can be used, but this isn’t specific to the following method. Also make sure to never draw nine patches smaller than its min size, to avoid strange rendering. Note I changed the code sample a bit to get a NinePatchDrawable as opposed to a NinePatch as a Drawable is likely the goal.

Bitmap myBitmap = BitmapFactory.decode...// left up to you which way to read the file
NinePatch ninePatch = new NinePatch(mybitmap, mybitmap.getNinePatchChunk());
Drawable tada = new NinePatchDrawable(Res, ninePatch);

Original Source

Leave a Reply

Your email address will not be published. Required fields are marked *