Using a smoother progress bar
This commit is contained in:
parent
b87eb5e90e
commit
0116481022
@ -2,12 +2,7 @@
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/progress_background"
|
||||
android:layout_width="match_parent"
|
||||
android:orientation="horizontal"
|
||||
android:layout_height="5dp">
|
||||
|
||||
<View
|
||||
android:id="@android:id/progress"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"/>
|
||||
android:layout_height="5dp"
|
||||
android:orientation="horizontal" >
|
||||
|
||||
</LinearLayout>
|
@ -3,13 +3,14 @@ package acr.browser.lightning;
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.animation.Animation;
|
||||
import android.view.animation.Animation.AnimationListener;
|
||||
import android.view.animation.DecelerateInterpolator;
|
||||
import android.view.animation.Transformation;
|
||||
import android.widget.LinearLayout;
|
||||
@ -31,13 +32,10 @@ import android.widget.LinearLayout;
|
||||
*/
|
||||
public class AnimatedProgressBar extends LinearLayout {
|
||||
|
||||
private View mProgressView;
|
||||
private int mProgress = 0;
|
||||
private boolean mBidirectionalAnimate = true;
|
||||
private Animation mAnimation;
|
||||
int mInitialWidth;
|
||||
int mDeltaWidth;
|
||||
int mMaxWidth;
|
||||
private int mDrawWidth = 0;
|
||||
private int mProgressColor;
|
||||
|
||||
public AnimatedProgressBar(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
@ -49,11 +47,6 @@ public class AnimatedProgressBar extends LinearLayout {
|
||||
init(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the AnimatedProgressBar
|
||||
*
|
||||
@ -61,71 +54,26 @@ public class AnimatedProgressBar extends LinearLayout {
|
||||
* @param attrs is the attribute set passed by the constructor
|
||||
*/
|
||||
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);
|
||||
int backgroundColor;
|
||||
int progressColor;
|
||||
try { // Retrieve the style of the progress bar that the user hopefully set
|
||||
|
||||
int DEFAULT_BACKGROUND_COLOR = 0x00000000;
|
||||
int DEFAULT_BACKGROUND_COLOR = 0x424242;
|
||||
int DEFAULT_PROGRESS_COLOR = 0x2196f3;
|
||||
|
||||
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);
|
||||
|
||||
} finally {
|
||||
|
||||
array.recycle();
|
||||
|
||||
}
|
||||
|
||||
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
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
|
||||
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;
|
||||
}
|
||||
|
||||
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.
|
||||
@ -154,29 +112,30 @@ public class AnimatedProgressBar extends LinearLayout {
|
||||
progress = 0;
|
||||
}
|
||||
|
||||
if (mProgressView.getAlpha() < 1.0f && progress < 100) {
|
||||
if (this.getAlpha() < 1.0f) {
|
||||
fadeIn();
|
||||
}
|
||||
|
||||
final int maxWidth = this.getMeasuredWidth(); // get the maximum width the view can be
|
||||
int initialWidth = mProgressView.getMeasuredWidth(); // get the initial width of the view
|
||||
|
||||
int mWidth = this.getMeasuredWidth();
|
||||
// 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
|
||||
// then reset the view width if it is less than the
|
||||
// previous progress
|
||||
mProgressView.getLayoutParams().width = 0;
|
||||
mProgressView.requestLayout();
|
||||
initialWidth = 0;
|
||||
|
||||
mDrawWidth = 0;
|
||||
} else if (progress == mProgress) { // we don't need to go any farther if the progress is unchanged
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
private void animateView(final int initialWidth, final int maxWidth, final int deltaWidth) {
|
||||
mInitialWidth = initialWidth;
|
||||
mMaxWidth = maxWidth;
|
||||
mDeltaWidth = deltaWidth;
|
||||
mAnimation.reset();
|
||||
mProgressView.startAnimation(mAnimation);
|
||||
Animation fill = new Animation() {
|
||||
|
||||
@Override
|
||||
protected void applyTransformation(float interpolatedTime, Transformation t) {
|
||||
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
|
||||
*/
|
||||
private void fadeIn() {
|
||||
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(mProgressView, "alpha", 1.0f);
|
||||
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(this, "alpha", 1.0f);
|
||||
fadeIn.setDuration(200);
|
||||
fadeIn.setInterpolator(new DecelerateInterpolator());
|
||||
fadeIn.start();
|
||||
@ -208,7 +187,7 @@ public class AnimatedProgressBar extends LinearLayout {
|
||||
* fades out the progress bar
|
||||
*/
|
||||
private void fadeOut() {
|
||||
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(mProgressView, "alpha", 0.0f);
|
||||
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(this, "alpha", 0.0f);
|
||||
fadeOut.setDuration(200);
|
||||
fadeOut.setInterpolator(new DecelerateInterpolator());
|
||||
fadeOut.start();
|
||||
|
@ -2626,11 +2626,6 @@ public class BrowserActivity extends ActionBarActivity implements BrowserControl
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isActionBarShowing() {
|
||||
return mToolbarLayout.isShown();
|
||||
}
|
||||
|
||||
// Override this, use finish() for Incognito, moveTaskToBack for Main
|
||||
public void closeActivity() {
|
||||
finish();
|
||||
|
@ -48,7 +48,6 @@ public interface BrowserController {
|
||||
|
||||
public void openBookmarkPage(WebView view);
|
||||
|
||||
public boolean isActionBarShowing();
|
||||
|
||||
public void closeEmptyTab();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user