two kinds 9patch:
There are two types of ninepatch file formats in the Android world ("source" and "compiled"). The source version is where you add the 1px transparency border everywhere-- when you compile your app into a .apk later, aapt will convert your *.9.png files to the binary format that Android expects. This is where the png file gets its "chunk" metadata. (read more)
client code:
InputStream stream = .. //whatever
Bitmap bitmap = BitmapFactory.decodeStream(stream);
byte[] chunk = bitmap.getNinePatchChunk(); //读chunk[]
boolean result = NinePatch.isNinePatchChunk(chunk); //判断是否是编译后的9Patch
NinePatchDrawable patchy = new NinePatchDrawable(bitmap, chunk, new Rect(), //由流创建9Patch图片
Server-side:
you need to prepare your images. You can use the Android Binary Resource Compiler. This automates some of the pain away from creating a new Android project just to compile some *.9.png files into the Android native format. If you were to do this manually, you would essentially make a project and throw in some *.9.png files ("source" files), compile everything into the .apk format, unzip the .apk file, then find the *.9.png file, and that's the one you send to your clients.
tips:
相关源码在android源码的aapt目录下
If you really, really need to construct your own chunk byte array, I would start by looking atdo_9patch
, isNinePatchChunk
, Res_png_9patch
and Res_png_9patch::serialize()
in ResourceTypes.cpp. There's also a home-made npTc chunk reader from Dmitry Skiba. I can't post links, so if someone can edit my answer that would be cool.