使用MotionEvent.ACTION_MOVE创建类似主屏幕的ViewFlipper

10

好的,我有一个ViewFlipper,里面有三个嵌套的LinearLayouts。它默认显示第一个。这段代码:

// 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设置布局的位置?

有人给了我此链接,并建议我参考applyTransformation方法以获取如何执行此操作的提示,但我不知道如何重复相同的行为。

2个回答

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

希望这能帮到你。


1

如果我理解您的请求,这个效果现在应该使用视图翻页器来实现

看起来像这样:

enter image description here


这是另一种方法:http://www.androiduipatterns.com/2011/06/android-ui-pattern-workspaces.html - Kurru
忽略第二个链接,View Pager 是正确的做法。 - Kurru

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接