v0.6.0: bubble for Android 0.6.0 — the house look on the Rabbit, and a stretchy pull-to-refresh that pops
Desktop: Signal vibes or travewave, chosen in Preferences → Appearance. Rabbit: white on pure black, 22/28/32 sp text, outlined buttons and dim-edged text boxes, and drawn icons instead of emoji (rabbit.html). Pulling the chat list further once the search box shows stretches a bubble that pops at the mark and starts the check, then a spinner until it's done.
This commit is contained in:
@@ -1,15 +1,25 @@
|
||||
package me.teafry.voipmessages;
|
||||
|
||||
import android.animation.ValueAnimator;
|
||||
import android.content.Context;
|
||||
import android.view.Gravity;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.RectF;
|
||||
import android.view.HapticFeedbackConstants;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.animation.DecelerateInterpolator;
|
||||
import android.view.animation.OvershootInterpolator;
|
||||
import android.widget.AbsListView;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* Pull down on a list that's already at the top to check for new messages.
|
||||
* (The framework has no SwipeRefreshLayout without androidx.)
|
||||
* Pull down on a list that's already at its top (the search box showing)
|
||||
* to check for new messages. The pull is a rubber band: the further you go
|
||||
* the harder it gets. A bubble grows in the gap and stretches, then pops at
|
||||
* the mark, and the check starts right then; let go and the list springs
|
||||
* back to a spinner until the check is done. (The framework has no
|
||||
* SwipeRefreshLayout without androidx.)
|
||||
*/
|
||||
final class PullRefresh extends FrameLayout {
|
||||
interface OnRefresh {
|
||||
@@ -17,29 +27,48 @@ final class PullRefresh extends FrameLayout {
|
||||
}
|
||||
|
||||
private final AbsListView list;
|
||||
private final TextView hint;
|
||||
private final int threshold;
|
||||
private final Bubble bubble;
|
||||
private final int threshold, slop, reach, hold;
|
||||
private float startY = -1;
|
||||
private boolean pulling, refreshing;
|
||||
private boolean pulling, refreshing, popped;
|
||||
private ValueAnimator settle;
|
||||
OnRefresh onRefresh;
|
||||
|
||||
PullRefresh(Context c, AbsListView list) {
|
||||
super(c);
|
||||
this.list = list;
|
||||
threshold = Ui.dp(c, 140);
|
||||
hint = Ui.text(c, "", Ui.T_SMALL, Ui.MUTED);
|
||||
hint.setGravity(Gravity.CENTER);
|
||||
hint.setPadding(0, Ui.dp(c, 16), 0, Ui.dp(c, 16));
|
||||
hint.setVisibility(GONE);
|
||||
threshold = Ui.dp(c, 150);
|
||||
slop = Ui.dp(c, 24);
|
||||
reach = Ui.dp(c, 230); // the most the list ever moves
|
||||
hold = Ui.dp(c, 84); // the gap the spinner sits in
|
||||
bubble = new Bubble(c, this);
|
||||
addView(bubble, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
|
||||
addView(list, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
|
||||
addView(hint, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.TOP));
|
||||
hint.setBackgroundColor(Ui.PANEL);
|
||||
}
|
||||
|
||||
private boolean atTop() {
|
||||
return list.getChildCount() == 0 || (list.getFirstVisiblePosition() == 0 && list.getChildAt(0).getTop() >= list.getPaddingTop());
|
||||
}
|
||||
|
||||
/** How far the list sits below its place: the gap the bubble draws in. */
|
||||
float gap() {
|
||||
return list.getTranslationY();
|
||||
}
|
||||
|
||||
private void moveTo(float y) {
|
||||
list.setTranslationY(y);
|
||||
bubble.invalidate();
|
||||
}
|
||||
|
||||
private void animateTo(float y, boolean springy) {
|
||||
if (settle != null) settle.cancel();
|
||||
settle = ValueAnimator.ofFloat(gap(), y);
|
||||
settle.setDuration(springy ? 420 : 260);
|
||||
settle.setInterpolator(springy ? new OvershootInterpolator(1.6f) : new DecelerateInterpolator());
|
||||
settle.addUpdateListener(a -> moveTo((float) a.getAnimatedValue()));
|
||||
settle.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onInterceptTouchEvent(MotionEvent e) {
|
||||
if (refreshing) return false;
|
||||
@@ -49,8 +78,10 @@ final class PullRefresh extends FrameLayout {
|
||||
pulling = false;
|
||||
break;
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
if (startY >= 0 && e.getY() - startY > Ui.dp(getContext(), 24) && atTop()) {
|
||||
if (startY >= 0 && e.getY() - startY > slop && atTop()) {
|
||||
pulling = true;
|
||||
popped = false;
|
||||
if (settle != null) settle.cancel();
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
@@ -63,22 +94,28 @@ final class PullRefresh extends FrameLayout {
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent e) {
|
||||
if (!pulling) return false;
|
||||
float d = e.getY() - startY;
|
||||
float d = Math.max(0, e.getY() - startY - slop);
|
||||
switch (e.getActionMasked()) {
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
hint.setVisibility(VISIBLE);
|
||||
hint.setText(d > threshold ? "Release to check for messages" : "Pull to check for messages");
|
||||
list.setTranslationY(Math.min(d, threshold * 1.4f) / 2);
|
||||
// Rubber band: easy at first, ever stiffer towards reach.
|
||||
moveTo(reach * (1 - (float) Math.exp(-d / reach)));
|
||||
bubble.progress = Math.min(1f, d / threshold);
|
||||
if (!popped && d >= threshold && onRefresh != null) {
|
||||
popped = true;
|
||||
refreshing = true;
|
||||
bubble.pop();
|
||||
performHapticFeedback(HapticFeedbackConstants.CONFIRM);
|
||||
announceForAccessibility("Checking for messages");
|
||||
onRefresh.refresh();
|
||||
}
|
||||
return true;
|
||||
case MotionEvent.ACTION_UP:
|
||||
case MotionEvent.ACTION_CANCEL:
|
||||
pulling = false;
|
||||
list.animate().translationY(0).setDuration(150).start();
|
||||
if (d > threshold && e.getActionMasked() == MotionEvent.ACTION_UP && onRefresh != null) {
|
||||
setRefreshing(true);
|
||||
onRefresh.refresh();
|
||||
} else {
|
||||
hint.setVisibility(GONE);
|
||||
if (refreshing) animateTo(hold, false);
|
||||
else {
|
||||
bubble.progress = 0;
|
||||
animateTo(0, true);
|
||||
}
|
||||
return true;
|
||||
default:
|
||||
@@ -87,8 +124,91 @@ final class PullRefresh extends FrameLayout {
|
||||
}
|
||||
|
||||
void setRefreshing(boolean on) {
|
||||
refreshing = on;
|
||||
hint.setText("Checking for messages…");
|
||||
hint.setVisibility(on ? VISIBLE : GONE);
|
||||
if (on || !refreshing) return; // only a pull starts one
|
||||
refreshing = false;
|
||||
if (pulling) return; // still held: it springs back on release
|
||||
bubble.progress = 0;
|
||||
animateTo(0, true);
|
||||
}
|
||||
|
||||
boolean isRefreshing() {
|
||||
return refreshing;
|
||||
}
|
||||
|
||||
/** The bubble in the gap above the list: grows, stretches, pops, then spins. */
|
||||
private static final class Bubble extends View {
|
||||
private final PullRefresh owner;
|
||||
private final Paint fill = new Paint(Paint.ANTI_ALIAS_FLAG), shine = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
private final Paint ring = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||
private final float r, dp;
|
||||
private final RectF oval = new RectF();
|
||||
float progress;
|
||||
private long poppedAt = -1;
|
||||
|
||||
Bubble(Context c, PullRefresh owner) {
|
||||
super(c);
|
||||
this.owner = owner;
|
||||
dp = Ui.dp(c, 1);
|
||||
r = 20 * dp;
|
||||
fill.setColor(Ui.ACCENT);
|
||||
shine.setColor(0xCCFFFFFF);
|
||||
ring.setColor(Ui.TEXT);
|
||||
ring.setStyle(Paint.Style.STROKE);
|
||||
ring.setStrokeWidth(3 * dp);
|
||||
ring.setStrokeCap(Paint.Cap.ROUND);
|
||||
}
|
||||
|
||||
void pop() {
|
||||
poppedAt = System.currentTimeMillis();
|
||||
invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas c) {
|
||||
float gap = owner.gap();
|
||||
if (gap < 2 * dp) {
|
||||
if (!owner.isRefreshing()) poppedAt = -1;
|
||||
return;
|
||||
}
|
||||
float cx = getWidth() / 2f, cy = gap / 2f;
|
||||
long since = poppedAt < 0 ? -1 : System.currentTimeMillis() - poppedAt;
|
||||
if (since < 0) {
|
||||
// Growing for the first half of the pull, then stretching
|
||||
// tall and thin as it nears the mark.
|
||||
float grow = Math.min(1f, progress * 2), stretch = Math.max(0f, progress * 2 - 1);
|
||||
float w = r * grow * (1 - 0.22f * stretch), h = r * grow * (1 + 0.55f * stretch);
|
||||
h = Math.min(h, gap / 2 - 4 * dp);
|
||||
if (w <= 0 || h <= 0) return;
|
||||
oval.set(cx - w, cy - h, cx + w, cy + h);
|
||||
c.drawOval(oval, fill);
|
||||
c.drawCircle(cx - w * 0.38f, cy - h * 0.42f, w * 0.2f, shine);
|
||||
return;
|
||||
}
|
||||
if (since < 320) {
|
||||
// Pop: a ring bursting outwards, and droplets flying off.
|
||||
float t = since / 320f, ease = 1 - (1 - t) * (1 - t);
|
||||
int alpha = (int) (255 * (1 - t));
|
||||
ring.setAlpha(alpha);
|
||||
fill.setAlpha(alpha);
|
||||
c.drawCircle(cx, cy, r * (1 + 0.9f * ease), ring);
|
||||
for (int i = 0; i < 8; i++) {
|
||||
double a = Math.PI * i / 4 + Math.PI / 8;
|
||||
float d = r * (1.2f + 1.6f * ease);
|
||||
c.drawCircle(cx + (float) Math.cos(a) * d, cy + (float) Math.sin(a) * d, 4 * dp * (1 - 0.6f * t), fill);
|
||||
}
|
||||
ring.setAlpha(255);
|
||||
fill.setAlpha(255);
|
||||
postInvalidateOnAnimation();
|
||||
return;
|
||||
}
|
||||
if (owner.isRefreshing()) {
|
||||
// Checking: a turning arc.
|
||||
float spin = ((since - 320) % 900) / 900f * 360;
|
||||
float sr = 16 * dp;
|
||||
oval.set(cx - sr, cy - sr, cx + sr, cy + sr);
|
||||
c.drawArc(oval, spin, 270, false, ring);
|
||||
postInvalidateOnAnimation();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user