Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Animation: Wait until finished?
    primarykey
    data
    text
    <p>I would like to wait until an animation is finished* in an Android ImageView before continuing program execution, what is the proper way to do this?</p> <ul> <li>(in this context "finished" means that it runs through all of its frames exactly one time and stops on the last one. I am unclear whether this animation will be an android:oneshot="true" animation because I will be using it multiple times, but it will not be run continuously but intermittently)</li> </ul> <p>Research/Guesses:</p> <p>A. At heart my question seems to be a Java thread question because the Android <a href="http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html" rel="nofollow noreferrer">AnimationDrawable</a> implements <a href="http://developer.android.com/reference/java/lang/Runnable.html" rel="nofollow noreferrer">Java.lang.Runnable</a>. So maybe threads are the solution. Perhaps the answer will include <a href="http://download.oracle.com/javase/tutorial/essential/concurrency/join.html" rel="nofollow noreferrer">join</a>?</p> <p>B. The approach of others seems to have been to use <a href="https://stackoverflow.com/questions/4750939/android-animation-is-not-finished-in-onanimationend">AnimationListener</a>, this seems difficult and needlessly complex for my simple needs. Plus I'm not exactly sure how to do it.</p> <p>C. The <a href="http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html#isRunning%28%29" rel="nofollow noreferrer">AnimationDrawable</a> class has a (boolean) isRunning method which could probably be used in a while loop (i.e. while(anim.isRunning()){wait(100ms)}). But I have a gut feeling that this is the wrong approach. Although something similar seems to be mentioned in the <a href="http://download.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html" rel="nofollow noreferrer">concurrency tutorial</a></p> <p>Code Snippet</p> <pre><code> this.q_pic_view.setImageResource(0); this.q_pic_view.setBackgroundResource(R.drawable.animation_test); AnimationDrawable correct_animation = (AnimationDrawable) this.q_pic_view.getBackground(); correct_animation.start(); //here I tried to implement option C but it didn't work while(correct_animation.isRunning()){ try { Thread.sleep(20); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } </code></pre> <p>Animation</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;animation-list android:id="@+id/AnimTest" android:oneshot="true" xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@drawable/animtest001" android:duration="33"/&gt; &lt;item android:drawable="@drawable/animtest002" android:duration="100"/&gt; &lt;item android:drawable="@drawable/animtest003" android:duration="66"/&gt; &lt;item android:drawable="@drawable/animtest004" android:duration="66"/&gt; &lt;item android:drawable="@drawable/animtest005" android:duration="33"/&gt; &lt;item android:drawable="@drawable/animtest006" android:duration="66"/&gt; &lt;item android:drawable="@drawable/animtest007" android:duration="33"/&gt; &lt;item android:drawable="@drawable/animtest008" android:duration="66"/&gt; &lt;item android:drawable="@drawable/animtest009" android:duration="100"/&gt; &lt;item android:drawable="@drawable/animtest010" android:duration="100"/&gt; &lt;/animation-list&gt; </code></pre> <p>One Possible way to achieve the desired effect, though not an answer to my question, is to delay the execution of further code by doing something like this:</p> <pre><code>int duration = 0; //Add all of the frames together for (int i=0; i&lt;my_animation.getNumberOfFrames(); i++){ duration = duration + correct_animation.getDuration(i); } //delay the execution Handler handler = new Handler(); handler.postDelayed(new Runnable(){ public void run() { DoYourNextStuff(); } }, duration); //delay is here </code></pre> <p>Edit: There are many ways to solve this problem, the answer given probably does solve the problem <em>I didn't test it</em>, but I ended up just waiting the correct amount of time (at first), then I changed to using an async task (which has a handler method for completion) and ran my animation in the updateProgress call of the async task directly. In the last iteration I am using a threaded surface view to run the animation. This last way is by far the fastest and best (for reasons other then those asked about in this post). I hope it helps someone.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload