在 Android 应用的屏幕上移动 Textview

Moving Textview around the screen on Android app

我正在尝试在 android 应用程序的屏幕上移动 TextView。但是将 TextView 拖到最终位置后,它会随机移动到其他对立面。


private final class TextViewTouchListener implements View.OnTouchListener {

            @Override
            public boolean onTouch(View view, MotionEvent event) {
                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
                //start dragging the item touched
                view.startDrag(data, shadowBuilder, view, 0);
                return true;
            }
        }


private final class TextViewDragListener implements View.OnDragListener {

            @Override
            public boolean onDrag(View v, DragEvent event) {
                final float x = event.getX();
                final float y =event.getY();

// handling the case when the textview gets dragged out of screen
                leftMargin = Math.min(x, mDisplaySize.x - textview.getWidth());
                topargin = Math.min(y, mDisplaySize.y - textview.getHeight());

                final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) textView.getLayoutParams();
                params.leftMargin = (int) leftMargin;
                params.topMargin = (int) topargin;
                params.rightMargin = 0;
                params.bottomMargin = 0;

            textView.setLayoutParams(params);

                return true;
            }
        }

Seems like I am handling it wrong. Can someone help me what exactly I am doing wrong.

如果你只想在屏幕上移动视图,不需要使用drag/drop;您只需使用 View.OnTouchListenerMotionEvent.ACTION_DOWN & MotionEvent.ACTION_MOVE 事件更新屏幕上的视图 x 和 y 位置。

为了避免将视图移出屏幕,我们将使用其

计算根视图的宽度和高度

因此,您的布局将有一个包含 TextView 的根视图,您想在屏幕上移动它。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World" />

</RelativeLayout>

你的行为将是:

public class MainActivity extends AppCompatActivity {

    private int mXDelta = 0;
    private int mYDelta = 0;
    private int mRootWidth;
    private int mRootHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RelativeLayout rootLayout = findViewById(R.id.root_layout);

        rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    rootLayout.getViewTreeObserver()
                            .removeOnGlobalLayoutListener(this);
                }

                mRootWidth = rootLayout.getWidth();
                mRootHeight = rootLayout.getHeight();
            }
        });

        TextView textView = findViewById(R.id.textview);
        textView.setOnTouchListener(mOnTouchListener);


    }

    View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {

            int xScreenTouch = (int) event.getRawX(); // x location relative to the screen
            int yScreenTouch = (int) event.getRawY(); // y location relative to the screen

            switch (event.getAction() & MotionEvent.ACTION_MASK) {

                case MotionEvent.ACTION_DOWN:
                    RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                    mXDelta = xScreenTouch - lParams.leftMargin;
                    mYDelta = yScreenTouch - lParams.topMargin;
                    break;

                case MotionEvent.ACTION_MOVE:
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                            .getLayoutParams();

                    layoutParams.leftMargin = Math.max(0, Math.min(mRootWidth - view.getWidth(), xScreenTouch - mXDelta));
                    layoutParams.topMargin = Math.max(0, Math.min(mRootHeight - view.getHeight(), yScreenTouch - mYDelta));
                    view.setLayoutParams(layoutParams);
                    break;
            }

            return true;
        }
    };

}

结果