Resource,Drawable和Bitmap之间的转换

来源:互联网 发布:mac 命令查看运行程序 编辑:程序博客网 时间:2024/06/06 04:19

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

 

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();

 

阅读全文
0 0