动画结束后如何更改活动?动画监听器似乎没有工作

时间:2016-09-14 21:25:09

标签: android animation

我想在活动启动后立即启动动画。一旦动画结束,我想开始另一个活动。所以我搜索了很多,每个人都建议使用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) {

}
}

1 个答案:

答案 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);