0

我对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

感谢所有帮助的人!

4

2 回答 2

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();
    }
于 2012-05-07T12:40:27.960 回答
0

在 onCreate() 本身中启动动画,并在
onAnimationEnd()单独的线程中执行此操作。

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

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

于 2012-05-07T12:27:16.420 回答