Java Code Examples for android.animation.ValueAnimator#setCurrentPlayTime()

The following examples show how to use android.animation.ValueAnimator#setCurrentPlayTime() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RecordingThrobberView.java    From science-journal with Apache License 2.0 6 votes vote down vote up
public void startAnimation() {
  for (int i = 0; i < NUMBER_BARS; i++) {
    final int index = i;
    ValueAnimator animator = ValueAnimator.ofFloat(0, 100);
    animator.setDuration(MS_PER_CYCLE);
    animator.setRepeatMode(ValueAnimator.REVERSE);
    animator.setRepeatCount(ValueAnimator.INFINITE);
    animator.setInterpolator(new AccelerateDecelerateInterpolator());
    // Get sorta random starts using some prime numbers and modulo math
    animator.setCurrentPlayTime(
        (long) (MS_PER_CYCLE * (i * 3 + 7 * 1.0 % NUMBER_BARS) / NUMBER_BARS));
    animator.addUpdateListener(
        valueAnimator -> {
          animatedFraction[index] = valueAnimator.getAnimatedFraction();
          // Coordinate the invalidates for performance.
          postInvalidateOnAnimation();
        });
    animator.start();
    stopped.happensNext().subscribe(() -> animator.end());
  }
}
 
Example 2
Source File: JumpingBeansSpan.java    From AutoLoadListView with Apache License 2.0 6 votes vote down vote up
@Override
public void onAnimationUpdate(ValueAnimator animation) {
    // No need for synchronization as this always run on main thread anyway
    TextView v = textView.get();
    if (v != null) {
        if (isAttachedToHierarchy(v)) {
            shift = (int) animation.getAnimatedValue();
            v.invalidate();
        } else {
            animation.setCurrentPlayTime(0);
            animation.start();
        }
    } else {
        // The textview has been destroyed and teardown() hasn't been called
        teardown();
        if (BuildConfig.DEBUG) {
            Log.e("JumpingBeans", "!!! Remember to call JumpingBeans.stopJumping() when appropriate !!!");
        }
    }
}
 
Example 3
Source File: JumpingBeansSpan.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
@Override
public void onAnimationUpdate(ValueAnimator animation) {
    // No need for synchronization as this always run on main thread anyway
    TextView v = textView.get();
    if (v != null) {
        if (isAttachedToHierarchy(v)) {
            shift = (int) animation.getAnimatedValue();
            v.invalidate();
        } else {
            animation.setCurrentPlayTime(0);
            animation.start();
        }
    } else {
        // The textview has been destroyed and teardown() hasn't been called
        teardown();

            Logs.e("JumpingBeans", "!!! Remember to call JumpingBeans.stopJumping() when appropriate !!!");

    }
}
 
Example 4
Source File: JumpingBeansSpan.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
@Override
public void onAnimationUpdate(ValueAnimator animation) {
    // No need for synchronization as this always run on main thread anyway
    TextView v = textView.get();
    if (v != null) {
        if (isAttachedToHierarchy(v)) {
            shift = (int) animation.getAnimatedValue();
            v.invalidate();
        } else {
            animation.setCurrentPlayTime(0);
            animation.start();
        }
    } else {
        // The textview has been destroyed and teardown() hasn't been called
        teardown();

            Log.e("JumpingBeans", "!!! Remember to call JumpingBeans.stopJumping() when appropriate !!!");

    }
}
 
