Drawable转换为Bitmap两种方法

如果通过网络加载了一张位图,想拿到这张位图的Bitmap,有两种办法,至于那种好,可能要看是在什么情况下了

1,根据已有的Drawable创建一个新的Bitmap:

 1 private Bitmap bitmap;
2 private void drawableToBitamp(Drawable drawable)
3 {
4 int w = drawable.getIntrinsicWidth();
5 int h = drawable.getIntrinsicHeight();
6 System.out.println("Drawable转Bitmap");
7 Bitmap.Config config =
8 drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
9 : Bitmap.Config.RGB_565;
10 bitmap = Bitmap.createBitmap(w,h,config);
11 //注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
12 Canvas canvas = new Canvas(bitmap);
13 drawable.setBounds(0, 0, w, h);
14 drawable.draw(canvas);
15 }


2,直接从现有的Drawable中取出Bitmap:

1 private Bitmap bitmap;
2
3 private void drawableToBitamp(Drawable drawable)
4 {
5 BitmapDrawable bd = (BitmapDrawable) drawable;
6 bitmap = bd.getBitmap();
7 }



posted @ 2012-01-26 22:21  居家懒人  阅读(58275)  评论(0编辑  收藏  举报