Android将Ninepatch资源转换为位图

时间:2014-02-10 16:46:15

标签: java android bitmap nine-patch

我有一个可以绘制为PDF的9补丁。当我尝试使用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>

,代码是:

BitmapFactory.decodeResource(mResources, resId);

2 个答案:

答案 0 :(得分:6)

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);

试试这个。现在,位图将在其上绘制九个补丁。您可以提供不同的宽度和高度。

答案 1 :(得分:1)

您需要在此处使用NinePatchDrawable API -

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