Simple Android library which allows you to create a chunk for NinePatchDrawable at runtime. So you are able to load 9.png images, for example, from assets of your application or from other source.
This is a simple Android library which allows you to create a chunk for NinePatchDrawable at runtime. So you are able to load 9.png images, for example, from assets of your application or from other source.
The solution based on this implementation from Android Open Source Project.
Additional information about 9-patch chunk structure you can find here.
The main class of the library is NinePatchChunk.
public static final int NOCOLOR = 0x00000001; - The 9 patch segment is not a solid color.
public static final int TRANSPARENTCOLOR = 0x00000000; - The 9 patch segment is completely transparent.
Firstly let's go through the fields of the class object you would use.
boolean wasSerialized- The indicator if this chunk was serialized or not. I didn't saw images without this variable equals true. If you will - feel free to contact me. This value is
ArrayList<Div> xDivs- An array with stretchable areas goes on X axes. The 0 start pixel is pixel of actual bitmap without 9-patch 1-pixel border. The
Divclass will be described bellow. By default this value is null
ArrayList<Div> yDivs- The same as xDivs but for the Y axes.
Rect padding- The paddings from the ends of bitmap which you set while creating regular 9-patch image by defining right and bottom borders in Android 9-patch editor.
int colors- The colors of arrays which represents areas crossing X and Y stretchable and unstretchable areas of 9-patch bitmap. For more information look on
struct Respng9patchcommentaries here.
Color.TRANSPARENT. If the area is filled with different colors - the array should content
NO_COLORvalue. See static methods description to see how easily properly create this array.
The class has only one object-dependent method:
byte toBytes()- serializes your current chunk instance to a byte array so you can use it creating NinePatchDrawable object.
Also NinePatchChunk class has some static methods. Let's go through them.
NinePatchChunk parse(byte data) - Parses any serialized chunk into the object you can use or edit.
NinePatchDrawable create9PatchDrawable(Resources resources, Bitmap bitmap, String srcName) - Creates NinePatchDrawable right from raw Bitmap object. So resulting drawable will have width and height 2 pixels less if it is raw, not compiled 9-patch resource.
NinePatchChunk createEmptyChunk() - Jut creates empty chunk object to you have ability to change it.
int createColorsArray(NinePatchChunk chunk, int bitmapWidth, int bitmapHeight) - Creates a proper color array sized according to your X and Y divs.
void createColorsArrayAndSet(NinePatchChunk chunk, int bitmapWidth, int bitmapHeight) - The same as previous but this method also sets created array to your chunk if it is not null.
boolean isRawNinePatchBitmap(Bitmap bitmap) - This method checks if the bitmap is 9-patch image.
And finally, Div object description. This object is used to define stretchable areas:
int start- the starting pixel of stretchable area. The count starts from 0.
int stop- the ending pixel of stretchable area. This pixel is right after the latest Color.BLACK pixel of the stretchable area.
NinePatchChunk is a library which delivers as open source project.
As build tool it uses Gradle build system.
In order to build the library you need this environment installed: - Java JDK 6 - Android SDK with build tools version 18.1.1
For success build this environment variables should be defined in your system: - JAVAHOME - will point to your JDK location - ANDROIDHOME - will point to Android SDK location
For the next step go to command line and then go to location where NinePatchChunk library sources are stored
cd /NinePatchChunk (where gradlew script file stored)
From this point in order to get output files you just need to run this command from command line:
gradlew assemble to build release and debug aar files
gradlew assembleDebug to build debug aar file only
gradlew assembleRelease to build release aar file only
That's it. So now you can find the output files inside this folder:
This is it. The using is simple as that. See the java doc to learn more.
If you have some additional question - feel free to contact me.