How to convert a Drawable to a Bitmap?

来源:互联网 发布:权威的时尚杂志 知乎 编辑:程序博客网 时间:2024/05/17 21:41

原文:http://stackoverflow.com/questions/3035692/how-to-convert-a-drawable-to-a-bitmap

/** * This method returns a bitmap related to resource id. It is ready to use method, you can  * use it by simply copying in your project. *  * @param context Context of calling activity * @param drawableId Resource ID of bitmap drawable * @return Bitmap whose resource id was passed to method. */public static Bitmap getBitmapFromDrawableId(Context context,int drawableId){    Bitmap bitmap = null;    try {        BitmapDrawable drawable = (BitmapDrawable)context.getResources().getDrawable(drawableId);        bitmap = drawable.getBitmap();    } catch (Exception e) {        e.printStackTrace();    }    return bitmap;}/** * This method returns a bitmap related to drawable. It is ready to use method, you can  * use it by simply copying in your project. *  * @param drawable Drawable resource of image  * @return Bitmap whose resource id was passed to method. */public static Bitmap getBitmapFromDrawable(Drawable drawable){    Bitmap bitmap = null;    try {        BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;        bitmap = bitmapDrawable.getBitmap();    } catch (Exception e) {        e.printStackTrace();    }    return bitmap;}


原创粉丝点击