Java Android将Ninepatch资源转换为位图

Java Android将Ninepatch资源转换为位图,java,android,bitmap,nine-patch,Java,Android,Bitmap,Nine Patch,我有一个9补丁,可以作为XML绘制。当我尝试使用BitmapFactory.decode从中提取位图时,它返回null。是否仍然可以从该资源获取位图 nine_patch.xml: <?xml version="1.0" encoding="utf-8"?> <nine-patch xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/empty_icon_vi

我有一个9补丁,可以作为XML绘制。当我尝试使用
BitmapFactory.decode
从中提取位图时,它返回
null
。是否仍然可以从该资源获取位图

nine_patch.xml:

<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/empty_icon_video" >

</nine-patch>
试试这个。现在位图将在其上绘制ninepatch。您可以提供不同的宽度和高度。

您需要在此处使用API-

NinePatchDrawable npDrawable = (NinePatchDrawable)getResources().getDrawable(R.drawable.empty_icon_video);

我的问题是,我是从BitmapDrawable继承的,因此超级构造函数采用位图而不是NinePatchDrawable
Bitmap bmp = Bitmap.createBitmap(yourWidth, yourHeight, Bitmap.Config.ARGB_8888);
Drawable drawable = getResources().getDrawable(R.drawable.resId);
Canvas canvas = new Canvas(bmp);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
NinePatchDrawable npDrawable = (NinePatchDrawable)getResources().getDrawable(R.drawable.empty_icon_video);