Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to animate increase in mCurrentProgress #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,42 +1,26 @@
package com.daimajia.numberprogressbar.example;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.daimajia.numberprogressbar.NumberProgressBar;
import com.daimajia.numberprogressbar.OnProgressBarListener;

import java.util.Timer;
import java.util.TimerTask;


public class MainActivity extends ActionBarActivity implements OnProgressBarListener {
private Timer timer;
public class MainActivity extends AppCompatActivity implements OnProgressBarListener {

private NumberProgressBar bnp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bnp = (NumberProgressBar)findViewById(R.id.numberbar1);
bnp.setOnProgressBarListener(this);
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
bnp.incrementProgressBy(1);
}
});
}
}, 1000, 100);
bnp.setProgress(100,true,this);
}

@Override
Expand All @@ -61,7 +45,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
@Override
protected void onDestroy() {
super.onDestroy();
timer.cancel();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Tue Feb 24 10:50:01 CST 2015
#Tue Jun 21 15:07:22 IST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.daimajia.numberprogressbar;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
Expand All @@ -11,6 +13,9 @@
import android.util.AttributeSet;
import android.view.View;

import java.util.Timer;
import java.util.TimerTask;

import static com.daimajia.numberprogressbar.NumberProgressBar.ProgressTextVisibility.Invisible;
import static com.daimajia.numberprogressbar.NumberProgressBar.ProgressTextVisibility.Visible;

Expand Down Expand Up @@ -434,9 +439,50 @@ public void incrementProgressBy(int by) {
}

public void setProgress(int progress) {
setProgress(progress,false,null,0);
}

/**
* Sets the NumberProgressBar to the required value, with optional animation
* @param progress the progress to be set
* @param animate whether change should be incremental (not sudden)
* @param activity the calling activity, may be left null in case of no animation
*/
public void setProgress(final int progress, boolean animate, final Activity activity){
setProgress(progress,animate,activity,100);
}

/**
* Sets the NumberProgressBar to the required value, with optional animation, with the increments at defined periods
* @param progress the progress to be set
* @param animate whether change should be incremental (not sudden)
* @param activity the calling activity, may be left null in case of no animation
* @param period amount of time in milliseconds between subsequent increments.
*/
public void setProgress(final int progress, boolean animate, final Activity activity, int period){
if (progress <= getMax() && progress >= 0) {
this.mCurrentProgress = progress;
invalidate();
if(!animate || progress<mCurrentProgress){
this.mCurrentProgress = progress;
invalidate();
}else{
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if(progress>mCurrentProgress)
incrementProgressBy(1);
else {
timer.cancel();
timer.purge();
}
}
});
}
}, 0, period);
}
}
}

Expand Down