2012-01-30 66 views
1

我已經完成了這些不起作用?如何在android中的onCreate()中啓動動畫?

public class AnimationActivity extends Activity{ 
      private AnimationDrawable yourAnimation; 
      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
       // TODO Auto-generated method stub 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.animation); 
       getIntentValues(); 
       final ImageView imageView = (ImageView) findViewById(R.id.animation_iv); 
       imageView.setBackgroundResource(R.drawable.loadinganim); 
       yourAnimation = (AnimationDrawable) imageView.getBackground(); 
       yourAnimation.start(); 
    } 

RES \抽拉\ loading.xml

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false"> 
    <item android:drawable="@drawable/v1" android:duration="160" /> 
    <item android:drawable="@drawable/v4" android:duration="160" /> 
    <item android:drawable="@drawable/v7" android:duration="160" /> 
    <item android:drawable="@drawable/v8" android:duration="160" /> 
    <item android:drawable="@drawable/v9" android:duration="160" /> 
    <item android:drawable="@drawable/v10" android:duration="160" /> 
    <item android:drawable="@drawable/v11" android:duration="160" /> 
    </animation-list> 

RES \佈局\ animation.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
    > 
<ImageView 
android:id="@+id/animation_iv" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
/> 

動畫開始於ImageView的的onClick()。如果有的話一個人有想法幫助我。

+0

我的代碼或例子對您有用嗎? – Praveenkumar 2012-01-31 04:01:09

回答

3
  view.setImageResource(R.anim.framer); 
      view.post(new Runnable() { 
      public void run() {    
       AnimationDrawable animationDrawable = (AnimationDrawable)view.getDrawable(); 
       animationDrawable.start(); 
      } 
      }); 

這對我工作嘗試!!!!

+0

我怎麼能在幾秒鐘後停止它 – sai 2012-01-30 12:57:00

+0

它工作正常。但幾秒鐘後,我想轉到下一個活動。 – sai 2012-01-30 13:06:31

+0

我不明白你的意思,幾秒鐘後去下一個活動,但要停止它,你可以使用animationDrawable.stop(); – 2012-02-01 05:54:12

1

似乎在onCreate方法運行時Drawable沒有完全初始化,因此啓動動畫將不起作用。

onWindowFocusChanged方法開始動畫。這可悲的只記錄在Animation Drawable Guide而不是相應的Java文檔中。

我在這個問題上提交了一個Documentation Bug。如果您希望Android團隊改進文檔,請將問題列出。

0

好像你在做錯cast.Your背景是StateListDrawable,而不是AnimationDrawable。有類似的問題here。希望它會有所幫助。

2

是的,你的方法是正確的,但是,你需要實現這一點 -

public void onWindowFocusChanged (boolean hasFocus) 
{ 
    super.onWindowFocusChanged(hasFocus); 
    AnimationDrawable yourAnimation = 
     (AnimationDrawable) animation.getBackground(); 
    if(hasFocus) { 
     frameAnimation.start(); 
    } else { 
     frameAnimation.stop(); 
    } 
} 

想在這個example

0

onEnterAnimationComplete()

「活動不能在此期間繪製的窗口動畫爲了知道什麼時候開始繪製是安全的,他們可以重寫這個方法,當輸入的動畫完成時這個方法會被調用。

您可以重寫此方法並在此方法中啓動您的動畫。

+0

這工作對我來說,不知道爲什麼downvoted。 – 2016-11-23 12:09:20