chunk的字节个数不固定,因为我要下班回家了,先不写了。
/frameworks/base/include/androidfw/ResourceTypes.h
int8_t wasDeserialized; int8_t numXDivs; int8_t numYDivs; int8_t numColors; // These tell where the next section of a patch starts. // For example, the first patch includes the pixels from // 0 to xDivs[0]?1 and the second patch includes the pixels // from xDivs[0] to xDivs[1]?1. // Note: allocation/free of these pointers is left to the caller. int32_t* xDivs; int32_t* yDivs; int32_t paddingLeft, paddingRight; int32_t paddingTop, paddingBottom; enum { // The 9 patch segment is not a solid color. NO_COLOR = 0x00000001, // The 9 patch segment is completely transparent. TRANSPARENT_COLOR = 0x00000000 }; // Note: allocation/free of this pointer is left to the caller. uint32_t* colors; // Convert data from device representation to PNG file representation. void deviceToFile(); // Convert data from PNG file representation to device representation. void fileToDevice(); // Serialize/Marshall the patch data into a newly malloc?ed block void* serialize(); // Serialize/Marshall the patch data void serialize(void* outData); // Deserialize/Unmarshall the patch data static Res_png_9patch* deserialize(const void* data); // Compute the size of the serialized data structure size_t serializedSize();
public static NinePatch createFixedNinePatch(Resources res, Bitmap bitmap, int top, int left, int bottom, int right, String srcName){ ByteBuffer buffer = getByteBufferFixed(top, left, bottom, right); NinePatch patch = new NinePatch(bitmap, buffer.array(), srcName); return patch; } public static ByteBuffer getByteBufferFixed(int top, int left, int bottom, int right) { //Docs check the NinePatchChunkFile ByteBuffer buffer = ByteBuffer.allocate(84).order(ByteOrder.nativeOrder()); //was translated buffer.put((byte)0x01); //divx size buffer.put((byte)0x02); //divy size buffer.put((byte)0x02); //color size buffer.put(( byte)0x09); //skip buffer.putInt(0); buffer.putInt(0); //padding buffer.putInt(0); buffer.putInt(0); buffer.putInt(0); buffer.putInt(0); //skip 4 bytes buffer.putInt(0); buffer.putInt(left); buffer.putInt(right); buffer.putInt(top); buffer.putInt(bottom); buffer.putInt(NO_COLOR); buffer.putInt(NO_COLOR); buffer.putInt(NO_COLOR); buffer.putInt(NO_COLOR); buffer.putInt(NO_COLOR); buffer.putInt(NO_COLOR); buffer.putInt(NO_COLOR); buffer.putInt(NO_COLOR); buffer.putInt(NO_COLOR); return buffer; }
参考:
http://stackoverflow.com/questions/16509404/building-a-9-patch-drawable-at-runtime/16676419#16676419
http://www.itrunner.cn/?p=244
时间: 2024-10-11 13:26:10