我在
android位图缩放和采样之间有困惑这里可能有两个代码用于缩放,另一个用于采样任何人都可以帮助我识别这两个代码的工作以及它们之间的主要区别.
缩放:
public static Bitmap getScaleBitmap(Bitmap bitmap,int newWidth,int newHeight) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth,scaleHeight); return Bitmap.createBitmap(bitmap,width,height,matrix,false); }
抽样:
mImageView.setimageBitmap(decodeSampledBitmapFromresource(getResources(),R.id.myimage,100,100)); public static Bitmap decodeSampledBitmapFromresource(Resources res,int resId,int reqWidth,int reqHeight) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res,resId,options); options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight); options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res,options); } public static int calculateInSampleSize( BitmapFactory.Options options,int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfheight = height / 2; final int halfWidth = width / 2; while ((halfheight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) { inSampleSize *= 2; } } return inSampleSize; }
解决方法
缩放:首先,您在内存中解码整个位图,然后进行缩放.
采样:您可以获得所需的缩放位图,而无需在内存中加载整个位图.