位图Bitmap缩放两种方法

一、位图缩放采用现成的类和方法
举例:

//从本地加载位图,userPic.jpg位图存储在本地SD卡上,路径为/sdcard//DCIM/userPic.jpg private void loadPicFromLocal(){ String status = Environment.getExternalStorageState(); if (status.equals(Environment.MEDIA_MOUNTED)) // 判断是否有SD卡 { //存储在SD卡上的本地头像路径,如:/sdcard//DCIM/userPic.jpg String picPath = Environment.getExternalStorageDirectory() + "/DCIM/userPic..jpg"; File f=new File(picPath); if(f.exists()){ BitmapFactory.Options options = new BitmapFactory.Options(); //缩放为原图像的1/2 options.inSampleSize = 2; Bitmap bm = BitmapFactory.decodeFile(picPath, options); //加载图像到ImageView img.setImageBitmap(bm); } } else { Toast.makeText(类名.this, "找不到SD卡", Toast.LENGTH_LONG).show(); }

 

 
二、位图缩放采用自定义函数bitmapRoom
假设已存在位图对象photo,缩放函数的调用过程
举例:

//创建一个位图对象compressedPic存储缩放后的图片 Bitmap compressedPic = null; //调用缩放函数对图片进行缩放 compressedPic = bitmapRoom(photo,60,60); //加载图像到ImageView img.setImageBitmap(compressedPhoto);

 

 

bitmapRoom函数定义: //转自http://hi.baidu.com/samyou090/item/716d763dc91531f9df22211d ,同时参考http://www.ataaw.com/develop/356.html中的详细注释 public static Bitmap bitmapRoom(Bitmap srcBitmap,int newHeight,int newWidth) { int srcWidth = srcBitmap.getWidth(); int srcHeight = srcBitmap.getHeight(); float scaleWidth = ((float) newWidth) / srcWidth; float scaleHeight = ((float) newHeight) / srcHeight; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizedBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcWidth, srcHeight, matrix, true); if(resizedBitmap != null) { return resizedBitmap; } else { return srcBitmap; } }

 
  

 

 

                

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值