|
@@ -3,15 +3,14 @@ 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 android.view.ViewGroup;
|
|
|
|
|
|
+import com.cooleshow.base.utils.LOG;
|
|
|
+import com.cooleshow.base.utils.ScreenUtils;
|
|
|
import com.cooleshow.base.utils.SizeUtils;
|
|
|
|
|
|
-import androidx.annotation.NonNull;
|
|
|
import androidx.annotation.Nullable;
|
|
|
import androidx.appcompat.widget.AppCompatImageView;
|
|
|
|
|
@@ -21,7 +20,8 @@ import androidx.appcompat.widget.AppCompatImageView;
|
|
|
public class CustomDragView extends AppCompatImageView {
|
|
|
|
|
|
|
|
|
- private int hideSize = 0;
|
|
|
+ private int hideSize = -SizeUtils.dp2px(6);
|
|
|
+ private int paddingBottom = SizeUtils.dp2px(10);
|
|
|
private int lastX, lastY;
|
|
|
private int mStartX, mStartY = 0;
|
|
|
private boolean isMove = false;
|
|
@@ -40,7 +40,7 @@ public class CustomDragView extends AppCompatImageView {
|
|
|
|
|
|
@Override
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
|
- Log.i("pq", "event action:" + event.getAction());
|
|
|
+// Log.i("pq", "event action:" + event.getAction());
|
|
|
int x = (int) event.getRawX();
|
|
|
int y = (int) event.getRawY();
|
|
|
switch (event.getAction()) {
|
|
@@ -58,18 +58,29 @@ public class CustomDragView extends AppCompatImageView {
|
|
|
int top = getTop();
|
|
|
int left = getLeft();
|
|
|
int right = getRight();
|
|
|
+
|
|
|
int bottom = getBottom();
|
|
|
+ LOG.i("getBottom:" + bottom);
|
|
|
//高度范围
|
|
|
if (top < 0) {
|
|
|
top = 0;
|
|
|
bottom = getHeight();
|
|
|
}
|
|
|
-
|
|
|
+ LOG.i("screenHeight:" + screenHeight());
|
|
|
if (bottom >= screenHeight()) {
|
|
|
top = screenHeight() - getHeight();
|
|
|
bottom = screenHeight();
|
|
|
}
|
|
|
|
|
|
+ ViewGroup parent = (ViewGroup) getParent();
|
|
|
+ LOG.i(" parent.getBottom():" + parent.getBottom());
|
|
|
+ LOG.i("bottom1:" + bottom);
|
|
|
+ if (bottom > parent.getBottom()) {
|
|
|
+ top = parent.getBottom() - getHeight() - paddingBottom;
|
|
|
+ bottom = parent.getBottom() - paddingBottom;
|
|
|
+ }
|
|
|
+ LOG.i("bottom2:" + bottom);
|
|
|
+
|
|
|
//宽度范围
|
|
|
if (left < -hideSize) {
|
|
|
left = -hideSize;
|
|
@@ -86,8 +97,8 @@ public class CustomDragView extends AppCompatImageView {
|
|
|
|
|
|
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);
|
|
|
+// 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 {
|
|
@@ -95,18 +106,28 @@ public class CustomDragView extends AppCompatImageView {
|
|
|
}
|
|
|
break;
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
- if(isMove){
|
|
|
+ if (isMove) {
|
|
|
int maxDuration = 500;
|
|
|
int duration = 0;
|
|
|
int leftLimit = (screenWidth() - getWidth()) / 2;
|
|
|
if (getLeft() < leftLimit) {
|
|
|
- // layout(0,getTop(),getWidth(),getBottom());
|
|
|
+ // 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());
|
|
|
+ // layout(screenWidth()-getWidth(),getTop(),screenWidth(),getBottom());
|
|
|
+ }
|
|
|
+ ViewGroup parent1 = (ViewGroup) getParent();
|
|
|
+ int limitBottom = parent1.getBottom()-paddingBottom;
|
|
|
+ int limitTop = parent1.getTop()+paddingBottom;
|
|
|
+ if(getBottom()>limitBottom){
|
|
|
+ LOG.i("超出了容器bottom--"+getBottom()+"---limitBottom:"+limitBottom);
|
|
|
+ animSlideVertical(this, getBottom(),limitBottom, 500);
|
|
|
+ }else if(getTop()<limitTop){
|
|
|
+ LOG.i("超出了容器bottom--"+getTop()+"---limitTop:"+limitTop);
|
|
|
+ animSlideVertical(this, getTop()+getHeight(),limitTop+getHeight(), 500);
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
@@ -129,11 +150,55 @@ public class CustomDragView extends AppCompatImageView {
|
|
|
valueAnimator.start();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 垂直方向动画
|
|
|
+ * @param view
|
|
|
+ * @param from
|
|
|
+ * @param to
|
|
|
+ * @param duration
|
|
|
+ */
|
|
|
+ public void animSlideVertical(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 animValue = (int) animation.getAnimatedValue();
|
|
|
+ LOG.i("animValue:"+animValue);
|
|
|
+ layout(getLeft(),animValue-view.getHeight(), getRight(), animValue);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ //为防止溢出边界时,duration时间为负值,做下0判断
|
|
|
+ valueAnimator.setDuration(duration < 0 ? 0 : duration);
|
|
|
+ valueAnimator.start();
|
|
|
+ }
|
|
|
+
|
|
|
+// /**
|
|
|
+// * 垂直方向动画
|
|
|
+// * @param view
|
|
|
+// * @param from
|
|
|
+// * @param to
|
|
|
+// * @param duration
|
|
|
+// */
|
|
|
+// public void animSlideVerticalForTop(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 animValue = (int) animation.getAnimatedValue();
|
|
|
+// LOG.i("animValue:"+animValue);
|
|
|
+// layout(getLeft(),animValue, getRight(), animValue+getHeight());
|
|
|
+// }
|
|
|
+// });
|
|
|
+// //为防止溢出边界时,duration时间为负值,做下0判断
|
|
|
+// valueAnimator.setDuration(duration < 0 ? 0 : duration);
|
|
|
+// valueAnimator.start();
|
|
|
+// }
|
|
|
+
|
|
|
public int screenWidth() {
|
|
|
- return getContext().getResources().getDisplayMetrics().widthPixels;
|
|
|
+ return ScreenUtils.getScreenWidth();
|
|
|
}
|
|
|
|
|
|
public int screenHeight() {
|
|
|
- return getContext().getResources().getDisplayMetrics().heightPixels - SizeUtils.dp2px(50);
|
|
|
+ return ScreenUtils.getScreenHeight() - SizeUtils.dp2px(50);
|
|
|
}
|
|
|
}
|