【问题标题】:Android ViewFlipper + homescreen animationAndroid ViewFlipper + 主屏动画
【发布时间】:2010-04-28 18:27:41
【问题描述】:

我正在尝试使用 ViewFlipper 并使其像主屏幕一样(布局将随着您的手指移动)。以Check out this 为例。我想使用仅包含两个子项的 ViewFlipper 来执行此操作,因此根据用户移动手指的方式,应在当前视图的任一侧显示相反的视图。此代码有效,但一次只能用于 1 个方向。这是在 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;
}

是否可以将其设置为在当前视图的两侧显示相同的视图?

【问题讨论】:

  • 既然用户一次只能移动一个方向,为什么不使用 if 语句来查看他们正在移动的方向,然后只显示那一侧的视图?

标签: android animation homescreen viewflipper


【解决方案1】:

感谢隐形直升机

如果有人感兴趣,这里的工作是新代码。

        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 以尝试消除视图的一些闪烁。这有所帮助,但我仍然得到了一点。

【讨论】:

    【解决方案2】:

    我可能没有非常有建设性的建议,但如果你想让它表现得像一个主屏幕,为什么你不想看src of that,并根据你的需要进行修改?

    【讨论】:

      【解决方案3】:

      要消除闪烁,请将setVisibility(View.INVISIBLE) 设置为MotionEvent.ACTION_DOWN。视图上的默认值是“GONE”。这意味着您不能在视图上设置Layout(),除非它是“可见”或“不可见”。所以分三步:

      1. 在视图上将可见性设置为 INVISIBLE
      2. 在视图上设置 Layout()
      3. 将可见性设置为在视图上可见

      【讨论】:

        【解决方案4】:

        如果我理解您的要求,现在应该使用View Pager 实现此效果

        看起来像这样:

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多