简体   繁体   中英

How can I have an animation before I finishing my activity?

My scale/zoom animation won't start with the finish(). I hope there's a way to do it without deleting the said statement because it is necessary not to go back to the current activity.

Here's the code:

package com.capstone.mainmobidyx.filipino;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

import com.capstone.mainmobidyx.R;

public class F_FilipinoYunit1 extends Activity implements OnClickListener {

    Button btnLesson1, btnLesson2, btnLesson3;
    Intent lesson1, lesson2, lesson3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.f_yunit1);

        btnLesson1 = (Button) findViewById(R.id.btnY1Lesson1);
        btnLesson1.setOnClickListener(this);
        btnLesson2 = (Button) findViewById(R.id.btnY1Lesson2);
        btnLesson2.setOnClickListener(this);
        btnLesson3 = (Button) findViewById(R.id.btnY1Lesson3);
        btnLesson3.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        final Animation zoomAnim = AnimationUtils.loadAnimation(this,R.anim.zoom_in);
        v.startAnimation(zoomAnim);

        switch (v.getId()) {

            case R.id.btnY1Lesson1:

                    lesson1 = new Intent(v.getContext(), SX_Lesson01.class);   
                    startActivity(lesson1);
                    finish();
                    break;

            case R.id.btnY1Lesson2:
                    lesson2 = new Intent(v.getContext(),
                    SX_ScienceLesson02Menu.class);
                    startActivity(lesson2);
                    finish();
                     break;

            case R.id.btnY1Lesson3:
                    lesson2 = new Intent(v.getContext(),
                    SX_Lesson03Menu.class);
                    startActivity(lesson2);
                    finish();
                    break;
            }

       }
}

SOLUTION:

Using the method onAnimation end. I can now start the animation before finishing the activity by inserting the codes inside the method.

here is the working code:

@Override
    public void onClick(final View v) {
        // TODO Auto-generated method stub
        final Animation zoomAnim = AnimationUtils.loadAnimation(this,R.anim.zoom_in);    
        zoomAnim.setAnimationListener(new AnimationListener() {

            public void onAnimationEnd(Animation anim) {

                switch (v.getId()) {

                    case R.id.btnY1Lesson1:  
                        lesson1 = new Intent(v.getContext(), SX_Lesson01.class);  
                        startActivity(lesson1);
                        finish();
                        break;

                    case R.id.btnY1Lesson2:
                         lesson2 = new Intent(v.getContext(),
                         SX_ScienceLesson02Menu.class);
                         startActivity(lesson2);
                         finish();
                         break;

                    case R.id.btnY1Lesson3:
                         lesson2 = new Intent(v.getContext(),
                         SX_Lesson03Menu.class);
                         startActivity(lesson2);
                         finish();
                         break;
                  }

               }
            }

            public void onAnimationRepeat(Animation arg0) {}

            public void onAnimationStart(Animation arg0) {}
        });
        v.startAnimation(zoomAnim);

Set an animation end listener on the animation and finish() / startActivity() when the animation has ended :

http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html

example :

final Animation animation = AnimationUtils.loadAnimation(this,R.anim.zoom_in);
animation.setAnimationListener(new AnimationListener() {

    public void onAnimationEnd(Animation anim) {
            // Start your new activity and finish() the current activity here!
        }

    public void onAnimationRepeat(Animation arg0) {}

    public void onAnimationStart(Animation arg0) {}
}
animation.startAnimation()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM