MaterialHeader.java
11.6 KB
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
291
292
293
294
295
296
297
298
299
300
package com.studymachine.www.other;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.widget.ImageView;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import com.studymachine.www.R;
import com.scwang.smart.refresh.header.material.CircleImageView;
import com.scwang.smart.refresh.header.material.MaterialProgressDrawable;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshKernel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;
import com.scwang.smart.refresh.layout.simple.SimpleComponent;
import static android.view.View.MeasureSpec.getSize;
/**
* author : 树朾 & Android 轮子哥
* github : https://github.com/scwang90/SmartRefreshLayout/tree/master/refresh-header-material
* time : 2021/02/28
* desc : Material 风格的刷新球,参考 {@link com.scwang.smart.refresh.header.MaterialHeader}
*/
public final class MaterialHeader extends SimpleComponent implements RefreshHeader {
/** 刷新球大样式 */
public static final int BALL_STYLE_LARGE = 0;
/** 刷新球默认样式 */
public static final int BALL_STYLE_DEFAULT = 1;
protected static final int CIRCLE_BG_LIGHT = 0xFFFAFAFA;
protected static final float MAX_PROGRESS_ANGLE = 0.8f;
protected boolean mFinished;
protected int mCircleDiameter;
protected ImageView mCircleView;
protected MaterialProgressDrawable mProgressDrawable;
protected int mWaveHeight;
protected int mHeadHeight;
protected Path mBezierPath;
protected Paint mBezierPaint;
protected RefreshState mRefreshState;
protected boolean mShowBezierWave = false;
protected boolean mScrollableWhenRefreshing = true;
public MaterialHeader(Context context) {
this(context, null);
}
public MaterialHeader(Context context, AttributeSet attrs) {
super(context, attrs, 0);
mSpinnerStyle = SpinnerStyle.MatchLayout;
setMinimumHeight((int) getResources().getDimension(R.dimen.dp_100));
mProgressDrawable = new MaterialProgressDrawable(this);
mProgressDrawable.setColorSchemeColors(0xff0099cc, 0xffff4444, 0xff669900, 0xffaa66cc, 0xffff8800);
mCircleView = new CircleImageView(context, CIRCLE_BG_LIGHT);
mCircleView.setImageDrawable(mProgressDrawable);
mCircleView.setAlpha(0f);
addView(mCircleView);
mCircleDiameter = (int) getResources().getDimension(R.dimen.dp_40);
mBezierPath = new Path();
mBezierPaint = new Paint();
mBezierPaint.setAntiAlias(true);
mBezierPaint.setStyle(Paint.Style.FILL);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialHeader);
mShowBezierWave = typedArray.getBoolean(R.styleable.MaterialHeader_srlShowBezierWave, mShowBezierWave);
mScrollableWhenRefreshing = typedArray.getBoolean(R.styleable.MaterialHeader_srlScrollableWhenRefreshing, mScrollableWhenRefreshing);
mBezierPaint.setColor(typedArray.getColor(R.styleable.MaterialHeader_srlPrimaryColor, 0xff11bbff));
if (typedArray.hasValue(R.styleable.MaterialHeader_srlShadowRadius)) {
int radius = typedArray.getDimensionPixelOffset(R.styleable.MaterialHeader_srlShadowRadius, 0);
int color = typedArray.getColor(R.styleable.MaterialHeader_mhShadowColor, 0xff000000);
mBezierPaint.setShadowLayer(radius, 0, 0, color);
setLayerType(LAYER_TYPE_SOFTWARE, null);
}
mShowBezierWave = typedArray.getBoolean(R.styleable.MaterialHeader_mhShowBezierWave, mShowBezierWave);
mScrollableWhenRefreshing = typedArray.getBoolean(R.styleable.MaterialHeader_mhScrollableWhenRefreshing, mScrollableWhenRefreshing);
if (typedArray.hasValue(R.styleable.MaterialHeader_mhPrimaryColor)) {
mBezierPaint.setColor(typedArray.getColor(R.styleable.MaterialHeader_mhPrimaryColor, 0xff11bbff));
}
if (typedArray.hasValue(R.styleable.MaterialHeader_mhShadowRadius)) {
int radius = typedArray.getDimensionPixelOffset(R.styleable.MaterialHeader_mhShadowRadius, 0);
int color = typedArray.getColor(R.styleable.MaterialHeader_mhShadowColor, 0xff000000);
mBezierPaint.setShadowLayer(radius, 0, 0, color);
setLayerType(LAYER_TYPE_SOFTWARE, null);
}
typedArray.recycle();
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.setMeasuredDimension(getSize(widthMeasureSpec), getSize(heightMeasureSpec));
mCircleView.measure(MeasureSpec.makeMeasureSpec(mCircleDiameter, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(mCircleDiameter, MeasureSpec.EXACTLY));
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
if (getChildCount() == 0) {
return;
}
final int width = getMeasuredWidth();
int circleWidth = mCircleView.getMeasuredWidth();
int circleHeight = mCircleView.getMeasuredHeight();
if (isInEditMode() && mHeadHeight > 0) {
int circleTop = mHeadHeight - circleHeight / 2;
mCircleView.layout((width / 2 - circleWidth / 2), circleTop,
(width / 2 + circleWidth / 2), circleTop + circleHeight);
mProgressDrawable.showArrow(true);
mProgressDrawable.setStartEndTrim(0f, MAX_PROGRESS_ANGLE);
mProgressDrawable.setArrowScale(1);
mCircleView.setAlpha(1f);
mCircleView.setVisibility(VISIBLE);
} else {
mCircleView.layout((width / 2 - circleWidth / 2), -circleHeight, (width / 2 + circleWidth / 2), 0);
}
}
@Override
protected void dispatchDraw(Canvas canvas) {
if (mShowBezierWave) {
// 重置画笔
mBezierPath.reset();
mBezierPath.lineTo(0, mHeadHeight);
// 绘制贝塞尔曲线
mBezierPath.quadTo(getMeasuredWidth() / 2f, mHeadHeight + mWaveHeight * 1.9f, getMeasuredWidth(), mHeadHeight);
mBezierPath.lineTo(getMeasuredWidth(), 0);
canvas.drawPath(mBezierPath, mBezierPaint);
}
super.dispatchDraw(canvas);
}
@Override
public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {
if (!mShowBezierWave) {
kernel.requestDefaultTranslationContentFor(this, false);
}
if (isInEditMode()) {
mWaveHeight = mHeadHeight = height / 2;
}
}
@Override
public void onMoving(boolean dragging, float percent, int offset, int height, int maxDragHeight) {
if (mRefreshState == RefreshState.Refreshing) {
return;
}
if (mShowBezierWave) {
mHeadHeight = Math.min(offset, height);
mWaveHeight = Math.max(0, offset - height);
postInvalidate();
}
if (dragging || (!mProgressDrawable.isRunning() && !mFinished)) {
if (mRefreshState != RefreshState.Refreshing) {
float originalDragPercent = 1f * offset / height;
float dragPercent = Math.min(1f, Math.abs(originalDragPercent));
float adjustedPercent = (float) Math.max(dragPercent - .4, 0) * 5 / 3;
float extraOs = Math.abs(offset) - height;
float tensionSlingshotPercent = Math.max(0, Math.min(extraOs, (float) height * 2)
/ (float) height);
float tensionPercent = (float) ((tensionSlingshotPercent / 4) - Math.pow(
(tensionSlingshotPercent / 4), 2)) * 2f;
float strokeStart = adjustedPercent * .8f;
mProgressDrawable.showArrow(true);
mProgressDrawable.setStartEndTrim(0f, Math.min(MAX_PROGRESS_ANGLE, strokeStart));
mProgressDrawable.setArrowScale(Math.min(1f, adjustedPercent));
float rotation = (-0.25f + .4f * adjustedPercent + tensionPercent * 2) * .5f;
mProgressDrawable.setProgressRotation(rotation);
}
float targetY = offset / 2f + mCircleDiameter / 2f;
mCircleView.setTranslationY(Math.min(offset, targetY));
mCircleView.setAlpha(Math.min(1f, 4f * offset / mCircleDiameter));
}
}
@Override
public void onReleased(@NonNull RefreshLayout layout, int height, int maxDragHeight) {
mProgressDrawable.start();
}
@Override
public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
mRefreshState = newState;
if (newState == RefreshState.PullDownToRefresh) {
mFinished = false;
mCircleView.setVisibility(VISIBLE);
mCircleView.setTranslationY(0);
mCircleView.setScaleX(1);
mCircleView.setScaleY(1);
}
}
@Override
public int onFinish(@NonNull RefreshLayout layout, boolean success) {
mProgressDrawable.stop();
mCircleView.animate().scaleX(0).scaleY(0);
mFinished = true;
return 0;
}
/**
* 设置背景色
*/
public MaterialHeader setProgressBackgroundResource(@ColorRes int id) {
setProgressBackgroundColor(ContextCompat.getColor(getContext(), id));
return this;
}
public MaterialHeader setProgressBackgroundColor(@ColorInt int color) {
mCircleView.setBackgroundColor(color);
return this;
}
/**
* 设置 ColorScheme
*
* @param colors ColorScheme
*/
public MaterialHeader setColorSchemeColors(@ColorInt int... colors) {
mProgressDrawable.setColorSchemeColors(colors);
return this;
}
/**
* 设置 ColorScheme
*
* @param ids ColorSchemeResources
*/
public MaterialHeader setColorSchemeResources(@ColorRes int... ids) {
int[] colors = new int[ids.length];
for (int i = 0; i < ids.length; i++) {
colors[i] = ContextCompat.getColor(getContext(), ids[i]);
}
return setColorSchemeColors(colors);
}
/**
* 设置刷新球样式
*
* @param style 可传入:{@link #BALL_STYLE_LARGE,#BALL_STYLE_DEFAULT}
*/
public MaterialHeader setBallStyle(int style) {
if (style != BALL_STYLE_LARGE && style != BALL_STYLE_DEFAULT) {
return this;
}
if (style == BALL_STYLE_LARGE) {
mCircleDiameter = (int) getResources().getDimension(R.dimen.dp_56);
} else {
mCircleDiameter = (int) getResources().getDimension(R.dimen.dp_40);
}
// force the bounds of the progress circle inside the circle view to
// update by setting it to null before updating its size and then
// re-setting it
mCircleView.setImageDrawable(null);
mProgressDrawable.updateSizes(style);
mCircleView.setImageDrawable(mProgressDrawable);
return this;
}
/**
* 是否显示贝塞尔图形
*/
public MaterialHeader setShowBezierWave(boolean show) {
mShowBezierWave = show;
return this;
}
/**
* 设置实在正在刷新的时候可以上下滚动 Header
*/
public MaterialHeader setScrollableWhenRefreshing(boolean scrollable) {
mScrollableWhenRefreshing = scrollable;
return this;
}
}