Android ViewFlipper 立即跟随手势

发布于 2024-10-29 12:08:42 字数 129 浏览 0 评论 0原文

我在 ViewFlipper 上使用 GestureDetector 来实现 showPrevious 和 showNext。 一切都很好。但是,我想要 ViewFlipper 动画立即跟随手指手势的效果,而不是在手势完成后。 有什么建议吗?

I’m using GestureDetector on ViewFlipper to implement showPrevious and showNext.
Everything is fine. However, I wanna have the effect that ViewFlipper animation follows finger gesture right away, instead of after gesture is done.
Any advise?

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

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

发布评论

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

评论(1

熟人话多 2024-11-05 12:08:42

据我所知你必须手动完成。下面的代码应该为您指明正确的方向。您必须添加动态动画,以便视图在翻转过程中不会闪烁。根据视图翻转器中的内容,您还需要做更多事情,但它应该可以帮助您入门。

public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
        {
            //Gets the startpoint for you finger
            pressDownPoint = event.getX();

            //Gets how many view there is in the viewflipper
            flipperViewCount = flipper.getChildCount();

            //Checks if there is a view to the left of the current view
//if there is, it positions it to the left of the current view
            if (flipper.getDisplayedChild() > 0)
            {
                View leftChild = flipper.getChildAt(flipper.getDisplayedChild() - 1);
                //You must set the left view to invisible or visible 
//or it will not move to the position you tell it
                leftChild.setVisibility(View.INVISIBLE);
                leftChild.layout(-screenWidth, 
                        leftChild.getTop(), 0, 
                        leftChild.getBottom());
            }

            //Same as above but for the view to the right
            if (flipper.getDisplayedChild() < flipperViewCount - 1)
            {
                View rightChild = flipper.getChildAt(flipper.getDisplayedChild() + 1);
                rightChild.setVisibility(View.INVISIBLE);
                rightChild.layout(screenWidth, 
                        rightChild.getTop(), screenWidth * 2, 
                        rightChild.getBottom());
            }
            break;
        }
        case MotionEvent.ACTION_UP:
        {
            //Gets the absolute position on the screen
            float releasePoint = event.getRawX();

            //Calculates if the fling is to the right or left
//The screenDensity variable is simply the density of the device
//Have in mind that this will not flipp the viewflipper if you drag
//your finger less than about 0.5cm (depeding on the device)
//In that case you need to make an animation that takes the view back
//to its original position. Else it will just get stuck where you
//let go with your finger.
            if (Math.abs(pressDownPoint - releasePoint) / screenDensity > 30)
            {
                if (pressDownPoint > releasePoint)
                {
                    myAnimLeft(); //Method with your animation
                    flipper.showNext();
                }
                else
                {
                    myAnimRight();
                    flipper.showPrevious();
                }
            }
            break;
        }
        case MotionEvent.ACTION_MOVE:
        {
            View currentView = flipper.getCurrentView();

            //Moves the current view
//screenWidth is based on the current devices screen width
            currentView.layout((int)(event.getRawX() - pressDownPoint), 
                    currentView.getTop(), (int)(event.getRawX() - pressDownPoint) + screenWidth, 
                    currentView.getBottom());

            //Moves the view to the left if there is one
            if (flipper.getDisplayedChild() > 0)
            {
                View leftChild = flipper.getChildAt(flipper.getDisplayedChild() - 1);
                leftChild.layout((int)(event.getRawX() - pressDownPoint - screenWidth), 
                        leftChild.getTop(), (int)(event.getRawX() - pressDownPoint), 
                        leftChild.getBottom());

                //Sets the left view to visible so it shows
                if (leftChild.getVisibility() == View.INVISIBLE)
                {
                    leftChild.setVisibility(View.VISIBLE);
                }
            }

            //Same as above but for the view to the right
            if (flipper.getDisplayedChild() < flipperViewCount - 1)
            {
                View rightChild = flipper.getChildAt(flipper.getDisplayedChild() + 1);
                rightChild.layout((int)(event.getRawX() - pressDownPoint + screenWidth), 
                        rightChild.getTop(), (int)(event.getRawX() - pressDownPoint + (screenWidth * 2)), 
                        rightChild.getBottom());

                if (rightChild.getVisibility() == View.INVISIBLE)
                {
                    rightChild.setVisibility(View.VISIBLE);
                }
            }
        }

    }
    return true;
}

