-
Notifications
You must be signed in to change notification settings - Fork 31
/
WebProgress.java
384 lines (337 loc) · 12 KB
/
WebProgress.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
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
378
379
380
381
382
383
package me.jingbin.progress;
/*
* Copyright 2019. Bin Jing (https://github.com/youlookwhat)
*
* 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.
*/
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
/**
* WebView进度条,原作者: cenxiaozhong,在此基础上修改优化:
* 1. progress同时返回两次100时进度条出现两次
* 2. 当一条进度没跑完,又点击其他链接开始第二次进度时,第二次进度不出现
* 3. 修改消失动画时长,使其消失时看到可以进度跑完
* 4. [2019.9.29] 修复当第一次进度返回 0 或超过 10,出现不显示进度条的问题
* 5. 能显示渐变色
* 6. 进度在95-100时再次开始进度条透明度问题
*
* @author jingbin
* Link to https://github.com/youlookwhat/WebProgress
*/
public class WebProgress extends FrameLayout {
/**
* 默认匀速动画最大的时长
*/
public static final int MAX_UNIFORM_SPEED_DURATION = 8 * 1000;
/**
* 默认加速后减速动画最大时长
*/
public static final int MAX_DECELERATE_SPEED_DURATION = 450;
/**
* 95f-100f时,透明度1f-0f时长
*/
public static final int DO_END_ALPHA_DURATION = 630;
/**
* 95f - 100f动画时长
*/
public static final int DO_END_PROGRESS_DURATION = 500;
/**
* 当前匀速动画最大的时长
*/
private static int CURRENT_MAX_UNIFORM_SPEED_DURATION = MAX_UNIFORM_SPEED_DURATION;
/**
* 当前加速后减速动画最大时长
*/
private static int CURRENT_MAX_DECELERATE_SPEED_DURATION = MAX_DECELERATE_SPEED_DURATION;
/**
* 默认的高度(dp)
*/
public static int WEB_PROGRESS_DEFAULT_HEIGHT = 3;
/**
* 进度条颜色默认
*/
public static String WEB_PROGRESS_COLOR = "#2483D9";
/**
* 进度条颜色
*/
private int mColor;
/**
* 进度条的画笔
*/
private Paint mPaint;
/**
* 进度条动画
*/
private Animator mAnimator;
/**
* 控件的宽度
*/
private int mTargetWidth = 0;
/**
* 控件的高度
*/
private int mTargetHeight;
/**
* 标志当前进度条的状态
*/
private int TAG = 0;
/**
* 第一次过来进度show,后面就是setProgress
*/
private boolean isShow = false;
public static final int UN_START = 0;
public static final int STARTED = 1;
public static final int FINISH = 2;
private float mCurrentProgress = 0F;
public WebProgress(Context context) {
this(context, null);
}
public WebProgress(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public WebProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
mPaint = new Paint();
mColor = Color.parseColor(WEB_PROGRESS_COLOR);
mPaint.setAntiAlias(true);
mPaint.setColor(mColor);
mPaint.setDither(true);
mPaint.setStrokeCap(Paint.Cap.SQUARE);
mTargetWidth = context.getResources().getDisplayMetrics().widthPixels;
mTargetHeight = dip2px(WEB_PROGRESS_DEFAULT_HEIGHT);
}
/**
* 设置单色进度条
*/
public void setColor(int color) {
this.mColor = color;
mPaint.setColor(color);
}
public void setColor(String color) {
this.setColor(Color.parseColor(color));
}
public void setColor(int startColor, int endColor) {
LinearGradient linearGradient = new LinearGradient(0, 0, mTargetWidth, mTargetHeight, startColor, endColor, Shader.TileMode.CLAMP);
mPaint.setShader(linearGradient);
}
/**
* 设置渐变色进度条
*
* @param startColor 开始颜色
* @param endColor 结束颜色
*/
public void setColor(String startColor, String endColor) {
this.setColor(Color.parseColor(startColor), Color.parseColor(endColor));
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int wMode = MeasureSpec.getMode(widthMeasureSpec);
int w = MeasureSpec.getSize(widthMeasureSpec);
int hMode = MeasureSpec.getMode(heightMeasureSpec);
int h = MeasureSpec.getSize(heightMeasureSpec);
if (wMode == MeasureSpec.AT_MOST) {
w = Math.min(w, getContext().getResources().getDisplayMetrics().widthPixels);
}
if (hMode == MeasureSpec.AT_MOST) {
h = mTargetHeight;
}
this.setMeasuredDimension(w, h);
}
@Override
protected void onDraw(Canvas canvas) {
}
@Override
protected void dispatchDraw(Canvas canvas) {
canvas.drawRect(0, 0, mCurrentProgress / 100 * (float) this.getWidth(), this.getHeight(), mPaint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
this.mTargetWidth = getMeasuredWidth();
int screenWidth = getContext().getResources().getDisplayMetrics().widthPixels;
if (mTargetWidth >= screenWidth) {
CURRENT_MAX_DECELERATE_SPEED_DURATION = MAX_DECELERATE_SPEED_DURATION;
CURRENT_MAX_UNIFORM_SPEED_DURATION = MAX_UNIFORM_SPEED_DURATION;
} else {
//取比值
float rate = this.mTargetWidth / (float) screenWidth;
CURRENT_MAX_UNIFORM_SPEED_DURATION = (int) (MAX_UNIFORM_SPEED_DURATION * rate);
CURRENT_MAX_DECELERATE_SPEED_DURATION = (int) (MAX_DECELERATE_SPEED_DURATION * rate);
}
}
private void setFinish() {
isShow = false;
TAG = FINISH;
}
private void startAnim(boolean isFinished) {
float v = isFinished ? 100 : 95;
if (mAnimator != null && mAnimator.isStarted()) {
mAnimator.cancel();
}
mCurrentProgress = mCurrentProgress == 0 ? 0.00000001f : mCurrentProgress;
// 可能由于透明度造成突然出现的问题
setAlpha(1);
if (!isFinished) {
ValueAnimator mAnimator = ValueAnimator.ofFloat(mCurrentProgress, v);
float residue = 1f - mCurrentProgress / 100 - 0.05f;
mAnimator.setInterpolator(new LinearInterpolator());
mAnimator.setDuration((long) (residue * CURRENT_MAX_UNIFORM_SPEED_DURATION));
mAnimator.addUpdateListener(mAnimatorUpdateListener);
mAnimator.start();
this.mAnimator = mAnimator;
} else {
ValueAnimator segment95Animator = null;
if (mCurrentProgress < 95) {
segment95Animator = ValueAnimator.ofFloat(mCurrentProgress, 95);
float residue = 1f - mCurrentProgress / 100f - 0.05f;
segment95Animator.setInterpolator(new LinearInterpolator());
segment95Animator.setDuration((long) (residue * CURRENT_MAX_DECELERATE_SPEED_DURATION));
segment95Animator.setInterpolator(new DecelerateInterpolator());
segment95Animator.addUpdateListener(mAnimatorUpdateListener);
}
ObjectAnimator mObjectAnimator = ObjectAnimator.ofFloat(this, "alpha", 1f, 0f);
mObjectAnimator.setDuration(DO_END_ALPHA_DURATION);
ValueAnimator mValueAnimatorEnd = ValueAnimator.ofFloat(95f, 100f);
mValueAnimatorEnd.setDuration(DO_END_PROGRESS_DURATION);
mValueAnimatorEnd.addUpdateListener(mAnimatorUpdateListener);
AnimatorSet mAnimatorSet = new AnimatorSet();
mAnimatorSet.playTogether(mObjectAnimator, mValueAnimatorEnd);
if (segment95Animator != null) {
AnimatorSet mAnimatorSet1 = new AnimatorSet();
mAnimatorSet1.play(mAnimatorSet).after(segment95Animator);
mAnimatorSet = mAnimatorSet1;
}
mAnimatorSet.addListener(mAnimatorListenerAdapter);
mAnimatorSet.start();
mAnimator = mAnimatorSet;
}
TAG = STARTED;
}
private ValueAnimator.AnimatorUpdateListener mAnimatorUpdateListener = new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float t = (float) animation.getAnimatedValue();
WebProgress.this.mCurrentProgress = t;
WebProgress.this.invalidate();
}
};
private AnimatorListenerAdapter mAnimatorListenerAdapter = new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
doEnd();
}
};
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
/**
* animator cause leak , if not cancel;
*/
if (mAnimator != null && mAnimator.isStarted()) {
mAnimator.cancel();
mAnimator = null;
}
}
private void doEnd() {
if (TAG == FINISH && mCurrentProgress == 100) {
setVisibility(GONE);
mCurrentProgress = 0f;
this.setAlpha(1f);
}
TAG = UN_START;
}
public void reset() {
mCurrentProgress = 0;
if (mAnimator != null && mAnimator.isStarted()) {
mAnimator.cancel();
}
}
public void setProgress(int newProgress) {
setProgress(Float.valueOf(newProgress));
}
public LayoutParams offerLayoutParams() {
return new LayoutParams(mTargetWidth, mTargetHeight);
}
private int dip2px(float dpValue) {
final float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public WebProgress setHeight(int heightDp) {
this.mTargetHeight = dip2px(heightDp);
return this;
}
public void setProgress(float progress) {
// fix 同时返回两个 100,产生两次进度条的问题;
if (TAG == UN_START && progress == 100) {
setVisibility(View.GONE);
return;
}
if (getVisibility() == View.GONE) {
setVisibility(View.VISIBLE);
}
if (progress < 95) {
return;
}
if (TAG != FINISH) {
startAnim(true);
}
}
/**
* 显示进度条
*/
public void show() {
isShow = true;
setVisibility(View.VISIBLE);
mCurrentProgress = 0f;
startAnim(false);
}
/**
* 进度完成后消失
*/
public void hide() {
setWebProgress(100);
}
/**
* 为单独处理WebView进度条
*/
public void setWebProgress(int newProgress) {
if (newProgress >= 0 && newProgress < 95) {
if (!isShow) {
show();
} else {
setProgress(newProgress);
}
} else {
setProgress(newProgress);
setFinish();
}
}
}