首页 > 代码库 > 按比例缩放显示bitmap

按比例缩放显示bitmap

BitmapFactory.Options factoryOptions = new BitmapFactory.Options();// 不将图片读取到内存中,仍然可以通过参数获得它的高宽factoryOptions.inJustDecodeBounds = true;BitmapFactory.decodeFile(path, factoryOptions);int imageWidth = factoryOptions.outWidth;int imageHeight = factoryOptions.outHeight;
// 等比缩小,previewWidth和height是imageView的宽高int scaleFactor = Math.max(imageWidth / previewWidth,imageHeight / previewHeight);// 将图片读取到内存中factoryOptions.inJustDecodeBounds = false;// 设置等比缩小图factoryOptions.inSampleSize = scaleFactor;// 样图可以回收内存factoryOptions.inPurgeable = true;uploadImage = BitmapFactory.decodeFile(path, factoryOptions);imgPrivew.setImageBitmap(uploadImage);

  加载显示一符图像,对内存的使用有显著影响,BitmapFactory提供了一系列静态方法加载不同来源的图片。

1.factoryOptions.inJustDecodeBounds = true;设置不对图片解码,只需返回图片范围,
factoryOptions.outWidth; factoryOptions.outHeight;
两个属性就会被赋值。
2.
factoryOptions.inSampleSize = scaleFactor; 
比如inSampleSize=8,产生的是原图像1/8大小尺寸的图像
此外,Any value <= 1 is treated the same as 1,任意小于等于1的值的当做1处理。