gpt4 book ai didi

android - 通过 AnimationDrawable 播放动画时在运行时释放内存

转载 作者:太空狗 更新时间:2023-10-29 12:44:26 25 4
gpt4 key购买 nike

我至少有 <=30 张图像来制作动画。当我播放动画手机内存满载。我在 log cat 中收到一条消息,例如:

    12-26 18:01:48.335: D/dalvikvm(7385): GC_FOR_ALLOC freed 257K, 5% free 44870K/47175K, paused 16ms, total 16ms
12-26 18:01:48.340: I/dalvikvm-heap(7385): **Grow heap (frag case) to 45.700MB** for 1048592-byte allocation
12-26 18:01:48.355: D/dalvikvm(7385): GC_FOR_ALLOC freed <1K, 5% free 45894K/48263K, paused 15ms, total 15ms

将堆( fragment 情况)增加到 45.700MB。因此,许多低虚拟内存设备无法运行此动画和应用程序崩溃。

我使用优化的 PNG 文件来减少运行时的 VM 大小。但仍然没有变化。我还在 list 文件中写了 android:allowBackup="true" ,它有更好的帮助。我这样播放我的动画:

private AnimationDrawable independentAnimation;

imageViewAnimation = (ImageView) findViewById(R.id.imageViewAnimation);
// set the animation drawable as background
imageViewAnimation
.setBackgroundResource(R.drawable.demo_logo_animation);
// create an animation drawable using the background
independentAnimation = (AnimationDrawable) imageViewAnimation
.getBackground();


// start the animation
imageViewAnimation.post(new Runnable() {

@Override
public void run() {
// for 2.3 devices & All runs well
independentAnimation.start();
}
});

所需的动画 demo_logo_animation.xml 看起来:部分:

  <?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >

<item
android:drawable="@drawable/itv001"
android:duration="@string/log_in_duration"/>

<item
android:drawable="@drawable/itv003"
android:duration="@string/log_in_duration"/>
<!-- <item -->
<!-- android:drawable="@drawable/logoani256_00004" -->
<!-- android:duration="25"/> -->
<!-- <item -->

<!-- android:drawable="@drawable/logoani256_00005" -->
<!-- android:duration="25"/> -->
<item
android:drawable="@drawable/itv006"
android:duration="@string/log_in_duration"/>

<!-- remaining are here -->
</animation-list>

我在 SAMSUNG Galaxy S3 中运行该应用程序 --> 结果没问题。但在 Sony X-Peria New我有时会出错。有什么解决方案可以在各种设备上顺利运行吗?

最佳答案

这些图片可能太大了。将它们转换为 .PNG 的事实对您没有帮助,因为磁盘上的大小与消耗的内存大小无关。图像作为位图对象加载。假设您有一张尺寸为 1,000 x 1,000 像素的图像。如果位图配置为 ARGB_8888,则每个像素为 4 个字节。计算一下,该图像的大小为 4 MB。将其乘以 30 张图像,突然间您需要 120 MB 的内存。

要么缩小图像的分辨率,要么以缩小的比例加载它们。

largeHeap=true 是解决实际问题的创可贴。

关于android - 通过 AnimationDrawable 播放动画时在运行时释放内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20784698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com