如何使一个视图使用onScroll和GestureDetector按照我的手指 - 机器人 [英] How to make a View follow my finger using onScroll and GestureDetector - Android

查看:163
本文介绍了如何使一个视图使用onScroll和GestureDetector按照我的手指 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在中间一个TextView一个RelativeLayout的。我知道了检测onFling,onDown和onScroll事件使用SimpleOnGestureListener()。

I have a RelativeLayout with a TextView in the middle. I've got it to detect onFling, onDown, and onScroll events using SimpleOnGestureListener().

我想的TextView跟着我的手指在屏幕上(可以只是在X轴),当我举起我的手指就那么动画无论在屏幕或回到中间(取决于多远我感动的话)。

I would like the TextView to follow my finger around the screen (can be just in the x axis), and when I lift my finger for it so animate either out of the screen or back to the middle (depending on how far I've moved it).

我不知道如何可以做到这一点。任何想法?

I have no idea how this can be done. Any ideas?

谢谢!

推荐答案

这就是我通常做在这些情况下。

This is what I normally do in these cases.

首先,你onScroll方法应该是这个样子:

First of all, your onScroll method should look something like this:

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
    // Make sure that mTextView is the text view you want to move around

    if (!(mTextView.getLayoutParams() instanceof MarginLayoutParams))
    {
        return false;
    }

    MarginLayoutParams marginLayoutParams = (MarginLayoutParams) mTextView.getLayoutParams();

    marginLayoutParams.leftMargin = (int) marginLayoutParams.leftMargin - distanceX;
    marginLayoutParams.topMargin = (int) marginLayoutParams.topMargin - distanceY;

    mTextView.requestLayout();

    return true;
}

我们正在修改的 LEFTMARGIN TOPMARGIN 相当于已滚动的距离的量。

We are modifying the leftMargin and topMargin an amount equivalent to the distance that has been scrolled.

接下来,使文本视图动画回到它原来的位置,你需要这样做时,该事件是 ACTION_UP ACTION_CANCEL

Next, to make the text view animate back to its original position you need to do so when the the event is ACTION_UP or ACTION_CANCEL:

@Override
public boolean onTouch(View arg0, MotionEvent event)
{
    if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL)
    {
        snapBack();
    }
    return mScrollDetector.onTouchEvent(event);
}

然后,在折返方法,我们回动画文本视图:

Then in the snapBack method we animate back the text view:

private void snapBack ()
{
    if (mTextView.getLayoutParams() instanceof MarginLayoutParams)
    {
        final MarginLayoutParams marginLayoutParams = (MarginLayoutParams) mTextView.getLayoutParams();

        final int startValueX = marginLayoutParams.leftMargin;
        final int startValueY = marginLayoutParams.topMargin;
        final int endValueX = 0;
        final int endValueY = 0;

        mTextView.clearAnimation();

        Animation animation = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t)
            {
                int leftMarginInterpolatedValue = (int) (startValueX + (endValueX - startValueX) * interpolatedTime);
                marginLayoutParams.leftMargin = leftMarginInterpolatedValue;

                int topMarginInterpolatedValue = (int) (startValueY + (endValueY - startValueY) * interpolatedTime);
                marginLayoutParams.topMargin = topMarginInterpolatedValue;

                mTextView.requestLayout();
            }
        };
        animation.setDuration(200);
        animation.setInterpolator(new DecelerateInterpolator());
        mFrontView.startAnimation(animation);
    }
}

和应该做的!您可以修改 endValueX endValueY 变量来控制,其中,文中观点可以追溯到当你抬起手指。

And that should do! You can modify the endValueX and endValueY variables to control where the text view goes back when you lift your finger.

这篇关于如何使一个视图使用onScroll和GestureDetector按照我的手指 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