一、可以拖动的View全局拖动,直接贴代码
public class MainActivity extends Activity {private LinearLayout btn_test;// 控件上一次所处的坐标private float lastX = 0;private float lastY = 0;// 记录控件开始位置private float beginX = 0;private float beginY = 0;// 屏幕的宽度和高度private int screenWidth;private int screenHeight;@SuppressLint("MissingInflatedId")@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获取屏幕的宽度和高度WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);screenWidth = windowManager.getDefaultDisplay().getWidth();screenHeight = windowManager.getDefaultDisplay().getHeight();btn_test = findViewById(R.id.btn_test);//点击事件响应btn_test.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(getApplication(), "点击事件", Toast.LENGTH_SHORT).show();}});//拖动事件响应btn_test.setOnTouchListener(new View.OnTouchListener() {@SuppressLint("ClickableViewAccessibility")@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN://获取点击时x y 轴的数据lastX = event.getRawX();lastY = event.getRawY();// 记录控件开始位置beginX = lastX;beginY = lastY;break;case MotionEvent.ACTION_UP:Log.d("抬起", "up" + event.getX() + ":" + event.getY());// 解决拖拽的时候松手点击事件触发if (Math.abs(lastX - beginX) < 10 && Math.abs(lastY - beginY) < 10){}else{return true;}break;case MotionEvent.ACTION_MOVE://获得x y轴的偏移量int dx = (int) (event.getRawX() - lastX);int dy = (int) (event.getRawY() - lastY);//获得控件上下左右的位置信息,加上我们的偏移量,新得到的位置就是我们控件将要出现的位置int l = v.getLeft() + dx;int b = v.getBottom() + dy;int r = v.getRight() + dx;int t = v.getTop() + dy;//判断四个实际位置,如果有一边已经划出屏幕,那就把这边位置设置为0然后相反的边的位置就设置成控件的高度或者宽度if (l < 0) {l = 0;r = l + v.getWidth();}if (t < 0) {t = 0;b = t + v.getHeight();}if (r > screenWidth) {r = screenWidth;l = r - v.getWidth();}if (b > screenHeight) {b = screenHeight;t = b - v.getHeight();}//然后使用我们view的layout重新在布局中把我们的控件画出来v.layout(l, t, r, b);//并把现在的x y设置给lastx lastylastX = (int) event.getRawX();lastY = (int) event.getRawY();v.postInvalidate();//绘制break;default:break;}return false;}});}
}
二、xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:id="@+id/btn_test"android:layout_marginBottom="@dimen/dp_130"android:layout_marginLeft="@dimen/dp_15"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:layout_width="@dimen/dp_60"android:layout_height="@dimen/dp_60"android:orientation="vertical"android:background="@drawable/ic_launcher_background"tools:ignore="MissingConstraints"><TextViewandroid:layout_marginTop="@dimen/dp_10"android:layout_gravity="center_horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮"android:textColor="#ffffff"android:textSize="@dimen/sp_14" /><TextViewandroid:layout_marginTop="@dimen/dp_0"android:layout_gravity="center_horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="移动/点击"android:textColor="#ffffff"android:textSize="@dimen/sp_12" /></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>