I want to display this animation in an Android app:
So I created a <animation-list>
to do so. It uses 46 frames, each one consisting of a 200x200px png
:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/anim_loading_ripple_frame_0" android:duration="45" />
<item android:drawable="@drawable/anim_loading_ripple_frame_1" android:duration="45" />
<item android:drawable="@drawable/anim_loading_ripple_frame_2" android:duration="45" />
<item android:drawable="@drawable/anim_loading_ripple_frame_3" android:duration="45" />
<item android:drawable="@drawable/anim_loading_ripple_frame_4" android:duration="45" />
<item android:drawable="@drawable/anim_loading_ripple_frame_5" android:duration="45" />
...
<item android:drawable="@drawable/anim_loading_ripple_frame_45" android:duration="45" />
</animation-list>
Then I set this as the background
of an ImageView
and start the animation using
AnimationDrawable loadingAnimation =
(AnimationDrawable) loadingAnimationView.getBackground();
loadingAnimation.start();
Everything works perfect, except for the fact that the app sometimes crashes due to an OutOfMemoryException
! It seems that the memory requirements by this kind of animation are quite high, as I've read others experiencing the same issues.
Then I thought I could probably just make each frame into an XML shape
drawable, so I tried experimenting, but I can't figure out how to make the circles change size - they always occupy the whole width of the view.
I tried doing something like this
<item>
<shape android:shape="oval">
<padding android:top="30dp" android:right="30dp" android:bottom="30dp" android:left="30dp" />
<size
android:width="100dp"
android:height="100dp" />
<stroke
android:color="#0F0"
android:width="4dp"/>
</shape>
</item>
And also using a layer-list
to define another shape
in the same XML file but with a different size, thinking that would cause the circle to become smaller, but they both ended up occupying 100% of the view.
Also, I'm not sure if the AnimationDrawable
will actually use less memory if I define the frames in XML as opposed to a series of png
images? Perhaps I'll still run into the same memory usage issue?
What alternatives do I have? If time wasn't an issue, I'd try to work out a custom View, but it seems like a lot of work just for this animation.