Android ViewFlipper + 主屏幕动画

4
我正在尝试使用ViewFlipper,并使其像主屏幕一样移动(布局会随着手指移动而移动)。请查看此处有一个示例。我想要在只包含两个子元素的ViewFlipper中实现这一点,因此对于用户移动手指的方向,当前视图的相反视图应该显示在当前视图的两侧。这段代码可以正常工作,但每次只能沿一个方向移动。这是在onTouchEvent中实现的。
case MotionEvent.ACTION_MOVE: 

leftView.setVisibility(View.VISIBLE);
rightView.setVisibility(View.VISIBLE);
// move the current view to the left or right.
currentView.layout((int) (touchEvent.getX() - oldTouchValue),
    currentView.getTop(),
    (int) (touchEvent.getX() - oldTouchValue) + 320,
    currentView.getBottom());

// place this view just left of the currentView
leftView.layout(currentView.getLeft() - 320, leftView.getTop(),
    currentView.getLeft(), leftView.getBottom());

// place this view just right of the currentView
rightView.layout(currentView.getRight(), rightView.getTop(), 
    currentView.getRight() + 320, rightView.getBottom());

无论我把底部两行中的哪一行放在最后,那个方向就能正确工作,而另一个方向则不能。
这是我设置leftView和rightView的方法:
final View currentView = myFlipper.getCurrentView();
final View leftView, rightView;
if (currentView == meView) {
    Log.d("current layout: ", "me");
    leftView = youView;
    rightView = youView;
} else if (currentView == youView) {
    Log.d("current layout: ", "you");
    leftView = meView;
    rightView = meView;
} else {
    leftView = null;
    rightView = null;
}

是否可以设置使当前视图的两侧显示相同的视图?


1
既然用户一次只能移动一个方向,为什么不使用if语句来判断他们的移动方向,然后只显示该方向上的视图呢? - stealthcopter
4个回答

1

谢谢 stealthcopter

这个有用,如果有人感兴趣的话,这是新代码。

        if (touchEvent.getX() < oldTouchValue){
             // place this view just right of the currentView
            rightView.layout(currentView.getRight(), rightView.getTop(), 
                            currentView.getRight() + 320, rightView.getBottom());

        }else if (touchEvent.getX() > oldTouchValue) {
            // place this view just left of the currentView
            leftView.layout(currentView.getLeft() - 320, leftView.getTop(),
                            currentView.getLeft(), leftView.getBottom());

        }

我还尝试将 setVisibility() 调用移动到 MotionEvent.ACTION_DOWN,以尝试消除视图的一些闪烁。这有所帮助,但仍然存在一些问题。


0

我可能没有非常建设性的建议,但如果你想让它像一个主屏幕一样运行,为什么不看看源代码,并根据自己的需求进行修改呢?


0
为了消除闪烁,需要在 MotionEvent.ACTION_DOWN 中设置 setVisibility(View.INVISIBLE)。视图的默认值是“GONE”。这意味着在视图变为“VISIBLE”或“INVISIBLE”之前,无法对其进行 Layout() 设置。因此,需要分三步进行操作:
  1. 将 View 的 Visibility 设置为 INVISIBLE
  2. 对 View 进行 Layout() 设置
  3. 将 View 的 Visibility 设置为 Visible

0

如果我理解您的请求,这个效果现在应该使用 View Pager 实现

看起来是这样的:

enter image description here


这是另一种方法 http://www.androiduipatterns.com/2011/06/android-ui-pattern-workspaces.html - Kurru

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