点9图默认是没有大小尺寸的,需要先指定尺寸后才能获得具体图像,根据资源文件获取位图的方法如下:
参数说明:
context:上下文索引
resId:点9图的资源Id,例如R.drawable.button
width:指定宽度(像素)
height:指定高度(像素)
public Bitmap NinePatch2Bitmap(Context context, int resId, int width, int height) {
Drawable drawable = context.getResources().getDrawable(resId);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
网友评论