Skip to content

Latest commit

 

History

History
76 lines (56 loc) · 5.61 KB

README.md

File metadata and controls

76 lines (56 loc) · 5.61 KB

NinePatchChunk

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.


Usage

The main class of the library is NinePatchChunk.
Global constants:
public static final int NO_COLOR = 0x00000001; - The 9 patch segment is not a solid color.
public static final int TRANSPARENT_COLOR = 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 true by default.
  • 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 Div class 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.
    Let's put some example. Let's say that you have 7x7 9-patch image. and you want the content will be exactly in the middle of the image.
    So your final image size without 9-patch specific borders would be 5x5 and if you want the contend will be placed inside 3-d pixel area you should define paddings as Rect(2,2,2,2)
  • 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 Res_png_9patch commentaries here.
    Long story short, if you have the same color in that area - the color for thar area will be that color. If color of that area is Transparent (has alpha 0) - the color should be Color.TRANSPARENT. If the area is filled with different colors - the array should content NO_COLOR value. 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.

Building from sources

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:

  • JAVA_HOME - will point to your JDK location
  • ANDROID_HOME - 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:
NinePatchChunk\NinePatchChunkLib\build\libs


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.

Thanks!