publicclassWebProgressextendsFrameLayout {
publicstaticfinalintMAX_UNIFORM_SPEED_DURATION=8*1000;
publicstaticfinalintMAX_DECELERATE_SPEED_DURATION=450;
publicstaticfinalintDO_END_ALPHA_DURATION=630;
publicstaticfinalintDO_END_PROGRESS_DURATION=500;
privatestaticintCURRENT_MAX_UNIFORM_SPEED_DURATION=MAX_UNIFORM_SPEED_DURATION;
privatestaticintCURRENT_MAX_DECELERATE_SPEED_DURATION=MAX_DECELERATE_SPEED_DURATION;
publicstaticintWEB_PROGRESS_DEFAULT_HEIGHT=3;
publicstaticStringWEB_PROGRESS_COLOR="#2483D9";
privateintmColor;
privatePaintmPaint;
privateAnimatormAnimator;
privateintmTargetWidth=0;
privateintmTargetHeight;
privateintTAG=0;
privatebooleanisShow=false;
publicstaticfinalintUN_START=0;
publicstaticfinalintSTARTED=1;
publicstaticfinalintFINISH=2;
privatefloatmCurrentProgress=0F;
publicWebProgress(Contextcontext) {
this(context, null);
}
publicWebProgress(Contextcontext, @NullableAttributeSetattrs) {
this(context, attrs, 0);
}
publicWebProgress(Contextcontext, @NullableAttributeSetattrs, intdefStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
@OverrideprotectedvoidonAttachedToWindow() {
super.onAttachedToWindow();
}
@OverrideprotectedvoidonDetachedFromWindow() {
super.onDetachedFromWindow();
if (mAnimator!=null&&mAnimator.isStarted()) {
mAnimator.cancel();
mAnimator=null;
}
}
privatevoidinit(Contextcontext, AttributeSetattrs, intdefStyleAttr) {
mPaint=newPaint();
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);
}
publicvoidsetColor(intcolor) {
this.mColor=color;
mPaint.setColor(color);
}
publicvoidsetColor(Stringcolor) {
this.setColor(Color.parseColor(color));
}
publicvoidsetColor(intstartColor, intendColor) {
try {
LinearGradientlinearGradient=newLinearGradient(0, 0, mTargetWidth,
mTargetHeight, startColor, endColor, Shader.TileMode.CLAMP);
mPaint.setShader(linearGradient);
} catch (Exceptione){
e.printStackTrace();
}
}
publicvoidsetColor(StringstartColor, StringendColor) {
this.setColor(Color.parseColor(startColor), Color.parseColor(endColor));
}
@OverrideprotectedvoidonMeasure(intwidthMeasureSpec, intheightMeasureSpec) {
intwMode=MeasureSpec.getMode(widthMeasureSpec);
inthMode=MeasureSpec.getMode(heightMeasureSpec);
intw=MeasureSpec.getSize(widthMeasureSpec);
inth=MeasureSpec.getSize(heightMeasureSpec);
if (wMode==MeasureSpec.AT_MOST) {
DisplayMetricsdisplayMetrics=getContext().getResources().getDisplayMetrics();
w=w<=displayMetrics.widthPixels?w : displayMetrics.widthPixels;
}
if (hMode==MeasureSpec.AT_MOST) {
h=mTargetHeight;
}
this.setMeasuredDimension(w, h);
}
@OverrideprotectedvoidonDraw(Canvascanvas) {
}
@OverrideprotectedvoiddispatchDraw(Canvascanvas) {
canvas.drawRect(0, 0,
mCurrentProgress/100*Float.valueOf(this.getWidth()), this.getHeight(), mPaint);
}
@OverrideprotectedvoidonSizeChanged(intw, inth, intoldw, intoldh) {
super.onSizeChanged(w, h, oldw, oldh);
this.mTargetWidth=getMeasuredWidth();
intscreenWidth=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 {
floatrate= (float) (this.mTargetWidth/ (screenWidth*1.0));
CURRENT_MAX_UNIFORM_SPEED_DURATION= (int) (MAX_UNIFORM_SPEED_DURATION*rate);
CURRENT_MAX_DECELERATE_SPEED_DURATION= (int) (MAX_DECELERATE_SPEED_DURATION*rate);
}
}
privatevoidsetFinish() {
isShow=false;
TAG=FINISH;
}
privatevoidstartAnim(booleanisFinished) {
floatv=isFinished?100 : 95;
if (mAnimator!=null&&mAnimator.isStarted()) {
mAnimator.cancel();
}
mCurrentProgress= ((int)mCurrentProgress) ==0f?0.00000001f : mCurrentProgress;
if (!isFinished) {
ValueAnimatormAnimator=ValueAnimator.ofFloat(mCurrentProgress, v);
floatresidue=1f-mCurrentProgress/100-0.05f;
mAnimator.setInterpolator(newLinearInterpolator());
mAnimator.setDuration((long) (residue*CURRENT_MAX_UNIFORM_SPEED_DURATION));
mAnimator.addUpdateListener(mAnimatorUpdateListener);
mAnimator.start();
this.mAnimator=mAnimator;
} else {
ValueAnimatorsegment95Animator=null;
if (mCurrentProgress<95f) {
segment95Animator=ValueAnimator.ofFloat(mCurrentProgress, 95);
floatresidue=1f-mCurrentProgress/100f-0.05f;
segment95Animator.setInterpolator(newLinearInterpolator());
segment95Animator.setDuration((long) (residue*CURRENT_MAX_DECELERATE_SPEED_DURATION));
segment95Animator.setInterpolator(newDecelerateInterpolator());
segment95Animator.addUpdateListener(mAnimatorUpdateListener);
}
ObjectAnimatormObjectAnimator=ObjectAnimator.ofFloat(
this, "alpha", 1f, 0f);
mObjectAnimator.setDuration(DO_END_ALPHA_DURATION);
ValueAnimatormValueAnimatorEnd=ValueAnimator.ofFloat(95f, 100f);
mValueAnimatorEnd.setDuration(DO_END_PROGRESS_DURATION);
mValueAnimatorEnd.addUpdateListener(mAnimatorUpdateListener);
AnimatorSetmAnimatorSet=newAnimatorSet();
mAnimatorSet.playTogether(mObjectAnimator, mValueAnimatorEnd);
if (segment95Animator!=null) {
AnimatorSetmAnimatorSet1=newAnimatorSet();
mAnimatorSet1.play(mAnimatorSet).after(segment95Animator);
mAnimatorSet=mAnimatorSet1;
}
mAnimatorSet.addListener(mAnimatorListener);
mAnimatorSet.start();
mAnimator=mAnimatorSet;
}
TAG=STARTED;
}
privateValueAnimator.AnimatorUpdateListenermAnimatorUpdateListener=newValueAnimator.AnimatorUpdateListener() {
@OverridepublicvoidonAnimationUpdate(ValueAnimatoranimation) {
floatt= (float) animation.getAnimatedValue();
WebProgress.this.mCurrentProgress=t;
WebProgress.this.invalidate();
}
};
privateAnimatorListenerAdaptermAnimatorListener=newAnimatorListenerAdapter() {
@OverridepublicvoidonAnimationEnd(Animatoranimation) {
doEnd();
}
@OverridepublicvoidonAnimationCancel(Animatoranimation) {
super.onAnimationCancel(animation);
}
@OverridepublicvoidonAnimationStart(Animatoranimation) {
super.onAnimationStart(animation);
}
};
privatevoiddoEnd() {
if (TAG==FINISH&& ((int)mCurrentProgress) ==100f) {
setVisibility(GONE);
mCurrentProgress=0f;
this.setAlpha(1f);
}
TAG=UN_START;
}
publicvoidreset() {
mCurrentProgress=0;
if (mAnimator!=null&&mAnimator.isStarted()) {
mAnimator.cancel();
}
}
publicvoidsetProgress(intprogress) {
if (TAG==UN_START&&progress==100f) {
setVisibility(View.GONE);
return;
}
if (getVisibility() ==View.GONE) {
setVisibility(View.VISIBLE);
}
if (progress<95f) {
return;
}
if (TAG!=FINISH) {
startAnim(true);
}
}
publicLayoutParamsofferLayoutParams() {
returnnewLayoutParams(mTargetWidth, mTargetHeight);
}
privateintdip2px(floatdpValue) {
finalfloatscale=getContext().getResources().getDisplayMetrics().density;
return (int) (dpValue*scale+0.5f);
}
publicvoidshow() {
isShow=true;
setVisibility(View.VISIBLE);
mCurrentProgress=0f;
startAnim(false);
}
publicvoidhide() {
setWebProgress(100);
}
publicvoidsetWebProgress(intnewProgress) {
if (newProgress>=0&&newProgress<95) {
if (!isShow) {
show();
} else {
setProgress(newProgress);
}
} else {
setProgress(newProgress);
setFinish();
}
}
}