【问题标题】:Android Animation List - Simple Example throws java.lang.OutOfMemoryErrorAndroid 动画列表 - 简单示例抛出 java.lang.OutOfMemoryError
【发布时间】:2016-07-18 14:37:33
【问题描述】:

Android/Xamarin 中动画列表的限制是什么?

我尝试了一个包含 10 帧的简单示例,每个帧小于 200kb,但仍然出现内存不足错误。我也尝试过this library,但出现位图解码错误。

TestActivity.xaml:

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/myLayout"
        android:clipChildren="false">
          <ImageView
            android:id="@+id/myAnimation"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:adjustViewBounds="true"
            android:src="@anim/TestAnimation"
            android:scaleType="centerInside" />
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:paddingLeft="10dp"
    android:paddingRight="20dp"
    android:paddingTop="170dp"
    android:paddingBottom="100dp"
    android:clipChildren="false"
    android:clipToPadding="false">
  <ImageButton
    android:id="@+id/triggerAnimation"
    android:layout_height="20dp"
    android:layout_width="60dp"
    android:background="@android:color/transparent"
    android:adjustViewBounds="false"
    android:src="@drawable/myButton"
    android:scaleType="fitXY"
    android:padding="0dp"/>
  </LinearLayout>
        </RelativeLayout>

TestAnimation.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/test0000" android:duration="33" />
  <item android:drawable="@drawable/test0001" android:duration="33" />
  <item android:drawable="@drawable/test0002" android:duration="33" />
  <item android:drawable="@drawable/test0003" android:duration="33" />
  <item android:drawable="@drawable/test0004" android:duration="33" />
  <item android:drawable="@drawable/test0005" android:duration="33" />
  <item android:drawable="@drawable/test0006" android:duration="33" />
  <item android:drawable="@drawable/test0007" android:duration="33" />
  <item android:drawable="@drawable/test0008" android:duration="33" />
  <item android:drawable="@drawable/test0009" android:duration="33" />
  <item android:drawable="@drawable/test0010" android:duration="33" />

TestActivity.cs(仅显示动画触发代码):

    var imgView = FindViewById<ImageView>(Resource.Id.myAnimation);
    AnimationDrawable animation = (AnimationDrawable)imgView.Drawable;
    animation.Start();

【问题讨论】:

  • 你的drawables的x/y尺寸是多少?
  • @SushiHangover 1280 x 1920

标签: java c# android xamarin


【解决方案1】:

ANDROID_BITMAP_FORMAT_RGBA_8888 格式的 1280 x 1920 图像:

  • 1280 x 1920 x 10 x(8 位 x 4 (RGBA))= 786,432,000 位 (96MB)。

因此,对于必须位于内存受限设备中的 10 个可绘制对象,至少需要 96 兆字节。

您可能希望将这些图像转换为视频,或对它们进行下采样等...

【讨论】:

  • 压缩后的格式无法与原始大小一起保留?
  • @user2966445 不,图像必须在内存中解压缩才能显示在 ImageView 中。这在 Android、iOS、Windows 等中都是如此......当上传到 OpenGL/DirectX 上下文时,有块压缩纹理,例如 PVRTC 和 DXT,但是使用 OpenGL 翻转一些图像对我来说似乎有点过头了,你必须处理来自纹理压缩的图像伪影。
  • 我尝试转换为 MP4 并通过 VIdeoView 播放,但屏幕为空白。此处发布的新问题:stackoverflow.com/questions/38442186/… 如果您有任何建议,请告诉我。
【解决方案2】:

您在Bitmaps 大小中看到的 200kb 是压缩后的大小。在应用内绘制时,每个Bitmap 占据其未压缩的大小。阅读this 答案以获取更多详细信息。

这个问题非常棘手,因为它会出现在某些设备上,而不会出现在很多设备上。这取决于设备的策略以及它如何处理/释放内存。 小米手机往往会遇到很多这样的OutOfMemoryErrors 根据我的经验,由于他们的自定义操作系统。

阅读我在上面分享的答案,它可能会帮助您解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    相关资源
    最近更新 更多