加载大背景图像时的OutOfMemory [英] OutOfMemory when loading large Background Image

查看:187
本文介绍了加载大背景图像时的OutOfMemory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 android:background 为android布局提供背景图片。

放置一些图像后我得到了这个例外:

I am using android:background to give a background image to android layout.
After putting some images I get this exception :

08-24 00:40:19.237: E/dalvikvm-heap(8533): Out of memory on a 36000016-byte allocation.


如何在android上使用大图像作为背景?

我可以扩展应用程序堆内存吗?还是不好做?


how can I use large images as backgrounds on android?
can I expand the application heap memory? or is it something not good to do ?

推荐答案

请查看我的相关问题:

高分辨率图片 - OutOfMemoryError

High resolution Image - OutOfMemoryError

尽量使背景图像尽可能小,尽量减少应用程序的内存使用量。

Try to minimize the memory usage of your application by keeping the background image as small as possible.

这个可以通过以下方式完成:

This can be done via:


  • 裁剪图像以使其适合屏幕

  • 压缩图像进一步(使用例如photoshop)甚至在应用程序中使用它之前

  • 使用下面的方法加载你的位图

  • 再次回收位图没有loger需要它

  • 确保你没有在内存中保留多个实例

  • 使用位图后将引用设置为null

  • cropping the image so that it fits the screen
  • compress the image further (use e.g. photoshop) before even using it in the app
  • use the below method to load your bitmap
  • recycle the bitmap as soon as you no loger need it
  • make sure you do not keep multiple instances of it in memory
  • set the reference to null after using the bitmap

确保您设置为背景的图像已正确加载(例如,裁剪尺寸,例如适合屏幕尺寸),并在不再需要时立即从存储器中释放。

确保你有内存中只有一个Bitmap实例。显示后,调用 recycle()并将引用设置为null。

Make sure you have only one instance of your Bitmap in memory. After displaying it, call recycle() and set your reference to null.

这是加载图片的方法:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    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;
}

感谢 Adam Stelmaszczyk 这段美丽的代码。

这篇关于加载大背景图像时的OutOfMemory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