Bitmap for NinePatch for the new scaled bitmap

My problem is this:

I need to create a rescaled bitmap created using NinePatch.

My current system creates a bitmap from a NinePatch file. Then it is served in NinePatch (or NinePatchDrawable). Then I need to resize it and output it to another bitmap.

I looked over this one and it helped quite a bit.

Here is my current code:

try { Bitmap bitmap1 = BitmapFactory.decodeStream(getAssets().open("gfx/head.9.png")); // Using NinePatch? NinePatch patch = new NinePatch(bitmap1, bitmap1.getNinePatchChunk(), null); // Or NinePatchDrawable? NinePatchDrawable patch = new NinePatchDrawable(bitmap1, bitmap1.getNinePatchChunk(), new Rect(), null); // Set dimensions from NinePatch and create new bitmap // Bitmap bitmap2 = ? } catch (IOException e) { e.printStackTrace(); } 
+3
android bitmap nine-patch
source share
3 answers
 public static Bitmap get_ninepatch(int id,int x, int y, Context context){ // id is a resource id for a valid ninepatch Bitmap bitmap = BitmapFactory.decodeResource( context.getResources(), id); byte[] chunk = bitmap.getNinePatchChunk(); NinePatchDrawable np_drawable = new NinePatchDrawable(bitmap, chunk, new Rect(), null); np_drawable.setBounds(0, 0,x, y); Bitmap output_bitmap = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output_bitmap); np_drawable.draw(canvas); return output_bitmap; } 

X and Y - as much as you want your nine-pack to be, context is your application context, so you can get your resource with nine folders located on id

Please note that if you intend to use this bitmap in OpenGL X, and Y must have a power of 2

+14
source share

Are you asking how to draw a NinePatchDrawable? If so, create a Canvas pointing to the Bitmap you want to draw into, and render the NinePatchDrawable in Canvas using Drawable.draw (). Be sure to use Drawable.setBounds to set the size you want.

0
source share

Tasomaniac - To get my x and y for this method, I use a window or layout size or what is needed for nine patches. If your window or layout is re-significant, just insert x and y with width and height, perhaps with: (e.g. popwindow) object.getViewTreeObserver (). addOnGlobalLayoutListener (new OnGlobalLayoutListener () ... and then every time something changes with this particular object or layout, just get x and y into integers.

0
source share

All Articles