|
|
@ -3,13 +3,14 @@ package acr.browser.lightning; |
|
|
|
import android.animation.ObjectAnimator; |
|
|
|
import android.animation.ObjectAnimator; |
|
|
|
import android.content.Context; |
|
|
|
import android.content.Context; |
|
|
|
import android.content.res.TypedArray; |
|
|
|
import android.content.res.TypedArray; |
|
|
|
|
|
|
|
import android.graphics.Canvas; |
|
|
|
|
|
|
|
import android.graphics.Paint; |
|
|
|
|
|
|
|
import android.graphics.Rect; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.os.Parcelable; |
|
|
|
import android.os.Parcelable; |
|
|
|
import android.util.AttributeSet; |
|
|
|
import android.util.AttributeSet; |
|
|
|
import android.view.LayoutInflater; |
|
|
|
import android.view.LayoutInflater; |
|
|
|
import android.view.View; |
|
|
|
|
|
|
|
import android.view.animation.Animation; |
|
|
|
import android.view.animation.Animation; |
|
|
|
import android.view.animation.Animation.AnimationListener; |
|
|
|
|
|
|
|
import android.view.animation.DecelerateInterpolator; |
|
|
|
import android.view.animation.DecelerateInterpolator; |
|
|
|
import android.view.animation.Transformation; |
|
|
|
import android.view.animation.Transformation; |
|
|
|
import android.widget.LinearLayout; |
|
|
|
import android.widget.LinearLayout; |
|
|
@ -31,13 +32,10 @@ import android.widget.LinearLayout; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class AnimatedProgressBar extends LinearLayout { |
|
|
|
public class AnimatedProgressBar extends LinearLayout { |
|
|
|
|
|
|
|
|
|
|
|
private View mProgressView; |
|
|
|
|
|
|
|
private int mProgress = 0; |
|
|
|
private int mProgress = 0; |
|
|
|
private boolean mBidirectionalAnimate = true; |
|
|
|
private boolean mBidirectionalAnimate = true; |
|
|
|
private Animation mAnimation; |
|
|
|
private int mDrawWidth = 0; |
|
|
|
int mInitialWidth; |
|
|
|
private int mProgressColor; |
|
|
|
int mDeltaWidth; |
|
|
|
|
|
|
|
int mMaxWidth; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public AnimatedProgressBar(Context context, AttributeSet attrs) { |
|
|
|
public AnimatedProgressBar(Context context, AttributeSet attrs) { |
|
|
|
super(context, attrs); |
|
|
|
super(context, attrs); |
|
|
@ -49,11 +47,6 @@ public class AnimatedProgressBar extends LinearLayout { |
|
|
|
init(context, attrs); |
|
|
|
init(context, attrs); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
|
|
|
|
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Initialize the AnimatedProgressBar |
|
|
|
* Initialize the AnimatedProgressBar |
|
|
|
* |
|
|
|
* |
|
|
@ -61,71 +54,26 @@ public class AnimatedProgressBar extends LinearLayout { |
|
|
|
* @param attrs is the attribute set passed by the constructor |
|
|
|
* @param attrs is the attribute set passed by the constructor |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void init(final Context context, AttributeSet attrs) { |
|
|
|
private void init(final Context context, AttributeSet attrs) { |
|
|
|
this.setLayerType(LAYER_TYPE_NONE, null); |
|
|
|
this.setLayerType(LAYER_TYPE_HARDWARE, null); |
|
|
|
TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AnimatedProgressBar, 0, 0); |
|
|
|
TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AnimatedProgressBar, 0, 0); |
|
|
|
int backgroundColor; |
|
|
|
int backgroundColor; |
|
|
|
int progressColor; |
|
|
|
|
|
|
|
try { // Retrieve the style of the progress bar that the user hopefully set
|
|
|
|
try { // Retrieve the style of the progress bar that the user hopefully set
|
|
|
|
|
|
|
|
int DEFAULT_BACKGROUND_COLOR = 0x424242; |
|
|
|
int DEFAULT_BACKGROUND_COLOR = 0x00000000; |
|
|
|
|
|
|
|
int DEFAULT_PROGRESS_COLOR = 0x2196f3; |
|
|
|
int DEFAULT_PROGRESS_COLOR = 0x2196f3; |
|
|
|
|
|
|
|
|
|
|
|
backgroundColor = array.getColor(R.styleable.AnimatedProgressBar_backgroundColor, DEFAULT_BACKGROUND_COLOR); |
|
|
|
backgroundColor = array.getColor(R.styleable.AnimatedProgressBar_backgroundColor, DEFAULT_BACKGROUND_COLOR); |
|
|
|
progressColor = array.getColor(R.styleable.AnimatedProgressBar_progressColor, DEFAULT_PROGRESS_COLOR); |
|
|
|
mProgressColor = array.getColor(R.styleable.AnimatedProgressBar_progressColor, DEFAULT_PROGRESS_COLOR); |
|
|
|
mBidirectionalAnimate = array.getBoolean(R.styleable.AnimatedProgressBar_bidirectionalAnimate, false); |
|
|
|
mBidirectionalAnimate = array.getBoolean(R.styleable.AnimatedProgressBar_bidirectionalAnimate, false); |
|
|
|
|
|
|
|
|
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
|
|
|
|
|
|
|
|
array.recycle(); |
|
|
|
array.recycle(); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
|
|
|
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
|
|
|
inflater.inflate(R.layout.animated_progress_bar, this, true); |
|
|
|
inflater.inflate(R.layout.animated_progress_bar, this, true); |
|
|
|
|
|
|
|
|
|
|
|
mProgressView = findViewById(android.R.id.progress); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.setBackgroundColor(backgroundColor); // set the background color for this view
|
|
|
|
this.setBackgroundColor(backgroundColor); // set the background color for this view
|
|
|
|
mProgressView.setBackgroundColor(progressColor); // set the color of the progress bar
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mAnimation = new Animation() { |
|
|
|
|
|
|
|
int width; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
protected void applyTransformation(float interpolatedTime, Transformation t) { |
|
|
|
|
|
|
|
width = mInitialWidth + (int) (mDeltaWidth * interpolatedTime); |
|
|
|
|
|
|
|
if (width <= mMaxWidth) { |
|
|
|
|
|
|
|
mProgressView.getLayoutParams().width = width; |
|
|
|
|
|
|
|
mProgressView.requestLayout(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean willChangeBounds() { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
mAnimation.setAnimationListener(new AnimationListener(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onAnimationStart(Animation animation) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onAnimationEnd(Animation animation) { |
|
|
|
|
|
|
|
if (mProgress >= 100) { |
|
|
|
|
|
|
|
fadeOut(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void onAnimationRepeat(Animation animation) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mAnimation.setDuration(300); |
|
|
|
|
|
|
|
mAnimation.setInterpolator(new DecelerateInterpolator()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -137,6 +85,16 @@ public class AnimatedProgressBar extends LinearLayout { |
|
|
|
return mProgress; |
|
|
|
return mProgress; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final Paint mPaint = new Paint(); |
|
|
|
|
|
|
|
private final Rect mRect = new Rect(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
protected void onDraw(Canvas canvas) { |
|
|
|
|
|
|
|
mPaint.setColor(mProgressColor); |
|
|
|
|
|
|
|
mPaint.setStrokeWidth(10); |
|
|
|
|
|
|
|
mRect.right = mRect.left + mDrawWidth; |
|
|
|
|
|
|
|
canvas.drawRect(mRect, mPaint); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* sets the progress as an integer value between 0 and 100. |
|
|
|
* sets the progress as an integer value between 0 and 100. |
|
|
@ -154,29 +112,30 @@ public class AnimatedProgressBar extends LinearLayout { |
|
|
|
progress = 0; |
|
|
|
progress = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (mProgressView.getAlpha() < 1.0f && progress < 100) { |
|
|
|
if (this.getAlpha() < 1.0f) { |
|
|
|
fadeIn(); |
|
|
|
fadeIn(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
final int maxWidth = this.getMeasuredWidth(); // get the maximum width the view can be
|
|
|
|
int mWidth = this.getMeasuredWidth(); |
|
|
|
int initialWidth = mProgressView.getMeasuredWidth(); // get the initial width of the view
|
|
|
|
// Set the drawing bounds for the ProgressBar
|
|
|
|
|
|
|
|
mRect.left = 0; |
|
|
|
|
|
|
|
mRect.top = 0; |
|
|
|
|
|
|
|
mRect.bottom = this.getBottom() - this.getTop(); |
|
|
|
if (progress < mProgress && !mBidirectionalAnimate) { // if the we only animate the view in one direction
|
|
|
|
if (progress < mProgress && !mBidirectionalAnimate) { // if the we only animate the view in one direction
|
|
|
|
// then reset the view width if it is less than the
|
|
|
|
// then reset the view width if it is less than the
|
|
|
|
// previous progress
|
|
|
|
// previous progress
|
|
|
|
mProgressView.getLayoutParams().width = 0; |
|
|
|
mDrawWidth = 0; |
|
|
|
mProgressView.requestLayout(); |
|
|
|
|
|
|
|
initialWidth = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else if (progress == mProgress) { // we don't need to go any farther if the progress is unchanged
|
|
|
|
} else if (progress == mProgress) { // we don't need to go any farther if the progress is unchanged
|
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mProgress = progress; // save the progress
|
|
|
|
mProgress = progress; // save the progress
|
|
|
|
|
|
|
|
|
|
|
|
final int deltaWidth = (maxWidth * mProgress / 100) - initialWidth; // calculate amount the width has to change
|
|
|
|
final int deltaWidth = (mWidth * mProgress / 100) - mDrawWidth; // calculate amount the width has to change
|
|
|
|
|
|
|
|
|
|
|
|
animateView(initialWidth, maxWidth, deltaWidth); // animate the width change
|
|
|
|
animateView(mDrawWidth, mWidth, deltaWidth); // animate the width change
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -187,18 +146,38 @@ public class AnimatedProgressBar extends LinearLayout { |
|
|
|
* @param deltaWidth is the amount by which the width of the progress view will change |
|
|
|
* @param deltaWidth is the amount by which the width of the progress view will change |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void animateView(final int initialWidth, final int maxWidth, final int deltaWidth) { |
|
|
|
private void animateView(final int initialWidth, final int maxWidth, final int deltaWidth) { |
|
|
|
mInitialWidth = initialWidth; |
|
|
|
Animation fill = new Animation() { |
|
|
|
mMaxWidth = maxWidth; |
|
|
|
|
|
|
|
mDeltaWidth = deltaWidth; |
|
|
|
@Override |
|
|
|
mAnimation.reset(); |
|
|
|
protected void applyTransformation(float interpolatedTime, Transformation t) { |
|
|
|
mProgressView.startAnimation(mAnimation); |
|
|
|
int width = initialWidth + (int) (deltaWidth * interpolatedTime); |
|
|
|
|
|
|
|
if (width <= maxWidth) { |
|
|
|
|
|
|
|
mDrawWidth = width; |
|
|
|
|
|
|
|
invalidate(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if ((1.0f - interpolatedTime) < 0.0005) { |
|
|
|
|
|
|
|
if (mProgress >= 100) { |
|
|
|
|
|
|
|
fadeOut(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean willChangeBounds() { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fill.setDuration(500); |
|
|
|
|
|
|
|
fill.setInterpolator(new DecelerateInterpolator()); |
|
|
|
|
|
|
|
this.startAnimation(fill); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* fades in the progress bar |
|
|
|
* fades in the progress bar |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void fadeIn() { |
|
|
|
private void fadeIn() { |
|
|
|
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(mProgressView, "alpha", 1.0f); |
|
|
|
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(this, "alpha", 1.0f); |
|
|
|
fadeIn.setDuration(200); |
|
|
|
fadeIn.setDuration(200); |
|
|
|
fadeIn.setInterpolator(new DecelerateInterpolator()); |
|
|
|
fadeIn.setInterpolator(new DecelerateInterpolator()); |
|
|
|
fadeIn.start(); |
|
|
|
fadeIn.start(); |
|
|
@ -208,7 +187,7 @@ public class AnimatedProgressBar extends LinearLayout { |
|
|
|
* fades out the progress bar |
|
|
|
* fades out the progress bar |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void fadeOut() { |
|
|
|
private void fadeOut() { |
|
|
|
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(mProgressView, "alpha", 0.0f); |
|
|
|
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(this, "alpha", 0.0f); |
|
|
|
fadeOut.setDuration(200); |
|
|
|
fadeOut.setDuration(200); |
|
|
|
fadeOut.setInterpolator(new DecelerateInterpolator()); |
|
|
|
fadeOut.setInterpolator(new DecelerateInterpolator()); |
|
|
|
fadeOut.start(); |
|
|
|
fadeOut.start(); |
|
|
|