Example 5
Source File: FirstFrameAnimatorHelper.java    From LaunchEnr with GNU General Public License v3.0 4 votes vote down vote up
public void onAnimationUpdate(final ValueAnimator animation) {
    final long currentTime = System.currentTimeMillis();
    if (mStartTime == -1) {
        mStartFrame = sGlobalFrameCounter;
        mStartTime = currentTime;
    }

    final long currentPlayTime = animation.getCurrentPlayTime();
    boolean isFinalFrame = Float.compare(1f, animation.getAnimatedFraction()) == 0;

    if (!mHandlingOnAnimationUpdate &&
        sVisible &&
        // If the current play time exceeds the duration, or the animated fraction is 1,
        // the animation will get finished, even if we call setCurrentPlayTime -- therefore
        // don't adjust the animation in that case
        currentPlayTime < animation.getDuration() && !isFinalFrame) {
        mHandlingOnAnimationUpdate = true;
        long frameNum = sGlobalFrameCounter - mStartFrame;
        // If we haven't drawn our first frame, reset the time to t = 0
        // (give up after MAX_DELAY ms of waiting though - might happen, for example, if we
        // are no longer in the foreground and no frames are being rendered ever)
        if (frameNum == 0 && currentTime < mStartTime + MAX_DELAY && currentPlayTime > 0) {
            // The first frame on animations doesn't always trigger an invalidate...
            // force an invalidate here to make sure the animation continues to advance
            mTarget.getRootView().invalidate();
            animation.setCurrentPlayTime(0);
        // For the second frame, if the first frame took more than 16ms,
        // adjust the start time and pretend it took only 16ms anyway. This
        // prevents a large jump in the animation due to an expensive first frame
        } else if (frameNum == 1 && currentTime < mStartTime + MAX_DELAY &&
                   !mAdjustedSecondFrameTime &&
                   currentTime > mStartTime + IDEAL_FRAME_DURATION &&
                   currentPlayTime > IDEAL_FRAME_DURATION) {
            animation.setCurrentPlayTime(IDEAL_FRAME_DURATION);
            mAdjustedSecondFrameTime = true;
        } else {
            if (frameNum > 1) {
                mTarget.post(new Runnable() {
                        public void run() {
                            animation.removeUpdateListener(FirstFrameAnimatorHelper.this);
                        }
                    });
            }
        }
        mHandlingOnAnimationUpdate = false;
    }
}
 
Example 6
Source File: FirstFrameAnimatorHelper.java    From Trebuchet with GNU General Public License v3.0 4 votes vote down vote up
public void onAnimationUpdate(final ValueAnimator animation) {
    final long currentTime = System.currentTimeMillis();
    if (mStartTime == -1) {
        mStartFrame = sGlobalFrameCounter;
        mStartTime = currentTime;
    }

    final long currentPlayTime = animation.getCurrentPlayTime();
    boolean isFinalFrame = Float.compare(1f, animation.getAnimatedFraction()) == 0;

    if (!mHandlingOnAnimationUpdate &&
        sVisible &&
        // If the current play time exceeds the duration, or the animated fraction is 1,
        // the animation will get finished, even if we call setCurrentPlayTime -- therefore
        // don't adjust the animation in that case
        currentPlayTime < animation.getDuration() && !isFinalFrame) {
        mHandlingOnAnimationUpdate = true;
        long frameNum = sGlobalFrameCounter - mStartFrame;
        // If we haven't drawn our first frame, reset the time to t = 0
        // (give up after MAX_DELAY ms of waiting though - might happen, for example, if we
        // are no longer in the foreground and no frames are being rendered ever)
        if (frameNum == 0 && currentTime < mStartTime + MAX_DELAY && currentPlayTime > 0) {
            // The first frame on animations doesn't always trigger an invalidate...
            // force an invalidate here to make sure the animation continues to advance
            mTarget.getRootView().invalidate();
            animation.setCurrentPlayTime(0);
        // For the second frame, if the first frame took more than 16ms,
        // adjust the start time and pretend it took only 16ms anyway. This
        // prevents a large jump in the animation due to an expensive first frame
        } else if (frameNum == 1 && currentTime < mStartTime + MAX_DELAY &&
                   !mAdjustedSecondFrameTime &&
                   currentTime > mStartTime + IDEAL_FRAME_DURATION &&
                   currentPlayTime > IDEAL_FRAME_DURATION) {
            animation.setCurrentPlayTime(IDEAL_FRAME_DURATION);
            mAdjustedSecondFrameTime = true;
        } else {
            if (frameNum > 1) {
                mTarget.post(new Runnable() {
                        public void run() {
                            animation.removeUpdateListener(FirstFrameAnimatorHelper.this);
                        }
                    });
            }
            if (DEBUG) print(animation);
        }
        mHandlingOnAnimationUpdate = false;
    } else {
        if (DEBUG) print(animation);
    }
}
 
