【问题标题】:Creating a scaled bitmap with createScaledBitmap in Android在 Android 中使用 createScaledBitmap 创建缩放位图
【发布时间】:2013-07-24 05:54:47
【问题描述】:

我想创建一个缩放位图,但我似乎得到了一个不成比例的图像。它看起来像一个正方形,而我想要长方形。

我的代码:

Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, 960, 960, false);

我希望图像的 MAX 为 960。我该怎么做?将宽度设置为 null 不会编译。这可能很简单,但我无法理解它。谢谢

【问题讨论】:

    标签: java android bitmap


    【解决方案1】:

    如果内存中已经有了原始位图,则不需要进行inJustDecodeBoundsinSampleSize等全过程,只需要弄清楚要使用什么比例并进行相应的缩放即可。

    final int maxSize = 960;
    int outWidth;
    int outHeight;
    int inWidth = myBitmap.getWidth();
    int inHeight = myBitmap.getHeight();
    if(inWidth > inHeight){
        outWidth = maxSize;
        outHeight = (inHeight * maxSize) / inWidth; 
    } else {
        outHeight = maxSize;
        outWidth = (inWidth * maxSize) / inHeight; 
    }
    
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, outWidth, outHeight, false);
    

    如果此图像的唯一用途是缩放版本,则最好使用 Tobiel 的答案,以尽量减少内存使用。

    【讨论】:

    • 您能详细说明您的评论吗? “如果此图像的唯一用途是缩放版本,您最好使用 Tobiel 的答案”?
    • 我的意思是,如果您使用它缩放,请使用其他答案。如果您只是以这种方式使用它,它会更有效。如果您已经在加载/显示它的完整版本,则此方法很好,因为无论如何您都需要内存。
    • 我用这种方法记忆犹新。
    • 我相信写outHeight = inHeight * (outWidth / inWidth);而不是outHeight = (inHeight * maxSize) / inWidth;会更易读/易懂
    【解决方案2】:

    您的图像是方形的,因为您设置了width = 960height = 960

    您需要创建一个方法来传递您想要的图像大小,如下所示:http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    在代码中是这样的:

    public static Bitmap lessResolution (String filePath, int width, int height) {
        int reqHeight = height;
        int reqWidth = width;
        BitmapFactory.Options options = new BitmapFactory.Options();    
    
        // First decode with inJustDecodeBounds=true to check dimensions
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
    
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;        
    
        return BitmapFactory.decodeFile(filePath, options); 
    }
    
    private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
    
        if (height > reqHeight || width > reqWidth) {
            // Calculate ratios of height and width to requested height and width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
    
            // Choose the smallest ratio as inSampleSize value, this will guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        return inSampleSize;
    }
    

    【讨论】:

    • 哇!感谢您的代码,但要确保图像保持其比例,还有很多工作要做。我希望有一些神奇的参数可以传入。感谢代码 sn-p!
    • 问题是您可以使用此代码阻止 OOM,原因有很多。这是关于位图的 android 文档:developer.android.com/training/displaying-bitmaps/index.html
    • OOM?另外,您如何看待这里的答案?非常感谢您的建议:stackoverflow.com/questions/4837715/…
    • OOM 表示“内存不足”,这将停止您的应用程序,因为您超出了允许的内存量。我的建议是尝试您认为适合您的解决方案。在答案中不要尝试标记为解决方案的那个,因为他们正在从 base64String 解码图像。
    • 请注意,此方法不会为您提供我们想要的大小,但图像的大小可能按 2 的幂按比例缩小(如果您有一个 10kx10k 的图像并要求一个 5001x5001 的图像,你会得到一个 10kx10k 的图像)。
    【解决方案3】:
    bmpimg = Bitmap.createScaledBitmap(srcimg, 100, 50, true);
    

    【讨论】:

      猜你喜欢
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多