2012-08-09 4 views
0

J'ai un ViewFlipper qui contient deux mises en page. Ces deux mises en page ne contient qu'un seul ImageView. Il y a une animation lorsque je fais défiler entre ces deux dispositions, mais pas l'animation triggerd avant que je lève le doigt, cela se produit à cause de cela:ViewFlipper animation

case MotionEvent.ACTION_UP: 
    //do animation 

Comment puis-je modifier mon code pour l'animation est triggerd quand Je tenais mon doigt et glisser de gauche à droite et vice verca?

codesnippet pour faire l'animation:

@Override 
    public boolean onTouchEvent(MotionEvent touchevent) { 
     switch (touchevent.getAction()) 
     { 
      case MotionEvent.ACTION_DOWN: 
      { 
       oldTouchValue = touchevent.getX(); 
       break; 
      } 
      case MotionEvent.ACTION_UP: 
      { 
       //if(this.searchOk==false) return false; 
       float currentX = touchevent.getX(); 
       if (oldTouchValue < currentX) 
       { 
        vf.setInAnimation(inFromLeftAnimation()); 
        vf.setOutAnimation(outToRightAnimation()); 
        vf.showNext(); 
       } 
       if (oldTouchValue > currentX) 
       { 
        vf.setInAnimation(inFromRightAnimation()); 
        vf.setOutAnimation(outToLeftAnimation()); 
        vf.showPrevious(); 
       } 
      break; 
      } 
     } 
     return false; 
    } 

    //for the previous movement 
    public static Animation inFromRightAnimation() { 

     Animation inFromRight = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
     ); 
     inFromRight.setDuration(250); 
     inFromRight.setInterpolator(new AccelerateInterpolator()); 
     return inFromRight; 
     } 
    public static Animation outToLeftAnimation() { 
     Animation outtoLeft = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
     ); 
     outtoLeft.setDuration(250); 
     outtoLeft.setInterpolator(new AccelerateInterpolator()); 
     return outtoLeft; 
     }  
    // for the next movement 
    public static Animation inFromLeftAnimation() { 
     Animation inFromLeft = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
     ); 
     inFromLeft.setDuration(250); 
     inFromLeft.setInterpolator(new AccelerateInterpolator()); 
     return inFromLeft; 
     } 
    public static Animation outToRightAnimation() { 
     Animation outtoRight = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
     ); 
     outtoRight.setDuration(250); 
     outtoRight.setInterpolator(new AccelerateInterpolator()); 
     return outtoRight; 
     } 

Répondre

1

Si je ne me trompe pas, vous avez besoin d'événement ACTION_MOVE. essayez le code ci-dessous. J'espère que cela aide.

if(event.getAction() == MotionEvent.ACTION_DOWN) 
{ 
    isDown = false;    
} 
if(event.getAction() == MotionEvent.ACTION_UP) 
{ 
    isDown = true;   
} 
if(event.getAction() == MotionEvent.ACTION_MOVE && !isDown) 
{ 
    // action you want to perform 
} 

une chose que vous pouvez utiliser est un GestureDetector

événement de mouvement Cadre:

final GestureDetector gestureDetector = new GestureDetector(new MyGestureDetector()); 

    View.OnTouchListener gestureListener = new View.OnTouchListener() 
    { 
     public boolean onTouch(View v, MotionEvent event) 
     { 
      if (gestureDetector.onTouchEvent(event)) 
      { 
       return true; 
      } 
      return false; 
     } 
    }; 

classe GestureListener:

private static final int SWIPE_MIN_DISTANCE = 120; 
private static final int SWIPE_MAX_OFF_PATH = 250; 
private static final int SWIPE_THRESHOLD_VELOCITY = 200; 

class MyGestureDetector extends SimpleOnGestureListener 
{ 
    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
     try 
     { 
      if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
       return false; 
      // right to left swipe 
      if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
      {     
       analogflipper.setInAnimation(slideLeftIn); 
       analogflipper.setOutAnimation(slideLeftOut);      

       analogflipper.showNext(); 


      } 
      else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
      {      
       analogflipper.setInAnimation(slideRightIn); 
       analogflipper.setOutAnimation(slideRightOut); 


       analogflipper.showPrevious(); 

      } 
     } 
     catch (Exception e) 
     { 
      System.out.println("Exception..."); 
     } 
     return false; 
    } 
} 
+0

Cela fonctionne un peu, mais je ne peux passer à la vue suivante une fois. Ex: Je suis dans la première vue, glisse mon doigt la deuxième vue montrera, si mon doigt est toujours sur l'écran, quand j'essaye de faire le geste inverse, glissant de la vue deux pour voir un sans relâcher mon doigt, ceci a gagné ne fonctionne pas. –

+0

Une fois supprimer cette condition &&! IsDown et essayer. J'espère que cela aidera – Braj

+0

Si je le fais, il va changer constamment la vue, très rapidement, quand je tiens mon doigt sur l'écran –