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

我在 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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...