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.