我在
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; }
解决方法
缩放:首先,您在内存中解码整个位图,然后进行缩放.
采样:您可以获得所需的缩放位图,而无需在内存中加载整个位图.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。