gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:03:54 24 4
gpt4 key购买 nike

好的,我有一个 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;
}
}

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

有人给了我this link并说我应该引用 applyTransformation 方法以获取有关如何执行此操作的提示,但我不知道如何重复相同的行为。

最佳答案

在移动手指的同时移动 View 很容易:

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

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

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

希望对您有所帮助。

关于android - 使用 MotionEvent.ACTION_MOVE 制作像主屏幕一样的 ViewFlipper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2600916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com