From 0116481022893c2ed9ee5447dcd1a01a8f2d387d Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Tue, 24 Mar 2015 16:13:08 -0400 Subject: [PATCH] Using a smoother progress bar --- res/layout/animated_progress_bar.xml | 9 +- .../lightning/AnimatedProgressBar.java | 133 ++++++++---------- .../browser/lightning/BrowserActivity.java | 5 - .../browser/lightning/BrowserController.java | 1 - 4 files changed, 58 insertions(+), 90 deletions(-) diff --git a/res/layout/animated_progress_bar.xml b/res/layout/animated_progress_bar.xml index 16b861b..7bb4ad1 100644 --- a/res/layout/animated_progress_bar.xml +++ b/res/layout/animated_progress_bar.xml @@ -2,12 +2,7 @@ - - + android:layout_height="5dp" + android:orientation="horizontal" > \ No newline at end of file diff --git a/src/acr/browser/lightning/AnimatedProgressBar.java b/src/acr/browser/lightning/AnimatedProgressBar.java index fbe1663..187f215 100644 --- a/src/acr/browser/lightning/AnimatedProgressBar.java +++ b/src/acr/browser/lightning/AnimatedProgressBar.java @@ -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(); diff --git a/src/acr/browser/lightning/BrowserActivity.java b/src/acr/browser/lightning/BrowserActivity.java index 2d77a86..13b1812 100644 --- a/src/acr/browser/lightning/BrowserActivity.java +++ b/src/acr/browser/lightning/BrowserActivity.java @@ -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(); diff --git a/src/acr/browser/lightning/BrowserController.java b/src/acr/browser/lightning/BrowserController.java index f8816e6..85eb544 100644 --- a/src/acr/browser/lightning/BrowserController.java +++ b/src/acr/browser/lightning/BrowserController.java @@ -48,7 +48,6 @@ public interface BrowserController { public void openBookmarkPage(WebView view); - public boolean isActionBarShowing(); public void closeEmptyTab();