Bitmap to NinePatch to New Scaled Bitmap

Deadly 提交于 2019-12-01 00:10:44
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 are how big you want the ninepatch to be, context is your application context so you can get your ninepatch resource located at id

Please Note if you intend to use this bitmap in OpenGL X and Y must be powers of 2

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

Tasomaniac - To get my x and y for that method I am using the window or size of layout or what ever is needed for the nine-patch. If your window or layout is re-sizable just plug in x and y with the width and height maybe with: (eg popwindow)object.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()... and then every time something changes with that particular object or layout, just get the x and y into integers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!