How to convert a Drawable to a Bitmap?

简介:

原文: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;
}


相关文章
|
2月前
|
存储 监控
Bitmap
【10月更文挑战第7天】
33 1
|
6月前
|
存储 算法 Java
BitMap介绍
BitMap介绍
30 0
|
Android开发
Android获取Bitmap网络图片类型
Android获取Bitmap网络图片类型
使用Bitmap.createBitmap遇到的问题
使用Bitmap.createBitmap遇到的问题
455 0
|
Java Android开发
Bitmap详解
Bitmap的分析与使用 Bitmap的创建 创建Bitmap的时候,Java不提供new Bitmap()的形式去创建,而是通过BitmapFactory中的静态方法去创建,如:BitmapFactory.
2104 0