2010-08-05 5 views
2

Je veux faire quelque chose comme l'effet d'écran d'accueil andriod. quand je clique sur l'écran et que je bouge. la vue actuelle bouge et la vue suivante peut également s'afficher.Effet de l'écran d'accueil Android

Maintenant, seul le code peut afficher la vue actuelle.

Merci pour votre aide

La source comme suit:

public class test extends Activity implements OnTouchListener{ 
float downXValue; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Set main.XML as the layout for this Activity 
    setContentView(R.layout.main); 

    // Add these two lines 
    LinearLayout layMain = (LinearLayout) findViewById(R.id.layout_main); 
    layMain.setOnTouchListener((OnTouchListener) this); 

    // Add a few countries to the spinner 
    Spinner spinnerCountries = (Spinner) findViewById(R.id.spinner_country); 
    ArrayAdapter countryArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,new String[] { "Canada", "USA" }); 
    spinnerCountries.setAdapter(countryArrayAdapter); 

} 

public boolean onTouch(View arg0, MotionEvent arg1) { 

    // Get the action that was done on this touch event 
    switch (arg1.getAction()) 
    { 
     case MotionEvent.ACTION_DOWN: 
     { 
      // store the X value when the user's finger was pressed down 
      downXValue = arg1.getX(); 
      break; 
     } 

     case MotionEvent.ACTION_UP: 
     { 
      // Get the X value when the user released his/her finger 
      float currentX = arg1.getX();    

      // going backwards: pushing stuff to the right 
      if (downXValue < currentX) 
      { 
       // Get a reference to the ViewFlipper 
       ViewFlipper vf = (ViewFlipper) findViewById(R.id.details); 
       // Set the animation 
        vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out)); 
        // Flip! 
        vf.showPrevious(); 

      } 

      // going forwards: pushing stuff to the left 
      if (downXValue > currentX) 
      { 
       // Get a reference to the ViewFlipper 
       ViewFlipper vf = (ViewFlipper) findViewById(R.id.details); 
       // Set the animation 
       vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in)); 
        // Flip! 
       vf.showNext(); 
      } 
      break; 
     } 
     case MotionEvent.ACTION_MOVE: 
      ViewFlipper vf = (ViewFlipper) findViewById(R.id.details); 
      final View currentView = vf.getCurrentView(); 
      currentView.layout((int)(arg1.getX() - downXValue), 
        currentView.getTop(), currentView.getRight(), 
        currentView.getBottom()); 
      /*may be do something in thest*/ 

     break; 

    } 

    // if you return false, these actions will not be recorded 
    return true; 
} 

} 

Répondre

0

Vous pouvez utiliser Viewpager. tofeeq ahmad dit:

Viewpager est la meilleure façon de passer entre vue. Il fournit un moyen de faire glisser les vues de gauche à droite et de droite à gauche. Viewpager fournir façon forte de glisser des vues.

Vous devez étendre PagerAdapter puis utilisez Viewpager .Vous pouvez voir snippet et le code source complet dans Guidance on Android, Java and Other mobile Technology.