Android Custom LineLayout Realization Full Screen Arbitrary Drag Function Sample Code
- 2021-12-04 11:25:10
- OfStack
1. Preface
In the development, there will be requirements to achieve controls in the screen dragging at will, which requires custom View, and then in OnTouchEvent event, processing MotionEvent.ACTION_MOVE event, and then through the coordinate point value to onlayout method, to achieve arbitrary control dragging, the specific code is as follows:
import android.content.Context;
import android.util.AttributeSet;
import android.view.Display;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.LinearLayout;
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);
this.mContext = context;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
mScreenWidth = getScreenWidth(mContext);
mScreenHeight = getScreenHeight(mContext) - getStatusBarHeight();
}
public int getStatusBarHeight() {
int resourceId = mContext.getResources().getIdentifier("status_bar_height", "dimen", "android");
return mContext.getResources().getDimensionPixelSize(resourceId);
}
public int getScreenWidth(Context context) {
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
return display.getWidth();
}
public int getScreenHeight(Context context) {
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
return display.getHeight();
}
private float mDownX;
private float mDownY;
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (this.isEnabled()) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isDrag = false;
mDownX = event.getX();
mDownY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
final float mXdistance = event.getX() - mDownX;
final float mYdistance = event.getY() - mDownY;
int l, r, t, b;
// When the horizontal or vertical sliding distance is greater than 10, Is considered a drag event
if (Math.abs(mXdistance) > 10 || Math.abs(mYdistance) > 10) {
isDrag = true;
l = (int) (getLeft() + mXdistance);
r = l + mWidth;
t = (int) (getTop() + mYdistance);
b = t + mHeight;
// Boundary judgment , Don't let the layout slip 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;
}
// Callback moved coordinate point
if(mLocationListener!=null){
mLocationListener.locationRect((l+r)/2,(t+b)/2);
}
this.layout(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) {
this.mLocationListener = LocationListener;
}
public interface onLocationListener {
void locationRect(float locationX, float locationY);
}
}
2. Use in code
<com.xinrui.guestservice.view.DragLineLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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:id="@+id/input_edt"
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:id="@+id/paint_typeface"
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:id="@+id/paint_fontsize"
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>
</com.xinrui.guestservice.view.DragLineLayout>
3. This allows you to load the xml on the Activity for any drag function
Summarize