SmartBallPulseFooter.java
5.56 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
package com.studymachine.www.other;
import android.animation.TimeInterpolator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.animation.AccelerateDecelerateInterpolator;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.ColorUtils;
import com.studymachine.www.R;
import com.scwang.smart.refresh.layout.api.RefreshFooter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;
import com.scwang.smart.refresh.layout.simple.SimpleComponent;
/**
* author : 树朾 & Android 轮子哥
* github : https://github.com/scwang90/SmartRefreshLayout/tree/master/refresh-footer-ball
* time : 2020/08/01
* desc : 球脉冲底部加载组件
*/
public final class SmartBallPulseFooter extends SimpleComponent implements RefreshFooter {
private final TimeInterpolator mInterpolator = new AccelerateDecelerateInterpolator();
private boolean mNoMoreData;
private boolean mManualNormalColor;
private boolean mManualAnimationColor;
private final Paint mPaint;
private int mNormalColor = 0xFFEEEEEE;
private int[] mAnimatingColor = {0xFF30B399, 0xFFFF4600, 0xFF142DCC};
private final float mCircleSpacing;
private long mStartTime = 0;
private boolean mStarted = false;
private final float mTextWidth;
public SmartBallPulseFooter(Context context) {
this(context, null);
}
public SmartBallPulseFooter(Context context, @Nullable AttributeSet attrs) {
super(context, attrs, 0);
setMinimumHeight((int) getResources().getDimension(R.dimen.dp_60));
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true);
mSpinnerStyle = SpinnerStyle.Translate;
mCircleSpacing = getResources().getDimension(R.dimen.dp_2);
mPaint.setTextSize(getResources().getDimension(R.dimen.sp_14));
mTextWidth = mPaint.measureText(getContext().getString(R.string.common_no_more_data));
}
@Override
protected void dispatchDraw(Canvas canvas) {
final int width = getWidth();
final int height = getHeight();
if (mNoMoreData) {
mPaint.setColor(0xFF898989);
canvas.drawText(getContext().getString(R.string.common_no_more_data),(width - mTextWidth) / 2,(height - mPaint.getTextSize()) / 2, mPaint);
} else {
float radius = (Math.min(width, height) - mCircleSpacing * 2) / 7;
float x = width / 2f - (radius * 2 + mCircleSpacing);
float y = height / 2f;
final long now = System.currentTimeMillis();
for (int i = 0; i < 3; i++) {
long time = now - mStartTime - 120 * (i + 1);
float percent = time > 0 ? ((time % 750) / 750f) : 0;
percent = mInterpolator.getInterpolation(percent);
canvas.save();
float translateX = x + (radius * 2) * i + mCircleSpacing * i;
if (percent < 0.5) {
float scale = 1 - percent * 2 * 0.7f;
float translateY = y - scale * 10;
canvas.translate(translateX, translateY);
} else {
float scale = percent * 2 * 0.7f - 0.4f;
float translateY = y + scale * 10;
canvas.translate(translateX, translateY);
}
mPaint.setColor(mAnimatingColor[i % mAnimatingColor.length]);
canvas.drawCircle(0, 0, radius / 3, mPaint);
canvas.restore();
}
}
if (mStarted) {
postInvalidate();
}
}
@Override
public void onStartAnimator(@NonNull RefreshLayout layout, int height, int maxDragHeight) {
if (mStarted) {
return;
}
invalidate();
mStarted = true;
mStartTime = System.currentTimeMillis();
}
@Override
public int onFinish(@NonNull RefreshLayout layout, boolean success) {
mStarted = false;
mStartTime = 0;
mPaint.setColor(mNormalColor);
return 0;
}
@Override
public void setPrimaryColors(@ColorInt int... colors) {
if (!mManualAnimationColor && colors.length > 1) {
setAnimatingColor(colors[0]);
mManualAnimationColor = false;
}
if (!mManualNormalColor) {
if (colors.length > 1) {
setNormalColor(colors[1]);
} else if (colors.length > 0) {
setNormalColor(ColorUtils.compositeColors(0x99FFFFFF, colors[0]));
}
mManualNormalColor = false;
}
}
@Override
public boolean setNoMoreData(boolean noMoreData) {
mNoMoreData = noMoreData;
return true;
}
public SmartBallPulseFooter setSpinnerStyle(SpinnerStyle style) {
mSpinnerStyle = style;
return this;
}
public SmartBallPulseFooter setNormalColor(@ColorInt int color) {
mNormalColor = color;
mManualNormalColor = true;
if (!mStarted) {
mPaint.setColor(color);
}
return this;
}
public SmartBallPulseFooter setAnimatingColor(@ColorInt int color) {
mAnimatingColor = new int[]{color};
mManualAnimationColor = true;
if (mStarted) {
mPaint.setColor(color);
}
return this;
}
}