简单谈谈Resource,Drawable和Bitmap之间的转换

一直接触这些东西,还是归个类整理一下比较好。

 

Resource -> Drawable

Drawable draw1 = this.getResources().getDrawable(R.drawable.icon);

Drawable -> Bitmap

1.

 

static Bitmap drawableToBitmap(Drawable drawable) // drawable 转换成bitmap  
{  
    int width = drawable.getIntrinsicWidth();// 取drawable的长宽  
    int height = drawable.getIntrinsicHeight();  
    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ?Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565;// 取drawable的颜色格式  
    Bitmap bitmap = Bitmap.createBitmap(width, height, config);// 建立对应bitmap  
    Canvas canvas = new Canvas(bitmap);// 建立对应bitmap的画布  
    drawable.setBounds(0, 0, width, height);  
    drawable.draw(canvas);// 把drawable内容画到画布中  
    return bitmap;  
}  

2.

Bitmap bitmap = ((android.graphics.drawable.BitmapDrawable) pm.getApplicationIcon(appInfo)).getBitmap();

 

posted @ 2014-07-17 09:03  Rex..  阅读(24119)  评论(0编辑  收藏  举报