|
@@ -0,0 +1,139 @@
|
|
|
+package com.cooleshow.base.widgets;
|
|
|
+
|
|
|
+import android.animation.ValueAnimator;
|
|
|
+import android.content.Context;
|
|
|
+import android.util.AttributeSet;
|
|
|
+import android.util.Log;
|
|
|
+import android.view.MotionEvent;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.FrameLayout;
|
|
|
+import android.widget.ImageView;
|
|
|
+
|
|
|
+import com.cooleshow.base.utils.SizeUtils;
|
|
|
+
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+import androidx.annotation.Nullable;
|
|
|
+import androidx.appcompat.widget.AppCompatImageView;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Author by pq, Date on 2022/10/20.
|
|
|
+ */
|
|
|
+public class CustomDragView extends AppCompatImageView {
|
|
|
+
|
|
|
+
|
|
|
+ private int hideSize = 0;
|
|
|
+ private int lastX, lastY;
|
|
|
+ private int mStartX, mStartY = 0;
|
|
|
+ private boolean isMove = false;
|
|
|
+
|
|
|
+ public CustomDragView(Context context) {
|
|
|
+ super(context);
|
|
|
+ }
|
|
|
+
|
|
|
+ public CustomDragView(Context context, @Nullable AttributeSet attrs) {
|
|
|
+ super(context, attrs);
|
|
|
+ }
|
|
|
+
|
|
|
+ public CustomDragView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
|
|
+ super(context, attrs, defStyleAttr);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onTouchEvent(MotionEvent event) {
|
|
|
+ Log.i("pq", "event action:" + event.getAction());
|
|
|
+ int x = (int) event.getRawX();
|
|
|
+ int y = (int) event.getRawY();
|
|
|
+ switch (event.getAction()) {
|
|
|
+ case MotionEvent.ACTION_DOWN:
|
|
|
+ lastX = (int) event.getRawX();
|
|
|
+ lastY = (int) event.getRawY();
|
|
|
+ mStartX = x;
|
|
|
+ mStartY = y;
|
|
|
+ isMove = false;
|
|
|
+ break;
|
|
|
+ case MotionEvent.ACTION_MOVE:
|
|
|
+ int moveX = (int) (event.getRawX() - lastX);
|
|
|
+ int moveY = (int) (event.getRawY() - lastY);
|
|
|
+
|
|
|
+ int top = getTop();
|
|
|
+ int left = getLeft();
|
|
|
+ int right = getRight();
|
|
|
+ int bottom = getBottom();
|
|
|
+ //高度范围
|
|
|
+ if (top < 0) {
|
|
|
+ top = 0;
|
|
|
+ bottom = getHeight();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (bottom >= screenHeight()) {
|
|
|
+ top = screenHeight() - getHeight();
|
|
|
+ bottom = screenHeight();
|
|
|
+ }
|
|
|
+
|
|
|
+ //宽度范围
|
|
|
+ if (left < -hideSize) {
|
|
|
+ left = -hideSize;
|
|
|
+ right = getWidth() - hideSize;
|
|
|
+ }
|
|
|
+ if (right > screenWidth() + hideSize) {
|
|
|
+ left = screenWidth() - getWidth() + hideSize;
|
|
|
+ right = screenWidth() + hideSize;
|
|
|
+ }
|
|
|
+ layout(left + moveX, top + moveY, right + moveX, bottom + moveY);
|
|
|
+
|
|
|
+ lastX = (int) event.getRawX();
|
|
|
+ lastY = (int) event.getRawY();
|
|
|
+
|
|
|
+ float deltaX1 = x - mStartX;
|
|
|
+ float deltaY1 = y - mStartY;
|
|
|
+ Log.i("pq", "deltaX:" + deltaX1 + "--x:" + x + "--mStartX:" + mStartX);
|
|
|
+ Log.i("pq", "deltaY:" + deltaY1 + "--y:" + y + "--mStartY:" + mStartY);
|
|
|
+ if (Math.abs(deltaX1) <= 5 && Math.abs(deltaY1) <= 5) {
|
|
|
+ isMove = false;
|
|
|
+ } else {
|
|
|
+ isMove = true;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case MotionEvent.ACTION_UP:
|
|
|
+ if(isMove){
|
|
|
+ int maxDuration = 500;
|
|
|
+ int duration = 0;
|
|
|
+ int leftLimit = (screenWidth() - getWidth()) / 2;
|
|
|
+ if (getLeft() < leftLimit) {
|
|
|
+ // layout(0,getTop(),getWidth(),getBottom());
|
|
|
+ duration = maxDuration * (getLeft() + hideSize) / (leftLimit + hideSize);
|
|
|
+ animSlide(this, getLeft(), -hideSize, duration);
|
|
|
+ } else {
|
|
|
+ duration = maxDuration * (screenWidth() - getRight() + hideSize) / (leftLimit + hideSize);
|
|
|
+ animSlide(this, getLeft(), screenWidth() - getWidth() + hideSize, 500);
|
|
|
+ // layout(screenWidth()-getWidth(),getTop(),screenWidth(),getBottom());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return isMove ? isMove : super.onTouchEvent(event);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void animSlide(View view, int from, int to, int duration) {
|
|
|
+ ValueAnimator valueAnimator = ValueAnimator.ofInt(from, to);
|
|
|
+ valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
|
|
|
+ @Override
|
|
|
+ public void onAnimationUpdate(ValueAnimator animation) {
|
|
|
+ int viewLeft = (int) animation.getAnimatedValue();
|
|
|
+ layout(viewLeft, getTop(), viewLeft + view.getWidth(), getBottom());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ //为防止溢出边界时,duration时间为负值,做下0判断
|
|
|
+ valueAnimator.setDuration(duration < 0 ? 0 : duration);
|
|
|
+ valueAnimator.start();
|
|
|
+ }
|
|
|
+
|
|
|
+ public int screenWidth() {
|
|
|
+ return getContext().getResources().getDisplayMetrics().widthPixels;
|
|
|
+ }
|
|
|
+
|
|
|
+ public int screenHeight() {
|
|
|
+ return getContext().getResources().getDisplayMetrics().heightPixels - SizeUtils.dp2px(50);
|
|
|
+ }
|
|
|
+}
|