View decorView;
int screenWidth;//屏宽
float startX, startY, endX, endY, distanceX, distanceY;
int pagerselected = 0;
oncrate初始化
//实现右滑退出activity
decorView = getWindow().getDecorView();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (pagerselected == 0) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
endX = event.getX();
endY = event.getY();
distanceX = endX - startX;
distanceY = Math.abs(endY - startY);
//1.判断手势右滑 2.横向滑动的距离要大于竖向滑动的距离
if (endX - startX > 0 && distanceY < distanceX) {
decorView.setX(distanceX);
return true;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
endX = event.getX();
distanceX = endX - startX;
endY = event.getY();
distanceY = Math.abs(endY - startY);
//1.判断手势右滑 2.横向滑动的距离要大于竖向滑动的距离 3.横向滑动距离大于屏幕三分之一才能finish
if (endX - startX > 0 && distanceY < distanceX && distanceX > screenWidth / 3) {
moveOn(distanceX);
}
//1.判断手势右滑 2.横向滑动的距离要大于竖向滑动的距离 但是横向滑动距离不够则返回原位置
else if (endX - startX > 0 && distanceY < distanceX) {
backOrigin(distanceX);
} else {
decorView.setX(0);
}
break;
}
}
return super.dispatchTouchEvent(event);
}
/**
* 返回原点
*
* @param distanceX 横向滑动距离
*/
private void backOrigin(float distanceX) {
ObjectAnimator.ofFloat(decorView, "X", distanceX, 0).setDuration(300).start();
}
/**
* 划出屏幕
*
* @param distanceX 横向滑动距离
*/
private void moveOn(float distanceX) {
ValueAnimator valueAnimator = ValueAnimator.ofFloat(distanceX, screenWidth);
valueAnimator.setDuration(300);
valueAnimator.start();
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
decorView.setX((Float) animation.getAnimatedValue());
}
});
valueAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
finish();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
//封装成一个layout使用
public class InterceptHLayout extends LinearLayout {
private static String TAG = "InterceptHLayout";
public InterceptHLayout(Context context) {
super(context);
init(context);
}
public InterceptHLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
public InterceptHLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}}
int screenWidth;//屏宽
float startX, startY, endX, endY, distanceX, distanceY;
int pagerselected = 0;
private void init(Context context) {
DisplayMetrics metrics = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
}
public void setExitCallBack(ExitCallBack exitCallBack) {
this.exitCallBack = exitCallBack;
}
ExitCallBack exitCallBack;
float startXi, startYi, endXi, endYi, distanceXi, distanceYi;
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (pagerselected == 0) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
endX = event.getX();
endY = event.getY();
distanceX = endX - startX;
distanceY = Math.abs(endY - startY);
//1.判断手势右滑 2.横向滑动的距离要大于竖向滑动的距离
if (endX - startX > 0 && distanceY < distanceX) {
return true;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
break;
}
}
return super.onInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (pagerselected == 0) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = event.getX();
startY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
endX = event.getX();
endY = event.getY();
distanceX = endX - startX;
distanceY = Math.abs(endY - startY);
//1.判断手势右滑 2.横向滑动的距离要大于竖向滑动的距离
if (endX - startX > 0 && distanceY < distanceX) {
Log.i(TAG, "onTouch: 右滑");
this.setX(distanceX);
return true;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
endX = event.getX();
distanceX = endX - startX;
endY = event.getY();
distanceY = Math.abs(endY - startY);
//1.判断手势右滑 2.横向滑动的距离要大于竖向滑动的距离 3.横向滑动距离大于屏幕三分之一才能finish
if (endX - startX > 0 && distanceY < distanceX && distanceX > screenWidth / 3) {
moveOn(distanceX);
}
//1.判断手势右滑 2.横向滑动的距离要大于竖向滑动的距离 但是横向滑动距离不够则返回原位置
else if (endX - startX > 0 && distanceY < distanceX) {
backOrigin(distanceX);
} else {
this.setX(0);
}
break;
}
}
return super.onTouchEvent(event);
}
/**
* 返回原点
*
* @param distanceX 横向滑动距离
*/
private void backOrigin(float distanceX) {
ObjectAnimator.ofFloat(this, "X", distanceX, 0).setDuration(300).start();
}
/**
* 划出屏幕
*
* @param distanceX 横向滑动距离
*/
private void moveOn(float distanceX) {
ValueAnimator valueAnimator = ValueAnimator.ofFloat(distanceX, screenWidth);
valueAnimator.setDuration(300);
valueAnimator.start();
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
InterceptHLayout.this.setX((Float) animation.getAnimatedValue());
}
});
valueAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
if (exitCallBack != null) {
exitCallBack.exit();
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
public interface ExitCallBack {
void exit();
}
//终极方案:使用系统的SwipeDismissLayout