Android - 完成动画结束活动?

时间:2012-05-07 12:13:07

标签: android animation android-activity

我对android开发很新,我正试图一点一点地抓住它!

我正在尝试实现启动画面,并希望它在切换到下一个活动之前执行动画。我一直试图弄清楚它一两个小时但偶然发现了这个网站,所以这就是......这就是我尝试过的,

public class SplashActivity extends Activity {
     // THREAD TO PROCESS SPLASH SCREEN
     private Thread splashThread;  
     // ANIMATION END FIELD
     private boolean ANIMATION_END;
     // CALLED WHEN ACTIVITY IS FIRST CREATED
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         // SET THE SPLASH SCREEN XML LAYOUT
         setContentView(R.layout.splash);
         // GET THE SPLASH IMAGE
         final ImageView logo = (ImageView) findViewById(R.id.splash_logo);
         // LOAD START ANIMATION FOR SPLASH IMAGE
         final Animation slideIn = AnimationUtils.loadAnimation(this, R.anim.slide_in_right);
         // START ANIMATE SPLASH LOGO
         logo.startAnimation(slideIn);
         // END ANIMATION SPLASH LOGO
         final Animation slideOut = AnimationUtils.loadAnimation(this, R.anim.slide_out_left);
         // SET EVENT LISTENER FOR ANIMATION TO FINISH
         slideOut.setAnimationListener(new Animation.AnimationListener(){
             @Override
             public void onAnimationStart(Animation animation) {
             }           
             @Override
             public void onAnimationRepeat(Animation animation) {
             }           
             @Override
             public void onAnimationEnd(Animation animation) {
                  ANIMATION_END = true;
             }
         });
         // SET SPLASHSCREEN THREAD
         splashThread = new Thread() {
             @Override
             public void run() {
                 try {
                     synchronized(this) {
                         // DURATION OF SPLASH SCREEN
                         wait(4000);
                     }
                 } catch (InterruptedException e) {   

                 }  
                 logo.startAnimation(slideOut);
                 // IF ANIMATION HAS ENDED
                 if (ANIMATION_END == true) {
                  finish();
                  // SWITCH FROM SPLASH ACTIVITY TO MENU ACTIVITY
                  startActivity(new Intent(SplashActivity.this, MenuActivity.class));
                 }
            }
       };
       // RUN THE SPLASH THREAD
       splashThread.start();        
   }
   // SPLASH SCREEN TOUCH EVENTS
   @Override
   public boolean onTouchEvent(MotionEvent event) {
      // USER TOUCHES SPLASH SCREEN
      if (event.getAction() == MotionEvent.ACTION_DOWN) {
           synchronized(splashThread) {
                // MAKE SPLASH SCREEN END
                splashThread.notifyAll();
           }
      }
      return true;
  }    
 }

可能不是我所知道的最好的代码,但我正在努力。当徽标滑入其中时,它会在尝试切换到MenuActivity

时突然结束

谢谢所有帮助的人!

2 个答案:

答案 0 :(得分:0)

在onCreate()本身开始你的动画,在onAnimationEnd()中你在另一个线程中做这件事。

finish(); startActivity(new Intent(SplashActivity.this, MenuActivity.class));

此外,尝试学习活动生命周期以及哪种方法代表UI回调,哪些方法不代表。

答案 1 :(得分:0)

private ImageView img;
    private Thread t;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        img = (ImageView)findViewById(R.id.im1);

        t = new Thread(){   
            @Override
            public void run() {
                try {
                    synchronized(this){ 
                        wait(2000);
                    }
                      // Here your code of animation

                } 
                catch(InterruptedException ex){ 
                    ex.printStackTrace();
                }

                Intent i = new Intent(QuizActivity.this,main.class);
                startActivity(i);
            }
        };
            t.start();
    }