閱讀139 返回首頁    go 阿裏雲 go 技術社區[雲棲]


How to convert a Drawable to a Bitmap?

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


最後更新:2017-04-02 15:15:09

  上一篇:go 曆史十大黑客事件:不堪一擊的係統
  下一篇:go 物理學家眼中的世界:編程的未來