位图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; } }

 
    
  

 

 

                

你可能感兴趣的:(Android相册,Android,Bitmap)