Browse Source

Switch to progress bar library

master
Anthony Restaino 8 years ago
parent
commit
a0ae42dbb9
  1. 3
      .gitmodules
  2. 1
      AnimatedProgressBar
  3. 2
      app/build.gradle
  4. 2
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  5. 223
      app/src/main/java/acr/browser/lightning/view/AnimatedProgressBar.java
  6. 6
      app/src/main/res/layout/toolbar.xml
  7. 6
      app/src/main/res/values/attr.xml
  8. 2
      settings.gradle

3
.gitmodules vendored

@ -5,3 +5,6 @@ @@ -5,3 +5,6 @@
[submodule "Bonsai"]
path = Bonsai
url = https://github.com/anthonycr/Bonsai.git
[submodule "AnimatedProgressBar"]
path = AnimatedProgressBar
url = git@github.com:anthonycr/AnimatedProgressBar.git

1
AnimatedProgressBar

@ -0,0 +1 @@ @@ -0,0 +1 @@
Subproject commit 2d7f756be8f562b2cedca46de7d2ff24f9383040

2
app/build.gradle

@ -103,6 +103,8 @@ dependencies { @@ -103,6 +103,8 @@ dependencies {
compile project(':bonsai')
compile project(':animated-progress-bar')
// memory leak analysis
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta2'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta2'

2
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -84,6 +84,7 @@ import android.widget.TextView.OnEditorActionListener; @@ -84,6 +84,7 @@ import android.widget.TextView.OnEditorActionListener;
import android.widget.VideoView;
import com.anthonycr.grant.PermissionsManager;
import com.anthonycr.progress.AnimatedProgressBar;
import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;
@ -126,7 +127,6 @@ import acr.browser.lightning.utils.ThemeUtils; @@ -126,7 +127,6 @@ import acr.browser.lightning.utils.ThemeUtils;
import acr.browser.lightning.utils.UrlUtils;
import acr.browser.lightning.utils.Utils;
import acr.browser.lightning.utils.WebUtils;
import acr.browser.lightning.view.AnimatedProgressBar;
import acr.browser.lightning.view.LightningView;
import acr.browser.lightning.view.SearchView;
import butterknife.Bind;

223
app/src/main/java/acr/browser/lightning/view/AnimatedProgressBar.java

@ -1,223 +0,0 @@ @@ -1,223 +0,0 @@
package acr.browser.lightning.view;
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.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Transformation;
import android.widget.LinearLayout;
import acr.browser.lightning.R;
/**
* Copyright 11/4/2014 Anthony Restaino
* <p/>
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.
*/
public class AnimatedProgressBar extends LinearLayout {
private int mProgress = 0;
private boolean mBidirectionalAnimate = true;
private int mDrawWidth = 0;
private int mProgressColor;
public AnimatedProgressBar(@NonNull Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public AnimatedProgressBar(@NonNull Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
/**
* Initialize the AnimatedProgressBar
*
* @param context is the context passed by the constructor
* @param attrs is the attribute set passed by the constructor
*/
private void init(@NonNull final Context context, AttributeSet attrs) {
TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AnimatedProgressBar, 0, 0);
int backgroundColor;
try { // Retrieve the style of the progress bar that the user hopefully set
int DEFAULT_BACKGROUND_COLOR = 0x424242;
int DEFAULT_PROGRESS_COLOR = 0x2196f3;
backgroundColor = array.getColor(R.styleable.AnimatedProgressBar_backgroundColor, DEFAULT_BACKGROUND_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);
this.setBackgroundColor(backgroundColor); // set the background color for this view
}
/**
* Returns the current progress value between 0 and 100
*
* @return progress of the view
*/
public int getProgress() {
return mProgress;
}
private final Paint mPaint = new Paint();
private final Rect mRect = new Rect();
@Override
protected void onDraw(@NonNull 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.
* Values above or below that interval will be adjusted to their
* nearest value within the interval, i.e. setting a value of 150 will have
* the effect of setting the progress to 100. You cannot trick us.
*
* @param progress an integer between 0 and 100
*/
public void setProgress(int progress) {
if (progress > 100) { // progress cannot be greater than 100
progress = 100;
} else if (progress < 0) { // progress cannot be less than 0
progress = 0;
}
if (this.getAlpha() < 1.0f) {
fadeIn();
}
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
mDrawWidth = 0;
} else if (progress == mProgress) { // we don't need to go any farther if the progress is unchanged
if (progress == 100) {
fadeOut();
}
return;
}
mProgress = progress; // save the progress
final int deltaWidth = (mWidth * mProgress / 100) - mDrawWidth; // calculate amount the width has to change
animateView(mDrawWidth, mWidth, deltaWidth); // animate the width change
}
/**
* private method used to create and run the animation used to change the progress
*
* @param initialWidth is the width at which the progress starts at
* @param maxWidth is the maximum width (total width of the view)
* @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) {
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(this, "alpha", 1.0f);
fadeIn.setDuration(200);
fadeIn.setInterpolator(new DecelerateInterpolator());
fadeIn.start();
}
/**
* fades out the progress bar
*/
private void fadeOut() {
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(this, "alpha", 0.0f);
fadeOut.setDuration(200);
fadeOut.setInterpolator(new DecelerateInterpolator());
fadeOut.start();
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
this.mProgress = bundle.getInt("progressState");
state = bundle.getParcelable("instanceState");
}
super.onRestoreInstanceState(state);
}
@NonNull
@Override
protected Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable("instanceState", super.onSaveInstanceState());
bundle.putInt("progressState", mProgress);
return bundle;
}
}

6
app/src/main/res/layout/toolbar.xml

@ -5,9 +5,9 @@ @@ -5,9 +5,9 @@
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:background="?attr/colorPrimary"
android:elevation="2dp"
android:fitsSystemWindows="true"
android:orientation="vertical">
<FrameLayout
@ -31,11 +31,11 @@ @@ -31,11 +31,11 @@
android:layout_height="wrap_content"
android:background="?attr/progressBackgroundColor">
<acr.browser.lightning.view.AnimatedProgressBar
<com.anthonycr.progress.AnimatedProgressBar
android:id="@+id/progress_view"
android:layout_width="match_parent"
android:layout_height="2dp"
custom:backgroundColor="#00000000"
android:background="@android:color/transparent"
custom:bidirectionalAnimate="false"
custom:progressColor="?attr/colorAccent"/>
</LinearLayout>

6
app/src/main/res/values/attr.xml

@ -12,10 +12,4 @@ @@ -12,10 +12,4 @@
<attr name="dividerColor" format="reference|color" />
<attr name="selectedBackground" format="reference|color" />
<declare-styleable name="AnimatedProgressBar">
<attr name="progressColor" format="color" />
<attr name="backgroundColor" format="color" />
<attr name="bidirectionalAnimate" format="boolean" />
</declare-styleable>
</resources>

2
settings.gradle

@ -1,6 +1,8 @@ @@ -1,6 +1,8 @@
include ':app'
include ':libnetcipher'
include ':bonsai'
include ':animated-progress-bar'
project(':libnetcipher').projectDir = new File(rootProject.projectDir, 'external/netcipher/libnetcipher')
project(':bonsai').projectDir = new File(rootProject.projectDir, 'Bonsai/library')
project(':animated-progress-bar').projectDir = new File(rootProject.projectDir, 'AnimatedProgressBar/library')
Loading…
Cancel
Save