/**
* Attempts to decode 9-patch data from a {@link Bitmap}.
* @param bitmap The {@link Bitmap} to check.
* @return An instance of {@link NinePatchData} representing the 9-patch information
* encoded in {@code bitmap} or {@code null} if the {@link Bitmap} wasn't a
* 9-patch.
*/
public static NinePatchData create(Bitmap bitmap) {
if (bitmap == null) return null;
try {
byte[] chunk = bitmap.getNinePatchChunk();
if (chunk == null || !NinePatch.isNinePatchChunk(chunk)) return null;
ByteBuffer buffer = ByteBuffer.wrap(chunk).order(ByteOrder.nativeOrder());
// int8_t wasDeserialized
if (buffer.get() == 0) return null;
// int8_t numXDivs
int numDivX = buffer.get();
if (numDivX == 0 || (numDivX & 0x01) != 0) return null;
// int8_t numYDivs
int numDivY = buffer.get();
if (numDivY == 0 || (numDivY & 0x01) != 0) return null;
// int8_t numColors
buffer.get();
// uint32_t xDivsOffset
buffer.getInt();
// uint32_t yDivsOffset
buffer.getInt();
Rect padding = new Rect();
// uint32_t paddingLeft
padding.left = buffer.getInt();
// uint32_t paddingRight
padding.right = buffer.getInt();
// uint32_t paddingTop
padding.top = buffer.getInt();
// uint32_t paddingBottom
padding.bottom = buffer.getInt();
// uint32_t colorsOffset
buffer.getInt();
// uint32_t uint32_t uint32_t ...
int[] divX = new int[numDivX];
for (int i = 0; i < numDivX; i++) divX[i] = buffer.getInt();
// uint32_t uint32_t uint32_t ...
int[] divY = new int[numDivY];
for (int i = 0; i < numDivY; i++) divY[i] = buffer.getInt();
return new NinePatchData(bitmap.getWidth(), bitmap.getHeight(), padding, divX, divY);
} catch (BufferUnderflowException ex) {
return null;
}
}
NinePatchData.java 文件源码
java
阅读 26
收藏 0
点赞 0
评论 0
项目:365browser
作者:
评论列表
文章目录