2010-04-08 12 views
10

Ok J'ai un ViewFlipper avec trois LinearLayouts imbriqués à l'intérieur. Il est par défaut de montrer le premier. Ce code:Faire un ViewFlipper comme l'écran d'accueil en utilisant MotionEvent.ACTION_MOVE

// Assumptions in my Activity class: 
// oldTouchValue is a float 
// vf is my view flipper 
@Override 
public boolean onTouchEvent(MotionEvent touchEvent) { 
    switch (touchEvent.getAction()) { 
    case MotionEvent.ACTION_DOWN: { 
     oldTouchValue = touchEvent.getX(); 
     break; 
    } 
    case MotionEvent.ACTION_UP: { 
     float currentX = touchEvent.getX(); 
     if (oldTouchValue < currentX) { 
     vf.setInAnimation(AnimationHelper.inFromLeftAnimation()); 
     vf.setOutAnimation(AnimationHelper.outToRightAnimation()); 
     vf.showNext(); 
     } 
     if (oldTouchValue > currentX) { 
     vf.setInAnimation(AnimationHelper.inFromRightAnimation()); 
     vf.setOutAnimation(AnimationHelper.outToLeftAnimation()); 
     vf.showPrevious(); 
     } 
     break; 
    } 
    case MotionEvent.ACTION_MOVE: { 
     // TODO: Some code to make the ViewFlipper 
     // act like the home screen. 
     break; 
    } 
    } 
    return false; 
} 

public static class AnimationHelper { 
    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(350); 
    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(350); 
    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(350); 
    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(350); 
    outtoRight.setInterpolator(new AccelerateInterpolator()); 
    return outtoRight; 
    } 
} 

... gère le retournement de la vue mais les animations sont très "on/off". Je me demande si quelqu'un peut m'aider avec la dernière partie. En supposant que je puisse accéder à LinearLayouts, y a-t-il un moyen de définir la position des layouts basés sur deltaX et deltaY?

Quelqu'un m'a donné this link et a dit que je devrais me référer à la méthode applyTransformation pour des conseils sur la façon de le faire mais je ne sais pas comment répéter ce même comportement.

Répondre

10

Déplacement de la vue pendant que vous vous déplacez votre doigt se fait facilement:

case MotionEvent.ACTION_MOVE: 
    final View currentView = vf.getCurrentView(); 
    currentView.layout((int)(touchEvent.getX() - oldTouchValue), 
      currentView.getTop(), currentView.getRight(), 
      currentView.getBottom()); 
break; 

Maintenant, cela ne déplace le point de vue actuel, pas les voisins. Je ne l'ai pas encore testé, mais ceux-ci peuvent probablement être obtenus par getChildAt:

vf.getChildAt(vf.getDisplayedChild() - 1); // previous 
vf.getChildAt(vf.getDisplayedChild() + 1); // next 

Hope that helps.

+0

Ok, ça marche! Je peux comprendre le reste, merci. – DJTripleThreat

1

Si je comprends votre demande, cet effet devrait maintenant être mis en œuvre en utilisant un View Pager

ressemble à ceci:

enter image description here

+0

Ceci est une autre approche http://www.androiduipatterns.com/2011/06/android-ui-pattern-workspaces.html – Kurru

+0

Ignorer ce 2ème lien, View Pagers sont la bonne façon de le faire – Kurru