我想在繼續執行程序之前等待動畫完成*在Android ImageView中,執行此操作的正確方法是什麼?Android動畫:等到完成?
- (在這種情況下,「完成」意味着它完全貫穿其所有幀並停在最後一幀。我不清楚這個動畫是否爲android:oneshot =「true」動畫,因爲我會多次使用它,但它不會連續運行,但間歇性)
研究/猜測:
A.在心臟,我的問題似乎是一個Java線程的問題,因爲Android的AnimationDrawable工具Java.lang.Runnable。所以也許線程是解決方案。也許答案將包括join?
B.其他人的做法似乎是使用AnimationListener,這似乎是困難和不必要的複雜,我的簡單需求。另外我不確定如何去做。
C. AnimationDrawable類有一個(boolean)isRunning方法,可能在while循環中使用(即while(anim.isRunning()){wait(100ms)})。但我有一種直覺,認爲這是錯誤的做法。雖然類似的事情似乎要提到的concurrency tutorial
代碼段
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();
}
}
動畫
<?xml version="1.0" encoding="utf-8"?>
<animation-list android:id="@+id/AnimTest" android:oneshot="true" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/animtest001" android:duration="33"/>
<item android:drawable="@drawable/animtest002" android:duration="100"/>
<item android:drawable="@drawable/animtest003" android:duration="66"/>
<item android:drawable="@drawable/animtest004" android:duration="66"/>
<item android:drawable="@drawable/animtest005" android:duration="33"/>
<item android:drawable="@drawable/animtest006" android:duration="66"/>
<item android:drawable="@drawable/animtest007" android:duration="33"/>
<item android:drawable="@drawable/animtest008" android:duration="66"/>
<item android:drawable="@drawable/animtest009" android:duration="100"/>
<item android:drawable="@drawable/animtest010" android:duration="100"/>
</animation-list>
一種可能的方式,以達到預期的效果,雖然沒有回答我的問題,是通過做這樣的事情來延遲執行進一步的代碼:
int duration = 0;
//Add all of the frames together
for (int i=0; i<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
編輯:有很多方法來解決這個問題,給出的答案可能確實解決了問題我沒有測試它,但我最終只是等待正確的時間量(起初),然後我改變了使用異步任務(具有完成的處理程序方法),並直接在異步任務的updateProgress調用中運行我的動畫。在最後一次迭代中,我使用螺紋表面視圖來運行動畫。最後一種方式是最快和最好的(因爲在這篇文章中被問到的其他原因)。我希望它能幫助別人。
動畫完成需要多少時間..? – 2011-03-16 05:48:54
CapDroid如果您將上述xml片段中的所有整數相加,您將獲得動畫的總持續時間。但是我正在尋找一個通用的解決方案,以免在這種情況下幫助我。 – tjb 2011-03-16 18:10:48
試試這個Thread.sleep(663); 663 =動畫總時間.. – 2011-03-17 04:57:50