0

What are the limitations of animation-list in Android/Xamarin?

I've tried a simple example with 10 frames, less than 200kb each and I still get Out of Memory errors. I've also tried this library but I get a Bitmap decode error.

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 (only animation trigger code is shown):

    var imgView = FindViewById<ImageView>(Resource.Id.myAnimation);
    AnimationDrawable animation = (AnimationDrawable)imgView.Drawable;
    animation.Start();
user2966445
  • 1,267
  • 16
  • 39

2 Answers2

2

Your 1280 x 1920 images in ANDROID_BITMAP_FORMAT_RGBA_8888 format:

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

So that is a minimum of 96 megabytes for your 10 drawables that have to be in memory in a memory-limited device.

You might want to look at converting those images to a video, or down-sampling them, etc...

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • There's no way to retain the compressed format along with the original size? – user2966445 Jul 18 '16 at 16:59
  • @user2966445 No, the images must be uncompressed in memory to be displayed in your ImageView. This is true across Android, iOS, Windows, etc... there are block compression textures such as PVRTC and DXT when uploading into OpenGL/DirectX contexts, but using OpenGL to flip a few images seems like overkill to me and you have to deal with image artifacts from the texture compression. – SushiHangover Jul 18 '16 at 17:06
  • I tried converting to an MP4 and playing via VIdeoView, but the screen is blank. New question posted here: http://stackoverflow.com/questions/38442186/android-playing-mp4-in-videoview-no-video-but-audio-z-order-doesnt-work/38442271#38442271 Let me know if you have suggestions. – user2966445 Jul 18 '16 at 21:02
1

The 200kb that you see in your Bitmaps size is the compressed size. When drawn in-app, each Bitmap occupies its uncompressed size. Read this answer for more details on this.

This issue is very tricky as it occurs on certain devices and doesn't on many. It depends on the device's policies and how it handles/frees up memory. Xiaomi phones tend to face a lot of these OutOfMemoryErrors in my experience due to their custom OS.

Read the answer I've shared above, it may help you figure something out.

Community
  • 1
  • 1
SlashG
  • 671
  • 4
  • 17