Android ViewFlipper +主屏幕动画

发布于 2024-08-30 06:43:30 字数 1519 浏览 5 评论 0原文

我正在尝试使用 ViewFlipper 并使其像主屏幕一样(布局将随着您的手指移动)。 看看这个 举个例子。我想使用仅包含两个子项的 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;
}

是否可以进行设置,以便在当前视图的两侧显示相同的视图?

I am trying to use a ViewFlipper and make it act like the home screen(The layout will move with your finger). Check out this for an example. I want to do this with a ViewFlipper that only contains two children so the opposite view should be shown on either side of the current view depending on which way the user moves their finger. This code works but only for 1 direction at a time. This is in 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());

Which ever of the bottom two lines I put last that direction will work correctly but the other will not.

Here is how I set the leftView and 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;
}

Is it going to be possible to set it up so that the same view is shown on both sides of the current view?

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

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

发布评论

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

评论(4

沫离伤花 2024-09-06 06:43:30

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

Thanks stealthcopter

That worked here is the new code if anyone is interested.

        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());

        }

I also moved the setVisibility() calls to the MotionEvent.ACTION_DOWN in an attempt to get rid of some flickering of the views. This helped but I still get a bit.

雨巷深深 2024-09-06 06:43:30

我可能没有很有建设性的建议,但如果你希望它表现得像主屏幕,为什么你不想看看 src ,并修改它满足您的需求?

I have possibly not very constructive suggestion, but if you want it to behave like a home screen, why you don't want to look at the src of that, and modify it to your needs ?

空宴 2024-09-06 06:43:30

要消除闪烁,请在 MotionEvent.ACTION_DOWN 中设置 setVisibility(View.INVISIBLE)。视图上的默认值为“GONE”。这意味着在视图为“VISIBLE”或“INVISIBLE”之前,您无法在视图上设置 Layout()。因此,分三个步骤:

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

To get rid of the flickering, set setVisibility(View.INVISIBLE) in MotionEvent.ACTION_DOWN. The default value is "GONE" on a view. That means that you can not set Layout() on a view until it's either "VISIBLE" or "INVISIBLE". So in three steps:

  1. Set Visibility to INVISIBLE on the View
  2. Set Layout() on the View
  3. Set Visibility to Visible on the View
霊感 2024-09-06 06:43:30

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

如下所示:

在此处输入图像描述

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

Looks like this:

enter image description here

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