1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package com.xunao.effectdemo.dragview;
- import android.content.Context;
- import android.os.Build;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.widget.RelativeLayout;
- import androidx.annotation.Nullable;
- import androidx.annotation.RequiresApi;
- /**
- * author : 程中强
- * e-mail : 740479946@qq.com
- * date : 2022/8/1013:27
- * desc :
- * version: 1.0
- */
- public class DragView extends RelativeLayout {
- public DragView(Context context) {
- super(context);
- }
- public DragView(Context context, @Nullable AttributeSet attrs) {
- super(context, attrs);
- }
- public DragView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
- @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
- public DragView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
- super(context, attrs, defStyleAttr, defStyleRes);
- }
- private float downX;
- private float downY;
- private long downTime;
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- super.onTouchEvent(event);
- if (this.isEnabled()) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- downX = event.getX();
- downY = event.getY();
- break;
- case MotionEvent.ACTION_MOVE:
- final float xDistance = event.getX() - downX;
- final float yDistance = event.getY() - downY;
- if (xDistance != 0 && yDistance != 0) {
- int l = (int) (getLeft() + xDistance);
- int r = (int) (getRight() + xDistance);
- int t = (int) (getTop() + yDistance);
- int b = (int) (getBottom() + yDistance);
- this.layout(l, t, r, b);
- }
- break;
- case MotionEvent.ACTION_UP:
- setPressed(false);
- break;
- case MotionEvent.ACTION_CANCEL:
- setPressed(false);
- break;
- default:
- break;
- }
- return true;
- }
- return false;
- }
- }
|