Last active
December 3, 2024 01:35
-
-
Save briangriffey/4391807 to your computer and use it in GitHub Desktop.
Create 9-patches simlar to - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.res.Resources; | |
import android.graphics.Bitmap; | |
import android.graphics.NinePatch; | |
import android.graphics.Rect; | |
import android.graphics.drawable.NinePatchDrawable; | |
import java.nio.ByteBuffer; | |
import java.nio.ByteOrder; | |
/** | |
* User: bgriffey | |
* Date: 12/27/12 | |
* Time: 2:37 PM | |
*/ | |
public class NinePatchBitmapFactory { | |
// The 9 patch segment is not a solid color. | |
private static final int NO_COLOR = 0x00000001; | |
// The 9 patch segment is completely transparent. | |
private static final int TRANSPARENT_COLOR = 0x00000000; | |
public static NinePatchDrawable createNinePathWithCapInsets(Resources res, Bitmap bitmap, int top, int left, int bottom, int right, String srcName) { | |
ByteBuffer buffer = getByteBuffer(top, left, bottom, right); | |
NinePatchDrawable drawable = new NinePatchDrawable(res, bitmap, buffer.array(), new Rect(), srcName); | |
return drawable; | |
} | |
public static NinePatch createNinePatch(Resources res, Bitmap bitmap, int top, int left, int bottom, int right, String srcName) { | |
ByteBuffer buffer = getByteBuffer(top, left, bottom, right); | |
NinePatch patch = new NinePatch(bitmap, buffer.array(), srcName); | |
return patch; | |
} | |
private static ByteBuffer getByteBuffer(int top, int left, int bottom, int right) { | |
//Docs check the NinePatchChunkFile | |
ByteBuffer buffer = ByteBuffer.allocate(56).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)0x02); | |
//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); | |
return buffer; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use this class? Can you share a code sample?
How do we get the left top right bottom in createNinePatch** methods ?