Android Bitmap用法大全,以后再也不担心了

本文介绍了Android开发中图片处理的多种实用方法,包括Drawable到Bitmap的转换、从资源获取Bitmap、Bitmap与byte数组间的相互转换、图片的缩放调整、从文件加载Bitmap等,为开发者提供了丰富的图片处理手段。

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

1、Drawable → Bitmap

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public static Bitmap drawableToBitmap(Drawable drawable) {  
  2.   
  3. Bitmap bitmap = Bitmap  
  4.   
  5. .createBitmap(  
  6.   
  7. drawable.getIntrinsicWidth(),  
  8.   
  9. drawable.getIntrinsicHeight(),  
  10.   
  11. drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
  12.   
  13. : Bitmap.Config.RGB_565);  
  14.   
  15. Canvas canvas = new Canvas(bitmap);  
  16.   
  17. // canvas.setBitmap(bitmap);  
  18.   
  19. drawable.setBounds(00, drawable.getIntrinsicWidth(),  
  20.   
  21. drawable.getIntrinsicHeight());  
  22.   
  23. drawable.draw(canvas);  
  24.   
  25. return bitmap;  
  26.   
  27. }  

2、从资源中获取Bitmap

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Resources res=getResources();  
  2.   
  3. Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);  
3、Bitmap → byte[]
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. private byte[] Bitmap2Bytes(Bitmap bm){  
  2.   
  3. ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  4.   
  5. bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  
  6.   
  7. return baos.toByteArray();  
  8.   
  9. }  

4、byte[] → Bitmap

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. private Bitmap Bytes2Bimap(byte[] b){  
  2.   
  3. if(b.length!=0){  
  4.   
  5. return BitmapFactory.decodeByteArray(b, 0, b.length);  
  6.   
  7. }  
  8.   
  9. else {  
  10.   
  11. return null;  
  12.   
  13. }  
  14.   
  15. }  
5、保存bitmap

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static boolean saveBitmap2file(Bitmap bmp,String filename){  
  2.   
  3. CompressFormat format= Bitmap.CompressFormat.JPEG;  
  4.   
  5. int quality = 100;  
  6.   
  7. OutputStream stream = null;  
  8.   
  9. try {  
  10.   
  11. stream = new FileOutputStream("/sdcard/" + filename);  
  12.   
  13. catch (FileNotFoundException e) {  
  14.   
  15. // TODO Auto-generated catch block  
  16.   
  17. Generated by Foxit PDF Creator © Foxit Software  
  18.   
  19. http://www.foxitsoftware.com For evaluation only.  
  20.   
  21. e.printStackTrace();  
  22.   
  23. }  
  24.   
  25. return bmp.compress(format, quality, stream);  
  26.   
  27. }  

6、将图片按自己的要求缩放

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // 图片源  
  2.   
  3. Bitmap bm = BitmapFactory.decodeStream(getResources()  
  4.   
  5. .openRawResource(R.drawable.dog));  
  6.   
  7. // 获得图片的宽高  
  8.   
  9. int width = bm.getWidth();  
  10.   
  11. int height = bm.getHeight();  
  12.   
  13. // 设置想要的大小  
  14.   
  15. int newWidth = 320;  
  16.   
  17. int newHeight = 480;  
  18.   
  19. // 计算缩放比例  
  20.   
  21. float scaleWidth = ((float) newWidth) / width;  
  22.   
  23. float scaleHeight = ((float) newHeight) / height;  
  24.   
  25. // 取得想要缩放的matrix参数  
  26.   
  27. Matrix matrix = new Matrix();  
  28.   
  29. matrix.postScale(scaleWidth, scaleHeight);  
  30.   
  31. // 得到新的图片  
  32.   
  33. Bitmap newbm = Bitmap.createBitmap(bm, 00, width, height, matrix,  
  34.   
  35. true);  
  36.   
  37. // 放在画布上  
  38.   
  39. canvas.drawBitmap(newbm, 00, paint);  

7:File图片转Bitmap

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Bitmap bt = BitmapFactory.decodeFile("/sdcard/myImage/" + "head.jpg");//图片地址  

8://图片转Bitmap

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public Bitmap drawableToBitamp(int drawableResource) {<span style="white-space:pre">  </span>//可以取raw里面的资源  
  2.         BitmapFactory.Options opt = new BitmapFactory.Options();  
  3.         opt.inPreferredConfig = Bitmap.Config.RGB_565;  
  4.         opt.inPurgeable = true;  
  5.         opt.inInputShareable = true;  
  6.         InputStream is = this.getResources().openRawResource(drawableResource);  
  7.         BitmapFactory.decodeStream(is, null, opt);  
  8.         return BitmapFactory.decodeStream(is, null, opt);  
  9.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值