简体   繁体   中英

Android Converting Ninepatch Resource To Bitmap

I have a 9 patch drawable as XML. When I try to use BitmapFactory.decode to get bitmap out of it, it returns null . Is there anyway to get a bitmap from this resource?

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>

and the code is:

BitmapFactory.decodeResource(mResources, resId);
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);

Try this. Now the bitmap will be having the ninepatch drawn over it. You can supply a different width and height.

您需要在此处使用NinePatchDrawable API-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM