2010-04-08 87 views
10

好吧我有一个ViewFlipper,其中嵌套了三个LinearLayouts。它默认显示第一个。此代码:使用MotionEvent.ACTION_MOVE制作类似主屏幕的ViewFlipper

// 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; 
    } 
} 

...处理视图翻转但动画非常“开/关”。我想知道是否有人能帮我解决最后一部分问题。假设我可以访问LinearLayouts,有没有一种方法可以根据deltaX和deltaY设置布局的位置?

有人给我this link,并说我应该参考applyTransformation方法提示如何做到这一点,但我不知道如何重复这种相同的行为。

回答

10

移动视图,当你移动你的手指是很容易做到:

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

现在,这只是移动当前视图,而不是邻居。我还没有测试,但那些可能可以通过getChildAt获得:

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

希望有所帮助。

+0

好吧,这作品!我可以弄清楚其余部分,谢谢。 – DJTripleThreat 2010-04-13 04:52:03

1

如果我明白你的要求,这种影响现在应该使用View Pager

看起来是这样实现的:

enter image description here

+0

这是另一种方法http://www.androiduipatterns.com/2011/06/android-ui-pattern-workspaces.html – Kurru 2011-10-14 05:34:37

+0

忽略第二个链接,View Pagers是正确的方法来做到这一点 – Kurru 2011-12-05 17:49:03