Example 7
Source File: FirstFrameAnimatorHelper.java    From TurboLauncher with Apache License 2.0 4 votes vote down vote up
public void onAnimationUpdate(final ValueAnimator animation) {
    final long currentTime = System.currentTimeMillis();
    if (mStartTime == -1) {
        mStartFrame = sGlobalFrameCounter;
        mStartTime = currentTime;
    }

    final long currentPlayTime = animation.getCurrentPlayTime();
    if (!mHandlingOnAnimationUpdate &&
        sVisible &&
        // If the current play time exceeds the duration, the animation
        // will get finished, even if we call setCurrentPlayTime -- therefore
        // don't adjust the animation in that case
        currentPlayTime < animation.getDuration()) {
        mHandlingOnAnimationUpdate = true;
        long frameNum = sGlobalFrameCounter - mStartFrame;
        // If we haven't drawn our first frame, reset the time to t = 0
        // (give up after MAX_DELAY ms of waiting though - might happen, for example, if we
        // are no longer in the foreground and no frames are being rendered ever)
        if (frameNum == 0 && currentTime < mStartTime + MAX_DELAY && currentPlayTime > 0) {
            // The first frame on animations doesn't always trigger an invalidate...
            // force an invalidate here to make sure the animation continues to advance
            mTarget.getRootView().invalidate();
            animation.setCurrentPlayTime(0);
        // For the second frame, if the first frame took more than 16ms,
        // adjust the start time and pretend it took only 16ms anyway. This
        // prevents a large jump in the animation due to an expensive first frame
        } else if (frameNum == 1 && currentTime < mStartTime + MAX_DELAY &&
                   !mAdjustedSecondFrameTime &&
                   currentTime > mStartTime + IDEAL_FRAME_DURATION &&
                   currentPlayTime > IDEAL_FRAME_DURATION) {
            animation.setCurrentPlayTime(IDEAL_FRAME_DURATION);
            mAdjustedSecondFrameTime = true;
        } else {
            if (frameNum > 1) {
                mTarget.post(new Runnable() {
                        public void run() {
                            animation.removeUpdateListener(FirstFrameAnimatorHelper.this);
                        }
                    });
            }
            if (DEBUG) print(animation);
        }
        mHandlingOnAnimationUpdate = false;
    } else {
        if (DEBUG) print(animation);
    }
}
 
Example 8
Source File: FirstFrameAnimatorHelper.java    From LB-Launcher with Apache License 2.0 4 votes vote down vote up
public void onAnimationUpdate(final ValueAnimator animation) {
    final long currentTime = System.currentTimeMillis();
    if (mStartTime == -1) {
        mStartFrame = sGlobalFrameCounter;
        mStartTime = currentTime;
    }

    final long currentPlayTime = animation.getCurrentPlayTime();
    boolean isFinalFrame = Float.compare(1f, animation.getAnimatedFraction()) == 0;

    if (!mHandlingOnAnimationUpdate &&
        sVisible &&
        // If the current play time exceeds the duration, or the animated fraction is 1,
        // the animation will get finished, even if we call setCurrentPlayTime -- therefore
        // don't adjust the animation in that case
        currentPlayTime < animation.getDuration() && !isFinalFrame) {
        mHandlingOnAnimationUpdate = true;
        long frameNum = sGlobalFrameCounter - mStartFrame;
        // If we haven't drawn our first frame, reset the time to t = 0
        // (give up after MAX_DELAY ms of waiting though - might happen, for example, if we
        // are no longer in the foreground and no frames are being rendered ever)
        if (frameNum == 0 && currentTime < mStartTime + MAX_DELAY && currentPlayTime > 0) {
            // The first frame on animations doesn't always trigger an invalidate...
            // force an invalidate here to make sure the animation continues to advance
            mTarget.getRootView().invalidate();
            animation.setCurrentPlayTime(0);
        // For the second frame, if the first frame took more than 16ms,
        // adjust the start time and pretend it took only 16ms anyway. This
        // prevents a large jump in the animation due to an expensive first frame
        } else if (frameNum == 1 && currentTime < mStartTime + MAX_DELAY &&
                   !mAdjustedSecondFrameTime &&
                   currentTime > mStartTime + IDEAL_FRAME_DURATION &&
                   currentPlayTime > IDEAL_FRAME_DURATION) {
            animation.setCurrentPlayTime(IDEAL_FRAME_DURATION);
            mAdjustedSecondFrameTime = true;
        } else {
            if (frameNum > 1) {
                mTarget.post(new Runnable() {
                        public void run() {
                            animation.removeUpdateListener(FirstFrameAnimatorHelper.this);
                        }
                    });
            }
            if (DEBUG) print(animation);
        }
        mHandlingOnAnimationUpdate = false;
    } else {
        if (DEBUG) print(animation);
    }
}