使用 MotionEvent.ACTION_MOVE 制作像主屏幕一样的 ViewFlipper

发布于 2024-08-28 07:30:14 字数 3020 浏览 8 评论 0原文

好的,我有一个 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 方法来获取有关如何执行此操作的提示,但我不知道如何重复相同的行为。

Ok I have a ViewFlipper with three LinearLayouts nested inside it. It defaults to showing the first one. This code:

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

... handles the view flipping but the animations are very "on/off". I'm wondering if someone can help me out with the last part. Assuming that I can access the LinearLayouts, is there a way where I can set the position of the layouts based on deltaX and deltaY?

Someone gave me this link and said I should refer to the applyTransformation method for hints on how to do this but I don't know how to repeat this same behavior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏了南城 2024-09-04 07:30:14

在移动手指时移动视图很容易完成:

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

希望有帮助。

Moving the view while you're moving your finger is easily done:

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

Now this only moves the current view, not the neighbors. I didn't test that yet, but those can probably be obtained by getChildAt:

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

Hope that helps.

瑕疵 2024-09-04 07:30:14

如果我理解您的请求,现在应该使用 查看寻呼机

如下所示:

在此处输入图像描述

If I understand your request, this effect should now be implemented using a View Pager

Looks like this:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文