pref: 删除滑动返回
This commit is contained in:
parent
32394f7b00
commit
0b2e64bcd3
|
|
@ -104,8 +104,6 @@ abstract class BaseActivity : AppCompatActivity(), ExtraRefreshable, CoroutineSc
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
SlideBack.create()
|
|
||||||
.attachToActivity(this)
|
|
||||||
fixBackground()
|
fixBackground()
|
||||||
getDeviceDensity()
|
getDeviceDensity()
|
||||||
INSTANCE.addActivity(this)
|
INSTANCE.addActivity(this)
|
||||||
|
|
@ -304,10 +302,9 @@ abstract class BaseActivity : AppCompatActivity(), ExtraRefreshable, CoroutineSc
|
||||||
recreate()
|
recreate()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if (oldTheme?.contains(ThemeUtil.THEME_TRANSLUCENT) == true && !ThemeUtil.isTranslucentTheme(
|
if (oldTheme.contains(ThemeUtil.THEME_TRANSLUCENT) &&
|
||||||
this
|
!ThemeUtil.isTranslucentTheme(this) || ThemeUtil.isTranslucentTheme(this) &&
|
||||||
) ||
|
!oldTheme.contains(ThemeUtil.THEME_TRANSLUCENT)
|
||||||
ThemeUtil.isTranslucentTheme(this) && oldTheme?.contains(ThemeUtil.THEME_TRANSLUCENT) == false
|
|
||||||
) {
|
) {
|
||||||
recreate()
|
recreate()
|
||||||
return true
|
return true
|
||||||
|
|
|
||||||
|
|
@ -1,134 +0,0 @@
|
||||||
package com.huanchengfly.tieba.post.ui.slideback;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.graphics.Canvas;
|
|
||||||
import android.graphics.Color;
|
|
||||||
import android.graphics.Paint;
|
|
||||||
import android.graphics.Path;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
public class DefaultSlideView implements ISlideView {
|
|
||||||
private final int arrowWidth;
|
|
||||||
private final int width;
|
|
||||||
//private LinearGradient shader;
|
|
||||||
private final int height;
|
|
||||||
private Path bezierPath;
|
|
||||||
private Paint paint, arrowPaint;
|
|
||||||
private int backViewColor = 0xff000000;
|
|
||||||
private int arrowColor = Color.WHITE;
|
|
||||||
|
|
||||||
|
|
||||||
public DefaultSlideView(@NonNull Context context) {
|
|
||||||
width = Utils.d2p(context, 50);
|
|
||||||
height = Utils.d2p(context, 200);
|
|
||||||
arrowWidth = Utils.d2p(context, 4);
|
|
||||||
init(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBackViewColor(int backViewColor) {
|
|
||||||
this.backViewColor = backViewColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setArrowColor(int arrowColor) {
|
|
||||||
this.arrowColor = arrowColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void init(Context context) {
|
|
||||||
bezierPath = new Path();
|
|
||||||
|
|
||||||
paint = new Paint();
|
|
||||||
paint.setAntiAlias(true);
|
|
||||||
paint.setStyle(Paint.Style.FILL);
|
|
||||||
paint.setColor(backViewColor);
|
|
||||||
paint.setStrokeWidth(Utils.d2p(context, 1.5f));
|
|
||||||
|
|
||||||
arrowPaint = new Paint();
|
|
||||||
arrowPaint.setAntiAlias(true);
|
|
||||||
arrowPaint.setStyle(Paint.Style.FILL_AND_STROKE);
|
|
||||||
arrowPaint.setColor(arrowColor);
|
|
||||||
arrowPaint.setStrokeWidth(Utils.d2p(context, 1.5f));
|
|
||||||
arrowPaint.setStrokeCap(Paint.Cap.ROUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean scrollVertical() {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getWidth() {
|
|
||||||
return width;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getHeight() {
|
|
||||||
return height;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDraw(Canvas canvas, float currentWidth, int orientation) {
|
|
||||||
float height = getHeight();
|
|
||||||
int maxWidth = getWidth();
|
|
||||||
float centerY = height / 2;
|
|
||||||
|
|
||||||
float progress = currentWidth / maxWidth;
|
|
||||||
if (progress == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
paint.setColor(backViewColor);
|
|
||||||
paint.setAlpha((int) (200 * progress));
|
|
||||||
|
|
||||||
//画半弧背景
|
|
||||||
/*
|
|
||||||
ps: 小点为起始点和结束点,星号为控制点
|
|
||||||
·
|
|
||||||
|
|
|
||||||
*
|
|
||||||
*
|
|
||||||
|
|
|
||||||
·
|
|
||||||
|
|
|
||||||
*
|
|
||||||
*
|
|
||||||
|
|
|
||||||
·
|
|
||||||
*/
|
|
||||||
|
|
||||||
float bezierWidth = currentWidth / 2;
|
|
||||||
bezierPath.reset();
|
|
||||||
bezierPath.moveTo(0, 0);
|
|
||||||
bezierPath.cubicTo(0, height / 4f, bezierWidth, height * 3f / 8, bezierWidth, centerY);
|
|
||||||
bezierPath.cubicTo(bezierWidth, height * 5f / 8, 0, height * 3f / 4, 0, height);
|
|
||||||
canvas.drawPath(bezierPath, paint);
|
|
||||||
|
|
||||||
|
|
||||||
arrowPaint.setColor(arrowColor);
|
|
||||||
arrowPaint.setAlpha((int) (255 * progress));
|
|
||||||
|
|
||||||
//画箭头
|
|
||||||
float arrowStart, arrowEnd;
|
|
||||||
if (orientation == SlideBackView.ORIENTATION_RIGHT) {
|
|
||||||
arrowStart = currentWidth / 6;
|
|
||||||
arrowEnd = arrowStart + (arrowWidth * (progress - 0.7f) / 0.3f);
|
|
||||||
} else {
|
|
||||||
arrowStart = currentWidth / 6;
|
|
||||||
arrowEnd = arrowStart - (arrowWidth * (progress - 0.7f) / 0.3f);
|
|
||||||
}
|
|
||||||
if (progress <= 0.2) {
|
|
||||||
//ingore
|
|
||||||
} else if (progress <= 0.7f) {
|
|
||||||
//起初变长竖直过程
|
|
||||||
float newProgress = (progress - 0.2f) / 0.5f;
|
|
||||||
canvas.drawLine(arrowStart, centerY - arrowWidth * newProgress, arrowStart, centerY + arrowWidth * newProgress, arrowPaint);
|
|
||||||
} else {
|
|
||||||
//后面变形到完整箭头过程
|
|
||||||
canvas.drawLine(arrowEnd, centerY - arrowWidth, arrowStart, centerY, arrowPaint);
|
|
||||||
canvas.drawLine(arrowStart, centerY, arrowEnd, centerY + arrowWidth, arrowPaint);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
package com.huanchengfly.tieba.post.ui.slideback;
|
|
||||||
|
|
||||||
public interface OnSlide {
|
|
||||||
void onSlideBack();
|
|
||||||
}
|
|
||||||
|
|
@ -1,62 +0,0 @@
|
||||||
package com.huanchengfly.tieba.post.ui.slideback;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
public class SlideBack {
|
|
||||||
private ISlideView slideView; //样式
|
|
||||||
private OnSlide onSlide; //滑动监听
|
|
||||||
private int canSlideWidth; //左边触发距离
|
|
||||||
|
|
||||||
public static SlideBack create() {
|
|
||||||
return new SlideBack();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 滑动返回样式
|
|
||||||
*
|
|
||||||
* @param slideView the slide view
|
|
||||||
* @return the slide back
|
|
||||||
*/
|
|
||||||
public SlideBack slideView(ISlideView slideView) {
|
|
||||||
this.slideView = slideView;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 左边开始触发距离
|
|
||||||
*
|
|
||||||
* @param canSlideWidth the can slide width
|
|
||||||
* @return the slide back
|
|
||||||
*/
|
|
||||||
public SlideBack canSlideWidth(int canSlideWidth) {
|
|
||||||
this.canSlideWidth = canSlideWidth;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 滑动触发监听
|
|
||||||
*
|
|
||||||
* @param onSlide the on slide
|
|
||||||
* @return the slide back
|
|
||||||
*/
|
|
||||||
public SlideBack onSlide(OnSlide onSlide) {
|
|
||||||
this.onSlide = onSlide;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public SlideControlLayout attachToActivity(@NonNull Activity activity) {
|
|
||||||
if (slideView == null) {
|
|
||||||
slideView = new DefaultSlideView(activity);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (canSlideWidth == 0) {
|
|
||||||
canSlideWidth = Utils.d2p(activity, 18);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new SlideControlLayout(activity, canSlideWidth, slideView, onSlide)
|
|
||||||
.attachToActivity(activity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,96 +0,0 @@
|
||||||
package com.huanchengfly.tieba.post.ui.slideback;
|
|
||||||
|
|
||||||
import android.animation.ValueAnimator;
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.graphics.Canvas;
|
|
||||||
import android.view.View;
|
|
||||||
import android.view.animation.DecelerateInterpolator;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
@SuppressLint("ViewConstructor")
|
|
||||||
public class SlideBackView extends View {
|
|
||||||
public static final int ORIENTATION_LEFT = 0;
|
|
||||||
public static final int ORIENTATION_RIGHT = 1;
|
|
||||||
private static final DecelerateInterpolator DECELERATE_INTERPOLATOR = new DecelerateInterpolator();
|
|
||||||
private ISlideView slideView;
|
|
||||||
private ValueAnimator animator;
|
|
||||||
private float rate = 0;//曲线的控制点
|
|
||||||
private int orientation = 0;//滑动方向
|
|
||||||
|
|
||||||
SlideBackView(Context context, @NonNull ISlideView slideView) {
|
|
||||||
super(context);
|
|
||||||
setSlideView(slideView);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ISlideView getSlideView() {
|
|
||||||
return slideView;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SlideBackView setSlideView(@NonNull ISlideView slideView) {
|
|
||||||
this.slideView = slideView;
|
|
||||||
setLayoutParams(new SlideControlLayout.LayoutParams(slideView.getWidth(), slideView.getHeight()));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onDraw(Canvas canvas) {
|
|
||||||
super.onDraw(canvas);
|
|
||||||
slideView.onDraw(canvas, rate, orientation);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateOrientation(int orientation) {
|
|
||||||
this.orientation = orientation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateRate(float updateRate, boolean hasAnim) {
|
|
||||||
if (updateRate > getWidth()) {
|
|
||||||
updateRate = getWidth();
|
|
||||||
}
|
|
||||||
if (rate == updateRate) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
cancelAnim();
|
|
||||||
if (!hasAnim) {
|
|
||||||
rate = updateRate;
|
|
||||||
invalidate();
|
|
||||||
if (rate == 0) {
|
|
||||||
setVisibility(GONE);
|
|
||||||
} else {
|
|
||||||
setVisibility(VISIBLE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
animator = ValueAnimator.ofFloat(rate, updateRate);
|
|
||||||
animator.setDuration(200);
|
|
||||||
animator.addUpdateListener(animation -> {
|
|
||||||
rate = (Float) animation.getAnimatedValue();
|
|
||||||
postInvalidate();
|
|
||||||
if (rate == 0) {
|
|
||||||
setVisibility(GONE);
|
|
||||||
} else {
|
|
||||||
setVisibility(VISIBLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
animator.setInterpolator(DECELERATE_INTERPOLATOR);
|
|
||||||
animator.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void cancelAnim() {
|
|
||||||
if (animator != null && animator.isRunning()) {
|
|
||||||
animator.cancel();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onDetachedFromWindow() {
|
|
||||||
cancelAnim();
|
|
||||||
if (rate != 0) {
|
|
||||||
rate = 0;
|
|
||||||
invalidate();
|
|
||||||
}
|
|
||||||
super.onDetachedFromWindow();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,139 +0,0 @@
|
||||||
package com.huanchengfly.tieba.post.ui.slideback;
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.view.MotionEvent;
|
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.view.ViewParent;
|
|
||||||
import android.widget.FrameLayout;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
@SuppressLint("ViewConstructor")
|
|
||||||
public class SlideControlLayout extends FrameLayout {
|
|
||||||
private final SlideBackView slideBackView;
|
|
||||||
private final OnSlide onSlide;
|
|
||||||
private final int canSlideWidth;
|
|
||||||
private final boolean enable = true;
|
|
||||||
|
|
||||||
private float downX;
|
|
||||||
private float moveX;
|
|
||||||
private boolean startDrag = false;
|
|
||||||
|
|
||||||
SlideControlLayout(@NonNull Context context, int canSlideWidth, ISlideView slideView, OnSlide onSlide) {
|
|
||||||
super(context);
|
|
||||||
this.canSlideWidth = canSlideWidth;
|
|
||||||
this.onSlide = onSlide;
|
|
||||||
slideBackView = new SlideBackView(context, slideView);
|
|
||||||
addView(slideBackView);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
SlideControlLayout attachToActivity(@NonNull Activity activity) {
|
|
||||||
ViewParent parent = getParent();
|
|
||||||
if (parent instanceof ViewGroup) {
|
|
||||||
((ViewGroup) parent).removeView(this);
|
|
||||||
}
|
|
||||||
ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
|
|
||||||
|
|
||||||
decor.addView(this, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onBack() {
|
|
||||||
if (onSlide == null) {
|
|
||||||
Utils.getActivityContext(getContext()).onBackPressed();
|
|
||||||
} else {
|
|
||||||
onSlide.onSlideBack();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void setSlideViewY(SlideBackView view, int y) {
|
|
||||||
if (!view.getSlideView().scrollVertical()) {
|
|
||||||
scrollTo(0, 0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
scrollTo(0, -(y - view.getHeight() / 2));
|
|
||||||
}
|
|
||||||
|
|
||||||
//region 手势控制
|
|
||||||
@Override
|
|
||||||
public boolean onInterceptTouchEvent(MotionEvent motionEvent) {
|
|
||||||
if (!enable) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (motionEvent.getAction()) {
|
|
||||||
case MotionEvent.ACTION_DOWN:
|
|
||||||
if (motionEvent.getRawX() <= canSlideWidth) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (motionEvent.getRawX() >= Utils.getScreenWidth(getContext()) - canSlideWidth) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return super.onInterceptTouchEvent(motionEvent);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onTouchEvent(MotionEvent motionEvent) {
|
|
||||||
if (!enable) {
|
|
||||||
return super.onTouchEvent(motionEvent);
|
|
||||||
}
|
|
||||||
|
|
||||||
float currentX = motionEvent.getRawX();
|
|
||||||
|
|
||||||
switch (motionEvent.getAction()) {
|
|
||||||
case MotionEvent.ACTION_DOWN:
|
|
||||||
float currentY = motionEvent.getRawY();
|
|
||||||
if (currentY > Utils.d2p(getContext(), 100)) {
|
|
||||||
if (currentX <= canSlideWidth) {
|
|
||||||
setRotationY(0f);
|
|
||||||
downX = currentX;
|
|
||||||
startDrag = true;
|
|
||||||
slideBackView.updateRate(0, false);
|
|
||||||
slideBackView.updateOrientation(SlideBackView.ORIENTATION_RIGHT);
|
|
||||||
setSlideViewY(slideBackView, (int) (motionEvent.getRawY()));
|
|
||||||
} else if (currentX >= Utils.getScreenWidth(getContext()) - canSlideWidth) {
|
|
||||||
setRotationY(180f);
|
|
||||||
downX = currentX;
|
|
||||||
startDrag = true;
|
|
||||||
slideBackView.updateRate(0, false);
|
|
||||||
slideBackView.updateOrientation(SlideBackView.ORIENTATION_LEFT);
|
|
||||||
setSlideViewY(slideBackView, (int) (motionEvent.getRawY()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MotionEvent.ACTION_MOVE:
|
|
||||||
if (startDrag) {
|
|
||||||
moveX = currentX - downX;
|
|
||||||
if (Math.abs(moveX) <= slideBackView.getWidth() * 2) {
|
|
||||||
slideBackView.updateRate(Math.abs(moveX) / 2, false);
|
|
||||||
} else {
|
|
||||||
slideBackView.updateRate(slideBackView.getWidth(), false);
|
|
||||||
}
|
|
||||||
setSlideViewY(slideBackView, (int) (motionEvent.getRawY()));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MotionEvent.ACTION_UP:
|
|
||||||
case MotionEvent.ACTION_CANCEL:
|
|
||||||
case MotionEvent.ACTION_OUTSIDE:
|
|
||||||
if (startDrag && Math.abs(moveX) >= slideBackView.getWidth() * 2) {
|
|
||||||
onBack();
|
|
||||||
slideBackView.updateRate(0, false);
|
|
||||||
} else {
|
|
||||||
slideBackView.updateRate(0, startDrag);
|
|
||||||
}
|
|
||||||
moveX = 0;
|
|
||||||
startDrag = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return startDrag || super.onTouchEvent(motionEvent);
|
|
||||||
}
|
|
||||||
//endregion
|
|
||||||
}
|
|
||||||
|
|
@ -1,48 +0,0 @@
|
||||||
package com.huanchengfly.tieba.post.ui.slideback;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.ContextWrapper;
|
|
||||||
import android.graphics.Color;
|
|
||||||
import android.util.DisplayMetrics;
|
|
||||||
import android.util.TypedValue;
|
|
||||||
import android.view.WindowManager;
|
|
||||||
|
|
||||||
import androidx.annotation.ColorInt;
|
|
||||||
|
|
||||||
public class Utils {
|
|
||||||
/**
|
|
||||||
* 屏幕宽度(像素)
|
|
||||||
*/
|
|
||||||
private static int screentwidth;
|
|
||||||
|
|
||||||
@ColorInt
|
|
||||||
static int setColorAlpha(int color, float alpha) {
|
|
||||||
color = Color.argb((int) (alpha * 255), Color.red(color), Color.green(color), Color.blue(color));
|
|
||||||
return color;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int d2p(Context var0, float var1) {
|
|
||||||
DisplayMetrics var2 = var0.getResources().getDisplayMetrics();
|
|
||||||
return (int) TypedValue.applyDimension(1, var1, var2);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getScreenWidth(Context context) {
|
|
||||||
if (screentwidth > 0)
|
|
||||||
return screentwidth;
|
|
||||||
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
|
||||||
DisplayMetrics outMetrics = new DisplayMetrics();
|
|
||||||
wm.getDefaultDisplay().getMetrics(outMetrics);
|
|
||||||
return screentwidth = outMetrics.widthPixels;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Activity getActivityContext(Context context) {
|
|
||||||
if (context == null)
|
|
||||||
return null;
|
|
||||||
else if (context instanceof Activity)
|
|
||||||
return (Activity) context;
|
|
||||||
else if (context instanceof ContextWrapper)
|
|
||||||
return getActivityContext(((ContextWrapper) context).getBaseContext());
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue