android – 位图的缩放和采样有什么区别?

android – 位图的缩放和采样有什么区别?,第1张

概述我在 android位图缩放采样之间有困惑这里可能有两个代码用于缩放,另一个用于采样任何人都可以帮助我识别这两个代码的工作以及它们之间的主要区别. 缩放: public static Bitmap getScaleBitmap(Bitmap bitmap, int newWidth, int newHeight) { int width = bitmap.getWidth(); 我在 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;}

这里两个代码执行图像调整大小但不同的方式,所以我如何识别哪一个是好的和简单的.

解决方法 缩放:首先,您在内存中解码整个位图,然后进行缩放.

采样:您可以获得所需的缩放位图,而无需在内存中加载整个位图.

总结

以上是内存溢出为你收集整理的android – 位图的缩放和采样有什么区别?全部内容,希望文章能够帮你解决android – 位图的缩放和采样有什么区别?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1123943.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存