-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
ElasticDragDismissFrameLayout.java
290 lines (250 loc) · 11.3 KB
/
ElasticDragDismissFrameLayout.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.plaidapp.ui.widget;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import java.util.ArrayList;
import java.util.List;
import io.plaidapp.R;
import io.plaidapp.util.AnimUtils;
import io.plaidapp.util.ColorUtils;
import io.plaidapp.util.ViewUtils;
/**
* A {@link FrameLayout} which responds to nested scrolls to create drag-dismissable layouts.
* Applies an elasticity factor to reduce movement as you approach the given dismiss distance.
* Optionally also scales down content during drag.
*/
public class ElasticDragDismissFrameLayout extends FrameLayout {
// configurable attribs
private float dragDismissDistance = Float.MAX_VALUE;
private float dragDismissFraction = -1f;
private float dragDismissScale = 1f;
private boolean shouldScale = false;
private float dragElacticity = 0.8f;
// state
private float totalDrag;
private boolean draggingDown = false;
private boolean draggingUp = false;
private List<ElasticDragDismissCallback> callbacks;
public ElasticDragDismissFrameLayout(Context context) {
this(context, null, 0, 0);
}
public ElasticDragDismissFrameLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0, 0);
}
public ElasticDragDismissFrameLayout(Context context, AttributeSet attrs,
int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public ElasticDragDismissFrameLayout(Context context, AttributeSet attrs,
int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
final TypedArray a = getContext().obtainStyledAttributes(
attrs, R.styleable.ElasticDragDismissFrameLayout, 0, 0);
if (a.hasValue(R.styleable.ElasticDragDismissFrameLayout_dragDismissDistance)) {
dragDismissDistance = a.getDimensionPixelSize(R.styleable
.ElasticDragDismissFrameLayout_dragDismissDistance, 0);
} else if (a.hasValue(R.styleable.ElasticDragDismissFrameLayout_dragDismissFraction)) {
dragDismissFraction = a.getFloat(R.styleable
.ElasticDragDismissFrameLayout_dragDismissFraction, dragDismissFraction);
}
if (a.hasValue(R.styleable.ElasticDragDismissFrameLayout_dragDismissScale)) {
dragDismissScale = a.getFloat(R.styleable
.ElasticDragDismissFrameLayout_dragDismissScale, dragDismissScale);
shouldScale = dragDismissScale != 1f;
}
if (a.hasValue(R.styleable.ElasticDragDismissFrameLayout_dragElasticity)) {
dragElacticity = a.getFloat(R.styleable.ElasticDragDismissFrameLayout_dragElasticity,
dragElacticity);
}
a.recycle();
}
public static abstract class ElasticDragDismissCallback {
/**
* Called for each drag event.
*
* @param elasticOffset Indicating the drag offset with elasticity applied i.e. may
* exceed 1.
* @param elasticOffsetPixels The elastically scaled drag distance in pixels.
* @param rawOffset Value from [0, 1] indicating the raw drag offset i.e.
* without elasticity applied. A value of 1 indicates that the
* dismiss distance has been reached.
* @param rawOffsetPixels The raw distance the user has dragged
*/
void onDrag(float elasticOffset, float elasticOffsetPixels,
float rawOffset, float rawOffsetPixels) { }
/**
* Called when dragging is released and has exceeded the threshold dismiss distance.
*/
void onDragDismissed() { }
}
@Override
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
return (nestedScrollAxes & View.SCROLL_AXIS_VERTICAL) != 0;
}
@Override
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
// if we're in a drag gesture and the user reverses up the we should take those events
if (draggingDown && dy > 0 || draggingUp && dy < 0) {
dragScale(dy);
consumed[1] = dy;
}
}
@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
int dxUnconsumed, int dyUnconsumed) {
dragScale(dyUnconsumed);
}
@Override
public void onStopNestedScroll(View child) {
if (Math.abs(totalDrag) >= dragDismissDistance) {
dispatchDismissCallback();
} else { // settle back to natural position
animate()
.translationY(0f)
.scaleX(1f)
.scaleY(1f)
.setDuration(200L)
.setInterpolator(AnimUtils.getFastOutSlowInInterpolator(getContext()))
.setListener(null)
.start();
totalDrag = 0;
draggingDown = draggingUp = false;
dispatchDragCallback(0f, 0f, 0f, 0f);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (dragDismissFraction > 0f) {
dragDismissDistance = h * dragDismissFraction;
}
}
public void addListener(ElasticDragDismissCallback listener) {
if (callbacks == null) {
callbacks = new ArrayList<>();
}
callbacks.add(listener);
}
public void removeListener(ElasticDragDismissCallback listener) {
if (callbacks != null && callbacks.size() > 0) {
callbacks.remove(listener);
}
}
private void dragScale(int scroll) {
if (scroll == 0) return;
totalDrag += scroll;
// track the direction & set the pivot point for scaling
// don't double track i.e. if start dragging down and then reverse, keep tracking as
// dragging down until they reach the 'natural' position
if (scroll < 0 && !draggingUp && !draggingDown) {
draggingDown = true;
if (shouldScale) setPivotY(getHeight());
} else if (scroll > 0 && !draggingDown && !draggingUp) {
draggingUp = true;
if (shouldScale) setPivotY(0f);
}
// how far have we dragged relative to the distance to perform a dismiss
// (0–1 where 1 = dismiss distance). Decreasing logarithmically as we approach the limit
float dragFraction = (float) Math.log10(1 + (Math.abs(totalDrag) / dragDismissDistance));
// calculate the desired translation given the drag fraction
float dragTo = dragFraction * dragDismissDistance * dragElacticity;
if (draggingUp) {
// as we use the absolute magnitude when calculating the drag fraction, need to
// re-apply the drag direction
dragTo *= -1;
}
setTranslationY(dragTo);
if (shouldScale) {
final float scale = 1 - ((1 - dragDismissScale) * dragFraction);
setScaleX(scale);
setScaleY(scale);
}
// if we've reversed direction and gone past the settle point then clear the flags to
// allow the list to get the scroll events & reset any transforms
if ((draggingDown && totalDrag >= 0)
|| (draggingUp && totalDrag <= 0)) {
totalDrag = dragTo = dragFraction = 0;
draggingDown = draggingUp = false;
setTranslationY(0f);
setScaleX(1f);
setScaleY(1f);
}
dispatchDragCallback(dragFraction, dragTo,
Math.min(1f, Math.abs(totalDrag) / dragDismissDistance), totalDrag);
}
private void dispatchDragCallback(float elasticOffset, float elasticOffsetPixels,
float rawOffset, float rawOffsetPixels) {
if (callbacks != null && !callbacks.isEmpty()) {
for (ElasticDragDismissCallback callback : callbacks) {
callback.onDrag(elasticOffset, elasticOffsetPixels,
rawOffset, rawOffsetPixels);
}
}
}
private void dispatchDismissCallback() {
if (callbacks != null && !callbacks.isEmpty()) {
for (ElasticDragDismissCallback callback : callbacks) {
callback.onDragDismissed();
}
}
}
/**
* An {@link ElasticDragDismissCallback} which fades system chrome (i.e. status bar and
* navigation bar) whilst elastic drags are performed and
* {@link Activity#finishAfterTransition() finishes} the activity when drag dismissed.
*/
public static class SystemChromeFader extends ElasticDragDismissCallback {
private final Activity activity;
private final int statusBarAlpha;
private final int navBarAlpha;
private final boolean fadeNavBar;
public SystemChromeFader(Activity activity) {
this.activity = activity;
statusBarAlpha = Color.alpha(activity.getWindow().getStatusBarColor());
navBarAlpha = Color.alpha(activity.getWindow().getNavigationBarColor());
fadeNavBar = ViewUtils.isNavBarOnBottom(activity);
}
@Override
public void onDrag(float elasticOffset, float elasticOffsetPixels,
float rawOffset, float rawOffsetPixels) {
if (elasticOffsetPixels > 0) {
// dragging downward, fade the status bar in proportion
activity.getWindow().setStatusBarColor(ColorUtils.modifyAlpha(activity.getWindow()
.getStatusBarColor(), (int) ((1f - rawOffset) * statusBarAlpha)));
} else if (elasticOffsetPixels == 0) {
// reset
activity.getWindow().setStatusBarColor(ColorUtils.modifyAlpha(
activity.getWindow().getStatusBarColor(), statusBarAlpha));
activity.getWindow().setNavigationBarColor(ColorUtils.modifyAlpha(
activity.getWindow().getNavigationBarColor(), navBarAlpha));
} else if (fadeNavBar) {
// dragging upward, fade the navigation bar in proportion
activity.getWindow().setNavigationBarColor(
ColorUtils.modifyAlpha(activity.getWindow().getNavigationBarColor(),
(int) ((1f - rawOffset) * navBarAlpha)));
}
}
public void onDragDismissed() {
activity.finishAfterTransition();
}
}
}