2011-12-20 6 views
9

Questa è la mia prima app e ho bisogno di iniziare una nuova attività quando termina l'animazione. Cosa devo fare? Il mio codice:Come iniziare l'attività sull'animazione fine

package com.lineage.goddess; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.WindowManager; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.TextView; 

public class LineageSplashActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 
     startAnimation(); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    } 

    private void startAnimation() { 
     // TODO Auto-generated method stub 
     TextView logo1= (TextView) findViewById(R.id.TextView1); 
     Animation fade1= AnimationUtils.loadAnimation(this, R.anim.fade_in); 
     logo1.startAnimation(fade1); 
     TextView logo2= (TextView) findViewById(R.id.TextView2); 
     Animation fade2= AnimationUtils.loadAnimation(this, R.anim.fade_in); 
     logo2.startAnimation(fade2); 
     TextView logo3= (TextView) findViewById(R.id.TextView3); 
     Animation fade3= AnimationUtils.loadAnimation(this, R.anim.fade_in); 
     logo3.startAnimation(fade3); 
     TextView logo4= (TextView) findViewById(R.id.TextView4); 
     Animation fade4= AnimationUtils.loadAnimation(this, R.anim.fade_in2); 
     logo4.startAnimation(fade4);} 
     public void onAnimationEnd() { 
      Intent i = new Intent(LineageSplashActivity.this, LineageMenuActivity.class); 
      LineageSplashActivity.this.startActivity(i); 
; 
     } 
    } 

risposta

17

Impostare un'AnimationListener per l'animazione che si desidera utilizzare per avviare la vostra attività.

myAnimation.setAnimationListener(new AnimationListener() { 
    public void onAnimationStart(Animation animation) {} 
    public void onAnimationRepeat(Animation animation) {} 
    public void onAnimationEnd(Animation animation) { 
     Intent i = new Intent(LineageSplashActivity.this, LineageMenuActivity.class); 
     LineageSplashActivity.this.startActivity(i); 
    } 
} 

Così, il vostro codice sarà simile a questo:

package com.lineage.goddess; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.WindowManager; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.TextView; 

public class LineageSplashActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 
     startAnimation(); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    } 

    private void startAnimation() { 
     // TODO Auto-generated method stub 
     TextView logo1= (TextView) findViewById(R.id.TextView1); 
     Animation fade1= AnimationUtils.loadAnimation(this, R.anim.fade_in); 
     logo1.startAnimation(fade1); 
     TextView logo2= (TextView) findViewById(R.id.TextView2); 
     Animation fade2= AnimationUtils.loadAnimation(this, R.anim.fade_in); 
     logo2.startAnimation(fade2); 
     TextView logo3= (TextView) findViewById(R.id.TextView3); 
     Animation fade3= AnimationUtils.loadAnimation(this, R.anim.fade_in); 
     logo3.startAnimation(fade3); 
     TextView logo4= (TextView) findViewById(R.id.TextView4); 
     Animation fade4= AnimationUtils.loadAnimation(this, R.anim.fade_in2); 
     face4.setAnimationListener(new AnimationListener() { 
      public void onAnimationStart(Animation animation) {} 
      public void onAnimationRepeat(Animation animation) {} 
      public void onAnimationEnd(Animation animation) { 
       Intent i = new Intent(LineageSplashActivity.this, LineageMenuActivity.class); 
       LineageSplashActivity.this.startActivity(i); 
      } 
     }  

     logo4.startAnimation(fade4);  
    } 
} 
+0

per favore dammi il codice completo ... quando lo pase dopo TextView logo4 = (TextView) findViewById (R.id.TextView4); Animazione fade4 = AnimationUtils.loadAnimation (questo, R.anim.fade_in2); logo4.startAnimation (fade4);} ho: myAnimation non può essere risolto e AnimationListener non può essere risolto con un tipo – user1108339

+0

Dovresti averlo provato tu stesso secondo me. Ad ogni modo, il codice è pubblicato nella mia risposta. – Jordi

+0

thx. è difficile per me: | – user1108339

4

Il codice ha fatto sanguinare il mio occhio, così ho fissato il più che potevo:

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.WindowManager; 
import android.view.animation.Animation; 
import android.view.animation.Animation.AnimationListener; 
import android.view.animation.AnimationUtils; 

public class LineageSplashActivity extends Activity implements AnimationListener { 

    private static final int NUMBER_OF_ANIMATIONS = 4; 
    private int animationFinishedCount = 0; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     startAnimations(); 
    } 

    private void startAnimations() { 
     Animation fade = AnimationUtils.loadAnimation(this, R.anim.fade_in); 
     fade.setAnimationListener(this);  

     findViewById(R.id.TextView1).startAnimation(fade); 
     findViewById(R.id.TextView2).startAnimation(fade); 
     findViewById(R.id.TextView3).startAnimation(fade); 
     findViewById(R.id.TextView4).startAnimation(fade); 
    } 


    @Override 
    public void onAnimationEnd(Animation animation) { 
      // When all animations have finished - start the next activity 
     if(++animationFinishedCount == NUMBER_OF_ANIMATIONS){ 
      Intent intent = new Intent(this, LineageMenuActivity.class); 
      startActivity(intent); 
     } 
    } 

    @Override 
    public void onAnimationStart(Animation animation) { 
     // Nothing 
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 
     // Nothing 
    } 
} 

E se è non è un mis-type e in realtà hai bisogno di un'animazione diversa per la 4a textview, puoi rimuovere il controllo del conteggio e aggiungere il listener dell'animazione a quella singola animazione.