java.lang.OutOfMemoryError发生在动画上吗? [英] java.lang.OutOfMemoryError occur on the animation Loading?

查看:71
本文介绍了java.lang.OutOfMemoryError发生在动画上吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从开发人员站点获得的信息是:如果在加载时动画的总大小超过了虚拟堆内存的值,则会发生此错误".对于解决方案,请使用 BitmapFactory .我在下面的代码中尝试过:

What I get from the Developer site is "If in the loading time total size of the animation exceed the value of Virtual heap memory , this error will occur" and For the Solution use BitmapFactory. I tried in the below code :

现在,问题是这样的,此错误的原因是什么?" :

Now, the question is this, "what are responsible for this error ?" :

  1. 在为动画加载图像时,是否计算图像的尺寸.

  1. Does dimensions of the image are count when the Images load for animation.

像:如果我有10个图像尺寸(1100 * 1100),并且对图像使用ARGB_8888模式.

Like : If I have 10 images dimensions( 1100 * 1100 ) and use ARGB_8888 pattern for the image.

图片大小(500KB,1MB等)对于此错误也很重要.

Size of images(500KB, 1MB etc ) also matter for this error.

或者这两个原因都引起了这个错误.

Or Both are responsible for this error.

如果我在一个活动上使用的动画超过一个,而不是我将本活动更改为另一个活动,如何释放上一个我正在使用的所有内存通过 AnimationDrawable 进行的活动?

If I am using more than one animation on a single activity than I change this Activity to another Activity , How to free all memory which I'm using in the previous Activity by AnimationDrawable ?

欢迎所有建议.谢谢

代码

public class AdActivity extends Activity {

ImageView iView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ad);
    
    iView = (ImageView) findViewById(R.id.iView);
    
    //iView.setBackgroundResource(R.drawable.anim); // ( 1 )

    try{ 
             //  (2)
        iView.setImageBitmap(decodeSampleBitmapFromResource(getResources(), R.drawable.anim, 100, 100));
        
        AnimationDrawable frameAnimation = (AnimationDrawable) iView.getBackground();
        frameAnimation.start();
    }
    catch(Exception e){
    }
}

public 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) {

        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

public static Bitmap decodeSampleBitmapFromResource(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, resId, options);
}

}

两件事:

  1. 在这里我提到了带有(1)的注释,这是在ImageView中获取动画的简单方法,但是在这种情况下,启动该应用程序时使用的总大小约为44MB.运行完美"

  1. Where I mention a comment with ( 1 ), This is simple way to get the animation in a ImageView but in this case the toatal size used is around 44MB when the App is started. 'Run Perfect'

在尝试中提到(2)的地方,这是Android开发人员网站

Where I mention ( 2 ) in the try , This is solution which is given by Android developer website here .

我未尝试执行任何错误,并且在运行该应用程序时大小为apporox 11Mb,但动画"未显示在ImageView中.

i tried no error and size is apporox 11Mb when the App is run but The Animation is not showing in the ImageView.

请告诉我我做错了什么?

Please Suggest me what I did wrong ?

推荐答案

您可以通过以下方法解决错误:1)清单文件中的android:largeheap ="true"增加dalvik堆大小

you can solve your error by 1) increasing your dalvik heap size by android:largeheap="true" in manifest file

2)如果您重复使用解码后的图像,则需要实现Lrucache

2) if your reusing your decoded images then you need to implement Lrucache

3)每当您通过使用encodeSampleBitmapFromResource()解码任何图像时,其引用都存储在本机堆中.可以通过System.gc()清除它以回收内存,但是我们不能依靠它,因为它是由OS决定的何时发布.

3) whenever you decode any image by using decodeSampleBitmapFromResource() then it refences are stored in native heap.it can be cleared by System.gc() to reclaim memory,but we cant depend on this because it is decided by OS when to release it.

4)如果您想清除本机堆,则应使用NDK编写代码

4) If u want to clear the native heap you should write a code in NDK

这篇关于java.lang.OutOfMemoryError发生在动画上吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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