2012-05-07 159 views
0

我對android開發非常陌生,並且試圖一點一點地把握它!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

感謝所有幫助!

回答

0

在onCreate()本身和
onAnimationEnd()中開始動畫,你正在做一個單獨的線程。

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

此外,嘗試學習活動的生命週期和方法代表了UI回調並沒有。

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(); 
    } 
+0

酷會試試這個謝謝 –