2016-09-14 47 views
1

我想在啓動活動後立即開始動畫。一旦動畫結束,我想開始另一項活動。所以我搜索了很多,每個人都推薦使用onAnimationEnd()。但是,在運行代碼時,動畫結束後不會顯示新的活動。有人能指出我的錯誤嗎?如何在動畫結束後更改活動?動畫監聽器似乎並不工作

這裏是我的MainActivity.java

public class MainActivity extends AppCompatActivity implements Animation.AnimationListener { 
TextView ticTacToe; 
Animation animation; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ticTacToe = (TextView) findViewById(R.id.tictactoe); 
    //bounce is the xml animation file 
    animation= AnimationUtils.loadAnimation(getApplicationContext(), 
      R.anim.bounce); 
} 

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 
    super.onWindowFocusChanged(hasFocus); 
    if (hasFocus) 
     ticTacToe.startAnimation(animation); 
} 

@Override 
public void onAnimationStart(Animation animation) { 

} 

@Override 
public void onAnimationEnd(Animation animation) { 
    Intent intent = new Intent(this,Main2Activity.class); 
    startActivity(intent); 
} 

@Override 
public void onAnimationRepeat(Animation animation) { 

} 
} 
+0

是你的開始動畫是否工作? –

+0

是的。它工作正常 –

回答

1

這樣做是你的onWindowFocusChanged,因爲你的動畫開始時,你的當前窗口獲得焦點,但聽衆仍沒有連接到您的animation

animation.setAnimationListener(new AnimationListener() { 
     public void onAnimationStart(Animation animation) {} 
     public void onAnimationRepeat(Animation animation) {} 
     public void onAnimationEnd(Animation animation) { 
       Intent intent = new Intent(MainActivity.this,Main2Activity.class); 
       startActivity(intent); 
     } 
    }  

    ticTacToe.startAnimation(animation) 

或者你也可以在oncreate

animation.setAnimationListener(this);

+0

謝謝。有效! –

+0

歡迎buddy.happy coading –