问题
I'm tring to download an image from the net, and to convert it to ninepatch format, how I select the stretchable pixels is out of scope for this thread.
I'm trying to use gist solution but I couldn't get it to work.
This is the source code:
NinePatchDrawable np = createNinePathWithCapInsets(res, bitmap, top,left, bitmap.getWidth() - right, bitmap.getHeight() - bottom, null);
and to get the resulted bitmap:
Bitmap outputBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outputBitmap);
//np.setBounds(0, 0, width, height);
np.draw(canvas);
canvas.drawBitmap(outputBitmap, width, height, new Paint());
The returned bitmap doesn't contain the source bitmap and looks just like 4 segmentations(rect seg').
I could find any other to achive that goal, even not using OpenGL
The resulted image is:
回答1:
try this:
public static byte[] getChunk(int xs, int xe, int ys, int ye) {
ByteBuffer b = ByteBuffer.allocate(84).order(ByteOrder.nativeOrder());
b.put((byte) 1); // wasDeserialized
b.put((byte) 2); // numXDivs
b.put((byte) 2); // numYDivs
b.put((byte) 9); // numColors
b.putInt(0); // UNKNOWN
b.putInt(0); // UNKNOWN
b.putInt(0); // paddingLeft
b.putInt(0); // paddingRight
b.putInt(0); // paddingTop
b.putInt(0); // paddingBottom
b.putInt(0); // UNKNOWN
b.putInt(xs); // segX start
b.putInt(xe); // segX end
b.putInt(ys); // segY start
b.putInt(ye); // segY end
for (int i = 0; i < 9; i++) {
b.putInt(1); // NO_COLOR
}
return b.array();
}
you can use it in NinePatchDrawable constructor (Bitmap, byte[], Rect, String)
来源:https://stackoverflow.com/questions/19955554/create-nine-patch-from-bitamp-and-getting-new-adjust-bitmap