一般来说,在Android中对图片进行缩放,我们使用以下的代码:
publicBitmap resizeImage(Bitmap bitmap,intw,inth) {
Bitmap BitmapOrg = bitmap;
intwidth = BitmapOrg.getWidth();
intheight = BitmapOrg.getHeight();
intnewWidth = w;
intnewHeight = h;
floatscaleWidth = ((float) newWidth) / width;
floatscaleHeight = ((float) newHeight) / height;
Matrix matrix = newMatrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0,0, width,
height, matrix, true);
returnresizedBitmap;
}
上面这段代码也是一般对图片的进行缩放的方式,不会出什么问题,但是有两个小问题:
1、我们先要从磁盘上先将图片加载到内存,然后才能对图片进行缩放,在移动设备上对内存的要求比较高,这在一定程度上降级了性能。
2、我们使用Bitmap.createBitmap这个方法进行缩放,使用的是Java层面的方法来缩放,我们知道Java层面对图片,视频等进行处理是有性能损失的。
如果只有一两张图片,用上面的方法进行缩放是没有问题的,如果是大量的图片或者是图片很大,就会出现明显降低性能,甚至出现OOM的异常,OOM的异常是必须要处理的。
下面给出优化方案,优化我们也是基于上面两点问题给出的。一次性从磁盘上读取并缩放,而且这个缩放实在Native层,能够显著提高效率,代码如下:
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
BitmapFactory.Options options = newBitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getAssets().open("g_b090.jpg"),null, options);
inttargetDensity = getResources().getDisplayMetrics().densityDpi;
DisplayMetrics dm = newDisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
intx = dm.widthPixels;
inty = dm.heightPixels;
options.inSampleSize = calculateInSampleSize(options, x, y);
doublexSScale = ((double)options.outWidth) / ((double)x);
doubleySScale = ((double)options.outHeight) / ((double)y);
doublestartScale = xSScale > ySScale ? xSScale : ySScale;
options.inScaled = true;
options.inDensity = (int) (targetDensity*startScale);
options.inTargetDensity = targetDensity;
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open("g_b090.jpg"),null, options);
} catch(IOException e) {
e.printStackTrace();
}
}
其中
calculateInSampleSize方法如下
publicintcalculateInSampleSize(BitmapFactory.Options options,
intreqWidth,intreqHeight) {
// Raw height and width of image
finalintheight = options.outHeight;
finalintwidth = options.outWidth;
intinSampleSize =1;
if(height > reqHeight || width > reqWidth) {
finalinthalfHeight = height /2;
finalinthalfWidth = width /2;
// Calculate the largest inSampleSize value that is a power of 2 and
// keeps both
// height and width larger than the requested height and width.
while((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
returninSampleSize;
}
这段代码是google给出的,我们无需对它进行修改,在上面这段代码中我们使用BitmapFactory.Options的inScaled,inDensity,inTargetDensity来变相对图片进行缩放,但是这有一个局限,就是只能对图片进行等比例的放大或者缩小,不能按任意大小进行缩放,不过这还是适用大部分的情况,因为很少有对图片进行任意的缩放,因为那样会使图片变形。大家有什么问题或者有什么错误,请给我留言,大家一起讨论!!!