位图NinePatch新缩放位图 [英] Bitmap to NinePatch to New Scaled Bitmap

查看:249
本文介绍了位图NinePatch新缩放位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是这样的:

我需要创建使用NinePatch创建一个重新缩放位图。

我现在的系统创建了一个从NinePatch文件的位图。这然后被送入一个NinePatch(或NinePatchDrawable)。然后,我需要将其输出调整到另一个位图。

我已审查<一个href=\"http://stackoverflow.com/questions/5079868/create-a-ninepatch-ninepatchdrawable-in-runtime\">this它帮了不少忙。

下面是我目前的code:

  {尝试
        位图bitmap1 = BitmapFactory.de codeStream(getAssets()开(GFX / head.9.png));        //使用NinePatch?
        NinePatch补丁=新NinePatch(bitmap1,bitmap1.getNin​​ePatchChunk(),NULL);
        //或NinePatchDrawable?
        NinePatchDrawable补丁=新NinePatchDrawable(bitmap1,bitmap1.getNin​​ePatchChunk(),新的矩形(),NULL);        //从NinePatch集的尺寸和创建新的位图
        //位图bitmap2 =?
}
赶上(IOException异常五){
        e.printStackTrace();
}


解决方案

 公共静态位图get_ninepatch(INT ID,诠释的x,INT Y,上下文的背景下){
        // id是一个有效的ninepatch资源ID        位图位图= BitmapFactory.de codeResource(
                context.getResources(),ID);        字节[] =块bitmap.getNin​​ePatchChunk();
        NinePatchDrawable np_drawable =新NinePatchDrawable(位图,
                块,新矩形(),NULL);
        np_drawable.setBounds(0,0,X,Y);        位图output_bitmap = Bitmap.createBitmap(X,Y,Bitmap.Config.ARGB_8888);
        帆布帆布=新的Canvas(output_bitmap);
        np_drawable.draw(画布);        返回output_bitmap;
    }

X和Y是你想要多大ninepatch是,上下文是您的应用程序上下文,这样你可以得到位于ID您ninepatch资源

请注意,如果你打算使用此位图在OpenGL的X和Y必须是2的幂

My problem is this:

I need to create a re-scaled bitmap created using a NinePatch.

My current system creates a Bitmap from a NinePatch file. This then gets fed into a NinePatch (or NinePatchDrawable). I then need to resize it and output to another Bitmap.

I have reviewed this 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();
}

解决方案

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

这篇关于位图NinePatch新缩放位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