【问题标题】:efficient way of loading bitmaps in android在android中加载位图的有效方法
【发布时间】:2013-07-18 18:15:23
【问题描述】:

我正在尝试使用以下代码将图库中的图片加载到位图中:

Bitmap myBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(adress), (int)viewWidth/2, (int)viewHeight/2, false);

其中“地址”是图库中图片的地址。这在我的 Galaxy Note 1,android 版本 2.3.6 上运行良好,但由于我的 Galaxy 2,android 版本 4.1.2 上的“内存不足”而崩溃

我在这里做错了吗?有没有办法得到一个缩放的位图? 生成的位图(当这确实有效时)由于缩放而有点污迹,但我不介意。 谢谢!!!

【问题讨论】:

标签: android memory bitmap


【解决方案1】:

this 直接是您的答案 - 您需要先计算尺寸,然后获取正确尺寸的图像

【讨论】:

    【解决方案2】:

    使用这个方法

        Bitmap bm=decodeSampledBitmapFromPath(src, reqWidth, reqHeight);
    

    实施-

         public 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) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }
        return inSampleSize;
    }
    
    public Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
            int reqHeight) {
    
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
    
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);
    
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap bmp = BitmapFactory.decodeFile(path, options);
        return bmp;
    }
    

    }

    【讨论】:

    • 这给了我原始比例的位图。我需要图像来填充必要的空间,即使这意味着被拉伸
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    相关资源
    最近更新 更多