android - 如何使用 onScroll 和 GestureDetector 使 View 跟随我的手指 - Android

标签 android textview gesturedetector onscroll

我有一个 RelativeLayout,中间有一个 TextView。我已使用 SimpleOnGestureListener() 检测 onFling、onDown 和 onScroll 事件。

我希望 TextView 跟随我的手指在屏幕上移动(可以只是在 x 轴上),当我抬起手指时,它会在屏幕外或回到中间(取决于多远)我已经移动了它)。

最佳答案

在这些情况下我通常会这样做。

首先,您的 onScroll 方法应该如下所示:

@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;
}

我们正在修改 leftMargintopMargin 一个相当于滚动距离的量。

接下来,要使 TextView 动画回到其原始位置,您需要在事件为 ACTION_UPACTION_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);
}

然后在 snapBack 方法中我们动画返回 TextView :

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());
        mTextView.startAnimation(animation);
    }
}

应该就可以了!您可以修改 endValueXendValueY 变量来控制当您抬起手指时 TextView 返回的位置。

关于android - 如何使用 onScroll 和 GestureDetector 使 View 跟随我的手指 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9137710/

相关文章:

flutter - 在 TextFormField 上捕获点击事件

Android webview 显示 url 源而不是正常加载 url

android - picasso 图像加载以前缓存的图像

android - 如何在 Android 中发布以下 Json 数组

user-interface - Flutter Gesture detector onTap 问题

flutter - 有没有办法阻止手势在 flutter 中冒泡?

android - 未调用我的 AsyncTask 的 onPreExecute/onPostExecute

android - setText方法导致Android App崩溃

iOS 和 xcode : How to make a allow a user to minimize the keyboard when in Text View field

java - View.INVISIBLE 和 android :invisible 的值之间的区别