Android进阶2之Bitmap、Drawable、byte[]转换

本文介绍了如何在Android中实现Drawable与Bitmap之间的相互转换,并提供了具体的Java代码实现。此外还介绍了如何从资源中获取Bitmap,以及Bitmap与byte数组之间的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将Drawable转化为Bitmap

  1. public static Bitmap drawableToBitmap(Drawable drawable) {  
  2.         // 取 drawable 的长宽   
  3.         int w = drawable.getIntrinsicWidth();  
  4.         int h = 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(00, w, h);  
  14.         // 把 drawable 内容画到画布中   
  15.         drawable.draw(canvas);  
  16.         return bitmap;  
  17.     }  
public static Bitmap drawableToBitmap(Drawable drawable) {
		// 取 drawable 的长宽
		int w = drawable.getIntrinsicWidth();
		int h = drawable.getIntrinsicHeight();

		// 取 drawable 的颜色格式
		Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
				: Bitmap.Config.RGB_565;
		// 建立对应 bitmap
		Bitmap bitmap = Bitmap.createBitmap(w, h, config);
		// 建立对应 bitmap 的画布
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, w, h);
		// 把 drawable 内容画到画布中
		drawable.draw(canvas);
		return bitmap;
	}
从资源中获取Bitmap
  1. Resources res = getResources();  
  2. Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);  
		Resources res = getResources();
		Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);

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

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

Bitmap转换成Drawable
  1. Bitmap bm=xxx; //xxx根据你的情况获取  
  2. BitmapDrawable bd= new BitmapDrawable(getResource(), bm);   
  3. 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。  
Bitmap bm=xxx; //xxx根据你的情况获取
BitmapDrawable bd= new BitmapDrawable(getResource(), bm); 
因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值