SoFunction
Updated on 2025-04-09

Sample code for custom LineLayout to implement arbitrary dragging function on the full screen

1. Preface

In development, there is a need to implement control dragging at will on the screen, which requires customizing the View, and then processing the MotionEvent.ACTION_MOVE event in the OnTouchEvent event, and then passing the value to the onlayout method through coordinate points, the control will be implemented to implement arbitrary dragging. The specific code is as follows:

import ;
import ;
import ;
import ;
import ;
import ;

public class DragLineLayout extends LinearLayout {

 private int mWidth;
 private int mHeight;
 private int mScreenWidth;
 private int mScreenHeight;
 private Context mContext;
 private onLocationListener mLocationListener;/*listen to the Rect */
 //Drag or not private boolean isDrag = false;

 public boolean isDrag() {
  return isDrag;
 }

 public DragView(Context context, AttributeSet attrs) {
  super(context, attrs);
   = context;
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  (widthMeasureSpec, heightMeasureSpec);
  mWidth = getMeasuredWidth();
  mHeight = getMeasuredHeight();
  mScreenWidth = getScreenWidth(mContext);
  mScreenHeight = getScreenHeight(mContext) - getStatusBarHeight();
 }

 public int getStatusBarHeight() {
  int resourceId = ().getIdentifier("status_bar_height", "dimen", "android");
  return ().getDimensionPixelSize(resourceId);
 }

 public int getScreenWidth(Context context) {
  WindowManager manager = (WindowManager) context
    .getSystemService(Context.WINDOW_SERVICE);
  Display display = ();
  return ();
 }

 public int getScreenHeight(Context context) {
  WindowManager manager = (WindowManager) context
    .getSystemService(Context.WINDOW_SERVICE);
  Display display = ();
  return ();
 }

 private float mDownX;
 private float mDownY;


 @Override
 public boolean onTouchEvent(MotionEvent event) {
  (event);
  if (()) {
   switch (()) {
    case MotionEvent.ACTION_DOWN:
     isDrag = false;
     mDownX = ();
     mDownY = ();
     break;
    case MotionEvent.ACTION_MOVE:
     final float mXdistance = () - mDownX;
     final float mYdistance = () - mDownY;
     int l, r, t, b;
     // When the horizontal or vertical sliding distance is greater than 10, it is considered a drag event     if ((mXdistance) > 10 || (mYdistance) > 10) {
      isDrag = true;
      l = (int) (getLeft() + mXdistance);
      r = l + mWidth;
      t = (int) (getTop() + mYdistance);
      b = t + mHeight;
      //Border judgment, do not allow the layout to slide out of the interface      if (l < 0) {
       l = 0;
       r = l + mWidth;
      } else if (r > mScreenWidth) {
       r = mScreenWidth;
       l = r - mWidth;
      }
      if (t < 0) {
       t = 0;
       b = t + mHeight;
      } else if (b > mScreenHeight) {
       b = mScreenHeight;
       t = b - mHeight;
      }
      //Coordinate points after callback move      if(mLocationListener!=null){
       ((l+r)/2,(t+b)/2);
      }
      (l, t, r, b);
     }
     break;
    case MotionEvent.ACTION_UP:
     setPressed(false);
     break;
    case MotionEvent.ACTION_CANCEL:
     setPressed(false);
     break;
   }
   return true;
  }
  return false;
 }
 public void setLocationListener(onLocationListener LocationListener) {
   = LocationListener;
 }
 public interface onLocationListener {
  void locationRect(float locationX, float locationY);
 }
}

2. Application in code

< 
 xmlns:andro
 android:layout_width="@dimen/dp_200"
 android:layout_height="@dimen/dp_110"
 android:orientation="vertical">
 <RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="@dimen/dp_50">
 <EditText
  android:
  android:layout_width="match_parent"
  android:layout_height="@dimen/dp_50"
  android:background="@drawable/edit_bg" />
 </RelativeLayout>
 <RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="@dimen/dp_55"
  android:layout_marginTop="@dimen/margin_5"
  android:background="@drawable/paint_bg">

  <TextView
   android:
   android:layout_width="@dimen/dp_50"
   android:layout_height="@dimen/dp_50"
   android:layout_alignParentLeft="true"
   android:layout_alignParentTop="true"
   android:layout_marginTop="@dimen/margin_5"
   android:background="@drawable/main_selector_write"
   android:clickable="true" />

  <TextView
   android:
   android:layout_width="@dimen/dp_50"
   android:layout_height="@dimen/dp_50"
   android:layout_alignParentTop="true"
   android:layout_marginLeft="@dimen/dp_10"
   android:layout_marginTop="@dimen/margin_5"
   android:layout_toRightOf="@id/paint_typeface"
   android:background="@drawable/main_selector_write"
   android:clickable="true" />
 </RelativeLayout>
</>

3. This way, you can load this xml in the Activity to implement any drag function

Summarize

This is the article about the sample code of Android custom LineLayout to implement arbitrary dragging function on the full screen. For more related Android custom LineLayout to implement arbitrary dragging content on the full screen, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!