android - 逐帧动画java.lang.OutOfMemoryError : bitmap size exceeds VM budget

标签 android animation memory-leaks

我尝试在单击按钮时加载动画。我在资源文件夹中有 90 张图片,我尝试加载它们,每张图片大约有。大小 50kb 当我点击按钮启动动画时,我得到了 OutOfmemory 异常。请检查以下代码。任何帮助将不胜感激。

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    startbtn=(Button) findViewById(R.id.myStartButton);
    stopbtn=(Button)findViewById(R.id.myStopButton);
    startbtn.setOnClickListener(this);
    stopbtn.setOnClickListener(this);
    images=(ImageView) findViewById(R.id.myImageView);

    images.setBackgroundResource(R.drawable.demo_animation);

    AniFrame = (AnimationDrawable)images.getBackground();



}

public void onClick(View v) {
    if(v.getId()==R.id.myStartButton)
    {
        AniFrame.start();
    }else if(v.getId()==R.id.myStopButton)
    {
        AniFrame.stop();
    }

}

最佳答案

图像是否经过压缩(jpg 或 png)?我认为当它们被加载到内存中时,它们会被转换为位图格式,并使用更多的内存(每个像素最多 4 个字节)。 这可能是您的内存不足异常的原因。您无法像在磁盘上那样计算 90x50kb,请考虑 90 x 750Kb。

关于android - 逐帧动画java.lang.OutOfMemoryError : bitmap size exceeds VM budget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8636266/

相关文章:

java - FTP:创建文件夹 (Android)

javascript - 向下滑动时旋转图像

c++ - 为什么这段代码不会导致内存泄漏?

android - 无法与 picasso 和 okhttp 一起设置 retrofit 超时

android - 使用这里的sdk时出现内存不足错误

android - 如何在版本之间进行数据库迁移单元测试

animation - 应用变换矩阵后计算路径中心

javascript - 根据传入的 props 有条件地设置模态的宽度

memory-leaks - 如何解决 System.Diagnostics.PerformanceCounter 引起的内存泄漏

iphone - NSCFTimer 是否存在内存泄漏?