更新代码后它对我有用。我认为颜色大小出于某种原因使其不愉快(根据android源代码中的注释,每个补丁都有一个颜色提示,在这种情况下设置少于9个部分似乎会导致问题)。我还没有测试你的图像。
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;
}