As far as I know you have to do it manually. The code below should point you into the right direction. You have to add dynamic animations so the view don't flicker during the flipp. There is more you need to do, depending of the content in the viewflipper, but it should get you started.

public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
        {
            //Gets the startpoint for you finger
            pressDownPoint = event.getX();

            //Gets how many view there is in the viewflipper
            flipperViewCount = flipper.getChildCount();

            //Checks if there is a view to the left of the current view
//if there is, it positions it to the left of the current view
            if (flipper.getDisplayedChild() > 0)
            {
                View leftChild = flipper.getChildAt(flipper.getDisplayedChild() - 1);
                //You must set the left view to invisible or visible 
//or it will not move to the position you tell it
                leftChild.setVisibility(View.INVISIBLE);
                leftChild.layout(-screenWidth, 
                        leftChild.getTop(), 0, 
                        leftChild.getBottom());
            }

            //Same as above but for the view to the right
            if (flipper.getDisplayedChild() < flipperViewCount - 1)
            {
                View rightChild = flipper.getChildAt(flipper.getDisplayedChild() + 1);
                rightChild.setVisibility(View.INVISIBLE);
                rightChild.layout(screenWidth, 
                        rightChild.getTop(), screenWidth * 2, 
                        rightChild.getBottom());
            }
            break;
        }
        case MotionEvent.ACTION_UP:
        {
            //Gets the absolute position on the screen
            float releasePoint = event.getRawX();

            //Calculates if the fling is to the right or left
//The screenDensity variable is simply the density of the device
//Have in mind that this will not flipp the viewflipper if you drag
//your finger less than about 0.5cm (depeding on the device)
//In that case you need to make an animation that takes the view back
//to its original position. Else it will just get stuck where you
//let go with your finger.
            if (Math.abs(pressDownPoint - releasePoint) / screenDensity > 30)
            {
                if (pressDownPoint > releasePoint)
                {
                    myAnimLeft(); //Method with your animation
                    flipper.showNext();
                }
                else
                {
                    myAnimRight();
                    flipper.showPrevious();
                }
            }
            break;
        }
        case MotionEvent.ACTION_MOVE:
        {
            View currentView = flipper.getCurrentView();

            //Moves the current view
//screenWidth is based on the current devices screen width
            currentView.layout((int)(event.getRawX() - pressDownPoint), 
                    currentView.getTop(), (int)(event.getRawX() - pressDownPoint) + screenWidth, 
                    currentView.getBottom());

            //Moves the view to the left if there is one
            if (flipper.getDisplayedChild() > 0)
            {
                View leftChild = flipper.getChildAt(flipper.getDisplayedChild() - 1);
                leftChild.layout((int)(event.getRawX() - pressDownPoint - screenWidth), 
                        leftChild.getTop(), (int)(event.getRawX() - pressDownPoint), 
                        leftChild.getBottom());

                //Sets the left view to visible so it shows
                if (leftChild.getVisibility() == View.INVISIBLE)
                {
                    leftChild.setVisibility(View.VISIBLE);
                }
            }

            //Same as above but for the view to the right
            if (flipper.getDisplayedChild() < flipperViewCount - 1)
            {
                View rightChild = flipper.getChildAt(flipper.getDisplayedChild() + 1);
                rightChild.layout((int)(event.getRawX() - pressDownPoint + screenWidth), 
                        rightChild.getTop(), (int)(event.getRawX() - pressDownPoint + (screenWidth * 2)), 
                        rightChild.getBottom());

                if (rightChild.getVisibility() == View.INVISIBLE)
                {
                    rightChild.setVisibility(View.VISIBLE);
                }
            }
        }

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