92 lines
3.8 KiB
Java
92 lines
3.8 KiB
Java
|
|
package cn.cmvr.projectwarehouse;
|
|||
|
|
|
|||
|
|
import android.content.Context;
|
|||
|
|
import android.graphics.Color;
|
|||
|
|
import android.view.*;
|
|||
|
|
import android.widget.*;
|
|||
|
|
|
|||
|
|
/** Edge-swipe fallback for devices that do not provide predictive-back progress. */
|
|||
|
|
final class EdgeBackLayout extends FrameLayout {
|
|||
|
|
interface Listener { void onBack(); }
|
|||
|
|
private final Listener listener;
|
|||
|
|
private final TextView indicator;
|
|||
|
|
private final int edgeWidth;
|
|||
|
|
private final int touchSlop;
|
|||
|
|
private View content;
|
|||
|
|
private float downX, downY;
|
|||
|
|
private boolean eligible, dragging;
|
|||
|
|
|
|||
|
|
EdgeBackLayout(Context context, Listener listener) {
|
|||
|
|
super(context);
|
|||
|
|
this.listener = listener;
|
|||
|
|
edgeWidth = dp(28);
|
|||
|
|
touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
|
|||
|
|
setClipChildren(false);
|
|||
|
|
indicator = new TextView(context);
|
|||
|
|
indicator.setText("‹");
|
|||
|
|
indicator.setTextSize(34);
|
|||
|
|
indicator.setTextColor(Color.WHITE);
|
|||
|
|
indicator.setGravity(Gravity.CENTER);
|
|||
|
|
indicator.setBackgroundColor(0xcc185bb4);
|
|||
|
|
indicator.setAlpha(0f);
|
|||
|
|
indicator.setVisibility(INVISIBLE);
|
|||
|
|
LayoutParams marker = new LayoutParams(dp(44), dp(56), Gravity.START | Gravity.CENTER_VERTICAL);
|
|||
|
|
marker.leftMargin = dp(8);
|
|||
|
|
addView(indicator, marker);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void setContent(View view) {
|
|||
|
|
if (content != null) removeView(content);
|
|||
|
|
content = view;
|
|||
|
|
addView(content, 0, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override public boolean onInterceptTouchEvent(android.view.MotionEvent event) {
|
|||
|
|
switch (event.getActionMasked()) {
|
|||
|
|
case MotionEvent.ACTION_DOWN:
|
|||
|
|
downX = event.getX(); downY = event.getY();
|
|||
|
|
eligible = downX <= edgeWidth; dragging = false;
|
|||
|
|
break;
|
|||
|
|
case MotionEvent.ACTION_MOVE:
|
|||
|
|
if (eligible) {
|
|||
|
|
float dx = event.getX() - downX, dy = Math.abs(event.getY() - downY);
|
|||
|
|
if (dx > touchSlop && dx > dy * 1.25f) { dragging = true; return true; }
|
|||
|
|
if (dy > touchSlop && dy > Math.abs(dx)) eligible = false;
|
|||
|
|
}
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override public boolean onTouchEvent(MotionEvent event) {
|
|||
|
|
if (!dragging) return false;
|
|||
|
|
float distance = Math.max(0, event.getX() - downX);
|
|||
|
|
if (event.getActionMasked() == MotionEvent.ACTION_MOVE) {
|
|||
|
|
float progress = Math.min(1f, distance / Math.max(dp(120), getWidth() * .28f));
|
|||
|
|
content.setTranslationX(dp(52) * progress);
|
|||
|
|
content.setScaleX(1f - .025f * progress);
|
|||
|
|
content.setScaleY(1f - .025f * progress);
|
|||
|
|
indicator.setVisibility(VISIBLE);
|
|||
|
|
indicator.setAlpha(.25f + .75f * progress);
|
|||
|
|
indicator.setTranslationX(dp(10) * progress);
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
if (event.getActionMasked() == MotionEvent.ACTION_UP || event.getActionMasked() == MotionEvent.ACTION_CANCEL) {
|
|||
|
|
boolean commit = event.getActionMasked() == MotionEvent.ACTION_UP && distance >= Math.max(dp(72), getWidth() * .20f);
|
|||
|
|
reset(commit);
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void reset(boolean commit) {
|
|||
|
|
long duration = commit ? 90 : 160;
|
|||
|
|
content.animate().translationX(0).scaleX(1).scaleY(1).setDuration(duration).start();
|
|||
|
|
indicator.animate().alpha(0).translationX(0).setDuration(duration).withEndAction(() -> indicator.setVisibility(INVISIBLE)).start();
|
|||
|
|
if (commit) { performHapticFeedback(HapticFeedbackConstants.CONTEXT_CLICK); postDelayed(listener::onBack, duration); }
|
|||
|
|
eligible = false; dragging = false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private int dp(int value) { return Math.round(value * getResources().getDisplayMetrics().density); }
|
|||
|
|
}
|