Bitmap 与 Drawable 转换

1、将图片转成Bitmap

  1. Resources res getResources();  
  2. Bitmap bmp BitmapFactory.decodeResource(res, R.drawable.icon);

2、Bitmap 转 byte[ ]

  1. public byte[] Bitmap2Bytes(Bitmap bm)  
  2.     ByteArrayOutputStream baos new ByteArrayOutputStream();  
  3.     bm.compress(Bitmap.CompressFormat.PNG, 100baos);  
  4.     return baos.toByteArray();  
  5.  
3、byte[ ] 转 Bitmap

  1. public Bitmap Bytes2Bimap(byte[] b)  
  2.     if (b.length != 0 
  3.         return BitmapFactory.decodeByteArray(b, 0b.length);  
  4.     else  
  5.         return null 
  6.      
  7.  
4、Drawable 转  Bitmap

  1. public static Bitmap drawableToBitmap(Drawable drawable)  
  2.         // 取 drawable 的长宽  
  3.         int drawable.getIntrinsicWidth();  
  4.         int drawable.getIntrinsicHeight();  
  5.   
  6.         // 取 drawable 的颜色格式  
  7.         Bitmap.Config config drawable.getOpacity() != PixelFormat.OPAQUE Bitmap.Config.ARGB_8888  
  8.                 Bitmap.Config.RGB_565;  
  9.         // 建立对应 bitmap  
  10.         Bitmap bitmap Bitmap.createBitmap(w, h, config);  
  11.         // 建立对应 bitmap 的画布  
  12.         Canvas canvas new Canvas(bitmap);  
  13.         drawable.setBounds(00w, h);  
  14.         // 把 drawable 内容画到画布中  
  15.         drawable.draw(canvas);  
  16.         return bitmap;  
  17.      
5、Bitmap 转  Drawable

  1. Bitmap bm=xxx; //xxx根据你的情况获取  
  2. BitmapDrawable bd= new BitmapDrawable(getResource(), bm);












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值