drawable 转换成bitmap

public Bitmap setIcon(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();   // 取drawable的长宽
        int height = drawable.getIntrinsicHeight();
        
        Log.v("setIcon","*****width:" + width);
        Log.v("setIcon","*****height:" + height);
        
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565;         // 取drawable的颜色格式
        int ww = (int)(width*1.5);
        int hh = (int)(height*1.5);
         Bitmap bitmap = Bitmap.createBitmap(ww, hh, config);     // 建立对应bitmap
        Canvas canvas = new Canvas(bitmap);         // 建立对应bitmap的画布
        drawable.setBounds(0, 0, ww, hh);
        drawable.draw(canvas);      // 把drawable内容画到画布中
        
       return bitmap;
	}

相关推荐