我的问题是这样的:
我需要创建一个使用 NinePatch 创建的重新缩放的位图。
我当前的系统从 NinePatch 文件创建位图。然后将其输入到 NinePatch(或 NinePatchDrawable)中。然后我需要调整它的大小并输出到另一个位图。
我已审查过this这很有帮助。
这是我当前的代码:
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 和 Y 是您希望 ninepatch 有多大,上下文是您的应用程序上下文,因此您可以获取位于 id 的 ninepatch 资源
请注意,如果您打算在 OpenGL 中使用此位图,X 和 Y 必须是 2 的幂
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)