daily pastebin goal
2%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import android.view.animation.AccelerateInterpolator;
  2. import android.view.animation.Interpolator;
  3. import android.widget.TextView;
  4.  
  5. public class CountingHelper implements Runnable {
  6.  
  7.     private final long mDuration;
  8.     private final int mStartingValue;
  9.     private final int mFinalValue;
  10.  
  11.     private final TextView mTextView;
  12.  
  13.     private final int mValueRange;
  14.  
  15.     private long mProgressTime = 0;
  16.     private long mLastUpdateTime = 0;
  17.  
  18.  
  19.     private Interpolator mInterpolator = new AccelerateInterpolator();
  20.  
  21.     public CountingHelper(int startingValue, int finalValue, int duration, TextView textView) {
  22.         mDuration = duration;
  23.         mStartingValue = startingValue;
  24.         mFinalValue = finalValue;
  25.         mTextView = textView;
  26.  
  27.         mValueRange = mFinalValue - mStartingValue;
  28.  
  29.         mLastUpdateTime = System.currentTimeMillis();
  30.     }
  31.  
  32.     @Override
  33.     public void run() {
  34.         long now = System.currentTimeMillis();
  35.         mProgressTime += (now - mLastUpdateTime);
  36.         boolean shouldSchedule = true;
  37.         if (mProgressTime >= mDuration) {
  38.             mProgressTime = mDuration;
  39.             shouldSchedule = false;
  40.         }
  41.         mTextView.setText(String.valueOf(getCurrentValue()));
  42.         if (shouldSchedule) {
  43.             mTextView.postOnAnimation(this);
  44.         }
  45.     }
  46.  
  47.     private int getCurrentValue() {
  48.         if (mProgressTime >= mDuration) {
  49.             return mFinalValue;
  50.         }
  51.         return (int) (mStartingValue + (mInterpolator.getInterpolation(
  52.                 (float) mProgressTime / (float) mDuration) * mValueRange));
  53.     }
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top