Guest User

Untitled

a guest
Feb 13th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  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. }
Add Comment
Please, Sign In to add comment