让我们一起爱米兰
站内搜搜:
移动设备
请扫描二维码
或访问
m.milan100.com
您所在的位置 -> 米兰百分百 -> android相关 -> Drawable、Bitmap、byte[]之间的转换

Drawable、Bitmap、byte[]之间的转换

点击数:1415 发表时间:2011-01-17 15:07:41 作者: 来源链接:
分享到:
分享到微信

android在处理一写图片资源的时候,会进行一些类型的转换,现在有空整理一下:

1、Drawable → Bitmap

	public static Bitmap drawableToBitmap(Drawable drawable) {
        
        Bitmap bitmap = Bitmap
                        .createBitmap(
                                        drawable.getIntrinsicWidth(),
                                        drawable.getIntrinsicHeight(),
                                        drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                                                        : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        //canvas.setBitmap(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
}

 

 

2、从资源中获取Bitmap

	Resources res=getResources();

Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);

 

 

3、Bitmap → byte[]

	private byte[] Bitmap2Bytes(Bitmap bm){  
    ByteArrayOutputStream baos = new ByteArrayOutputStream();    
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);    
    return baos.toByteArray();  
   }


4、 byte[] → Bitmap

 

	private Bitmap Bytes2Bimap(byte[] b){  
            if(b.length!=0){  
                return BitmapFactory.decodeByteArray(b, 0, b.length);  
            }  
            else {  
                return null;  
            }  
	}  

5、Drawable → Bitmap

Bitmap bmp1;
Drawable dw1 = new BitmapDrawable(bmp1);
0
很 好
0
一 般
0
差 劲
热门新闻
相关文章
上一篇: Android SDK 1.6之下读取手机联系人
下一篇: adb工具的使用
评论区
匿名

返回首页 | 收藏本页 | 回到顶部
Copyright 2010. 米兰百分百 Powered By Bridge.
京ICP备15050557号