0

I'm going through the Sams Android Development book, and I put an animation code and a code to move it to the next screen. I tested it on my phone and the AVD and it's not working. Here's the code:

public class QuizSplashActivity extends QuizActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    TextView logo1 = (TextView) findViewById(R.id.TextViewTopTitle);
    Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    logo1.startAnimation(fade1);

    TextView logo2 = (TextView) findViewById(R.id.BottomView1);
    Animation fade3 = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    logo1.startAnimation(fade3);

    Animation spinin= AnimationUtils.loadAnimation(this, R.anim.custom_anim);
    LayoutAnimationController controller = new LayoutAnimationController(spinin);
    TableLayout table = (TableLayout) findViewById(R.id.TableLayout01); {
    for (int i = 0; i < table.getChildCount(); i++) {
        TableRow row = (TableRow) table.getChildAt(i);
        row.setLayoutAnimation(controller);
    }
    }



Animation fade2 = AnimationUtils.loadAnimation(this,  R.anim.custom_anim);
fade2.setAnimationListener(new AnimationListener() {
    public void onAnimationEnd(Animation animation){
        startActivity(new Intent(QuizSplashActivity.this, QuizMenuActivity.class));
        QuizSplashActivity.this.finish();
    }
    public void onAnimationStart(Animation a) { }
    public void onAnimationRepeat(Animation a) { }
});
}



@Override
protected void onPause() {
    super.onPause();
    TextView logo1= (TextView) findViewById(R.id.TextViewTopTitle);
            logo1.clearAnimation();
    TextView logo2= (TextView) findViewById(R.id.BottomView1);
            logo2.clearAnimation();

}
}
`

Please help, I want to move onto the next chapter.

Again, if I run this code, the animation doesn't run and the app doesn't move onto the next screen.

Thanks

4

1 回答 1

0

First of all, onAnimationEnd of fade2 is where you start your next Activity but I don't see you using it anywhere.

I think you've got confused about which views you want to animate and which animations to use here:

    TextView logo1 = (TextView) findViewById(R.id.TextViewTopTitle);
    Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    logo1.startAnimation(fade1); // <----- is this right?

    TextView logo2 = (TextView) findViewById(R.id.BottomView1);
    Animation fade3 = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    logo1.startAnimation(fade3); // <----- and this one?

Also, onAnimationEnd is not reliable. You can take a look at setRepeatCount() and setRepeatMode() and play around with those but there are multiple bugs with them and worse, those bugs vary between Android version.

Instead, you can use a delayed Runnable to do your "post animation" work:

new Handler().postDelayed(new Runnable() {
    public void run() {

        view.clearAnimation();  // <--- whichever view you are animating

        startActivity(new Intent(QuizSplashActivity.this, QuizMenuActivity.class));
        QuizSplashActivity.this.finish();
    }
}, fade2.getDuration());

This runnable will be delayed for however long fade2 is set to animate for. When it fires, it clears the current animation and starts the activity.

Good luck!

于 2013-04-09T16:32:12.760 回答