-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathMaterialHeader.java
More file actions
377 lines (333 loc) · 13.6 KB
/
MaterialHeader.java
File metadata and controls
377 lines (333 loc) · 13.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package com.scwang.smartrefresh.header;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.scwang.smartrefresh.header.internal.MaterialProgressDrawable;
import com.scwang.smartrefresh.header.material.CircleImageView;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.internal.InternalAbstract;
import com.scwang.smartrefresh.layout.util.SmartUtil;
import static android.view.View.MeasureSpec.getSize;
/**
* Material 主题下拉头
* Created by scwang on 2017/6/2.
*/
@SuppressWarnings("unused")
public class MaterialHeader extends InternalAbstract implements RefreshHeader {
//<editor-fold desc="field">
// Maps to ProgressBar.Large style
public static final int SIZE_LARGE = 0;
// Maps to ProgressBar default style
public static final int SIZE_DEFAULT = 1;
protected static final int CIRCLE_BG_LIGHT = 0xFFFAFAFA;
protected static final float MAX_PROGRESS_ANGLE = .8f;
@VisibleForTesting
protected static final int CIRCLE_DIAMETER = 40;
@VisibleForTesting
protected static final int CIRCLE_DIAMETER_LARGE = 56;
protected boolean mFinished;
protected int mCircleDiameter;
protected ImageView mCircleView;
protected MaterialProgressDrawable mProgress;
/**
* 贝塞尔背景
*/
protected int mWaveHeight;
protected int mHeadHeight;
protected int mHeadDefaultHeight;
protected Path mBezierPath;
protected Paint mBezierPaint;
protected RefreshState mState;
protected boolean mShowBezierWave = false;
protected boolean mScrollableWhenRefreshing = true;
//</editor-fold>
//<editor-fold desc="MaterialHeader">
public MaterialHeader(Context context) {
this(context, null);
}
public MaterialHeader(Context context, AttributeSet attrs) {
super(context, attrs, 0);
mSpinnerStyle = SpinnerStyle.MatchLayout;
final View thisView = this;
final ViewGroup thisGroup = this;
mHeadDefaultHeight = SmartUtil.dp2px(100);
thisView.setMinimumHeight(mHeadDefaultHeight);
mProgress = new MaterialProgressDrawable(this);
mProgress.setBackgroundColor(CIRCLE_BG_LIGHT);
// mProgress.setAlpha(255);
mProgress.setColorSchemeColors(0xff0099cc,0xffff4444,0xff669900,0xffaa66cc,0xffff8800);
mCircleView = new CircleImageView(context,CIRCLE_BG_LIGHT);
mCircleView.setImageDrawable(mProgress);
mCircleView.setAlpha(0f);
thisGroup.addView(mCircleView);
final DisplayMetrics metrics = thisView.getResources().getDisplayMetrics();
mCircleDiameter = (int) (CIRCLE_DIAMETER * metrics.density);
mBezierPath = new Path();
mBezierPaint = new Paint();
mBezierPaint.setAntiAlias(true);
mBezierPaint.setStyle(Paint.Style.FILL);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MaterialHeader);
mShowBezierWave = ta.getBoolean(R.styleable.MaterialHeader_mhShowBezierWave, mShowBezierWave);
mScrollableWhenRefreshing = ta.getBoolean(R.styleable.MaterialHeader_mhScrollableWhenRefreshing, mScrollableWhenRefreshing);
mBezierPaint.setColor(ta.getColor(R.styleable.MaterialHeader_mhPrimaryColor, 0xff11bbff));
if (ta.hasValue(R.styleable.MaterialHeader_mhShadowRadius)) {
int radius = ta.getDimensionPixelOffset(R.styleable.MaterialHeader_mhShadowRadius, 0);
int color = ta.getColor(R.styleable.MaterialHeader_mhShadowColor, 0xff000000);
mBezierPaint.setShadowLayer(radius, 0, 0, color);
thisView.setLayerType(LAYER_TYPE_SOFTWARE, null);
}
ta.recycle();
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
super.setMeasuredDimension(getSize(widthMeasureSpec), mHeadDefaultHeight);
final View circleView = mCircleView;
circleView.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) {
final ViewGroup thisGroup = this;
if (thisGroup.getChildCount() == 0) {
return;
}
final View thisView = this;
final View circleView = mCircleView;
final int width = thisView.getMeasuredWidth();
int circleWidth = circleView.getMeasuredWidth();
int circleHeight = circleView.getMeasuredHeight();
if (thisView.isInEditMode() && mHeadHeight > 0) {
int circleTop = mHeadHeight - circleHeight / 2;
circleView.layout((width / 2 - circleWidth / 2), circleTop,
(width / 2 + circleWidth / 2), circleTop + circleHeight);
mProgress.showArrow(true);
mProgress.setStartEndTrim(0f, MAX_PROGRESS_ANGLE);
mProgress.setArrowScale(1);
circleView.setAlpha(1f);
circleView.setVisibility(VISIBLE);
} else {
circleView.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);
//绘制贝塞尔曲线
final View thisView = this;
mBezierPath.quadTo(thisView.getMeasuredWidth() / 2f, mHeadHeight + mWaveHeight * 1.9f, thisView.getMeasuredWidth(), mHeadHeight);
mBezierPath.lineTo(thisView.getMeasuredWidth(), 0);
canvas.drawPath(mBezierPath, mBezierPaint);
}
super.dispatchDraw(canvas);
}
//</editor-fold>
//<editor-fold desc="RefreshHeader">
@Override
public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {
final View thisView = this;
//不改变布局移动的默认设置因为会影响后续设置的刷新头部的效果
// if (!mShowBezierWave) {
// kernel.requestDefaultTranslationContentFor(this, false);
//// kernel.requestDefaultHeaderTranslationContent(false);
// }
//只改变自身是否移动布局内容
kernel.getRefreshLayout().setEnableHeaderTranslationContent(mShowBezierWave);
if (thisView.isInEditMode()) {
mWaveHeight = mHeadHeight = height / 2;
}
}
@Override
public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
if (mState == RefreshState.Refreshing) {
return;
}
if (mShowBezierWave) {
mHeadHeight = Math.min(offset, height);
mWaveHeight = Math.max(0, offset - height);
final View thisView = this;
thisView.postInvalidate();
}
if (isDragging || (!mProgress.isRunning() && !mFinished)) {
if (mState != 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;
mProgress.showArrow(true);
mProgress.setStartEndTrim(0f, Math.min(MAX_PROGRESS_ANGLE, strokeStart));
mProgress.setArrowScale(Math.min(1f, adjustedPercent));
float rotation = (-0.25f + .4f * adjustedPercent + tensionPercent * 2) * .5f;
mProgress.setProgressRotation(rotation);
}
final View circleView = mCircleView;
float targetY = offset / 2f + mCircleDiameter / 2f;
circleView.setTranslationY(Math.min(offset, targetY));
circleView.setAlpha(Math.min(1f, 4f * offset / mCircleDiameter));
}
}
@Override
public void onReleased(@NonNull RefreshLayout layout, int height, int maxDragHeight) {
mProgress.start();
}
@Override
public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
final View circleView = mCircleView;
mState = newState;
switch (newState) {
case None:
break;
case PullDownToRefresh:
mFinished = false;
circleView.setVisibility(VISIBLE);
circleView.setTranslationY(0);
circleView.setScaleX(1);
circleView.setScaleY(1);
break;
case ReleaseToRefresh:
break;
case Refreshing:
break;
}
}
@Override
public int onFinish(@NonNull RefreshLayout layout, boolean success) {
final View circleView = mCircleView;
mProgress.stop();
circleView.animate().scaleX(0).scaleY(0);
mFinished = true;
return 0;
}
/**
* @param colors 对应Xml中配置的 srlPrimaryColor srlAccentColor
* @deprecated 请使用 {@link RefreshLayout#setPrimaryColorsId(int...)}
*/
@Override@Deprecated
public void setPrimaryColors(@ColorInt int ... colors) {
if (colors.length > 0) {
mBezierPaint.setColor(colors[0]);
}
}
//</editor-fold>
//<editor-fold desc="API">
/**
* Set the background color of the progress spinner disc.
*
* @param colorRes Resource id of the color.
*/
public MaterialHeader setProgressBackgroundColorSchemeResource(@ColorRes int colorRes) {
final View thisView = this;
final Context context = thisView.getContext();
final int color = ContextCompat.getColor(context, colorRes);
return setProgressBackgroundColorSchemeColor(color);
}
/**
* Set the alpha of the progress spinner disc.
*
* @param alpha 透明度
*/
public MaterialHeader setProgressAlpha(@IntRange(from = 0, to = 1) int alpha) {
mProgress.setAlpha(alpha);
return this;
}
/**
* Set the background color of the progress spinner disc.
*
* @param color 颜色
*/
public MaterialHeader setProgressBackgroundColorSchemeColor(@ColorInt int color) {
mProgress.setBackgroundColor(color);
return this;
}
/**
* 设置 ColorScheme
* @param colors ColorScheme
* @return MaterialHeader
*/
public MaterialHeader setColorSchemeColors(@ColorInt int... colors) {
mProgress.setColorSchemeColors(colors);
return this;
}
/**
* 设置 ColorScheme
* @param colorIds ColorSchemeResources
* @return MaterialHeader
*/
public MaterialHeader setColorSchemeResources(@ColorRes int... colorIds) {
final View thisView = this;
final Context context = thisView.getContext();
int[] colors = new int[colorIds.length];
for (int i = 0; i < colorIds.length; i++) {
colors[i] = ContextCompat.getColor(context, colorIds[i]);
}
return setColorSchemeColors(colors);
}
/**
* 设置大小尺寸
* @param size One of DEFAULT, or LARGE.
* @return MaterialHeader
*/
public MaterialHeader setSize(int size) {
if (size != SIZE_LARGE && size != SIZE_DEFAULT) {
return this;
}
final View thisView = this;
DisplayMetrics metrics = thisView.getResources().getDisplayMetrics();
if (size == SIZE_LARGE) {
mCircleDiameter = (int) (CIRCLE_DIAMETER_LARGE * metrics.density);
} else {
mCircleDiameter = (int) (CIRCLE_DIAMETER * metrics.density);
}
// 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);
mProgress.updateSizes(size);
mCircleView.setImageDrawable(mProgress);
return this;
}
/**
* 是否显示贝塞尔图形
* @param show 是否显示
* @return MaterialHeader
*/
public MaterialHeader setShowBezierWave(boolean show) {
this.mShowBezierWave = show;
return this;
}
/**
* 设置实在正在刷新的时候可以 上下滚动 Header
* @param scrollable 是否支持滚动
*/
public MaterialHeader setScrollableWhenRefreshing(boolean scrollable) {
this.mScrollableWhenRefreshing = scrollable;
return this;
}
//</editor-fold>
}