public class

TimeAnimator

extends ValueAnimator

 java.lang.Object

androidx.core.animation.Animator

androidx.core.animation.ValueAnimator

↳androidx.core.animation.TimeAnimator

Gradle dependencies

compile group: 'androidx.core', name: 'core-animation', version: '1.0.0'

  • groupId: androidx.core
  • artifactId: core-animation
  • version: 1.0.0

Artifact androidx.core:core-animation:1.0.0 it located at Google repository (https://maven.google.com/)

Overview

This class provides a simple callback mechanism to listeners that is synchronized with all other animators in the system. There is no duration, interpolation, or object value-setting with this Animator. Instead, it is simply started, after which it proceeds to send out events on every animation frame to its TimeListener (if set), with information about this animator, the total elapsed time, and the elapsed time since the previous animation frame.

Summary

Fields
from ValueAnimatorINFINITE, RESTART, REVERSE
from AnimatorDURATION_INFINITE
Constructors
publicTimeAnimator()

Methods
public voidsetCurrentPlayTime(long playTime)

Sets the position of the animation to the specified point in time.

public voidsetTimeListener(TimeAnimator.TimeListener listener)

Sets a listener that is sent update events throughout the life of an animation.

public voidstart()

Starts this animation.

from ValueAnimatorareAnimatorsEnabled, cancel, clone, doAnimationFrame, end, getAnimatedFraction, getAnimatedValue, getAnimatedValue, getCurrentPlayTime, getDuration, getFrameDelay, getInterpolator, getNameForTrace, getRepeatCount, getRepeatMode, getStartDelay, getTotalDuration, getValues, isRunning, isStarted, ofArgb, ofFloat, ofInt, ofObject, ofPropertyValuesHolder, pause, resume, reverse, setCurrentFraction, setDuration, setEvaluator, setFloatValues, setFrameDelay, setInterpolator, setIntValues, setNameForTrace, setObjectValues, setRepeatCount, setRepeatMode, setStartDelay, setValues, toString
from AnimatoraddListener, addPauseListener, addUpdateListener, isPaused, removeAllListeners, removeAllUpdateListeners, removeListener, removePauseListener, removeUpdateListener, setTarget, setupEndValues, setupStartValues
from java.lang.Objectequals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructors

public TimeAnimator()

Methods

public void start()

Starts this animation. If the animation has a nonzero startDelay, the animation will start running after that delay elapses. A non-delayed animation will have its initial value(s) set immediately, followed by calls to Animator.AnimatorListener.onAnimationStart(Animator) for any listeners of this animator.

The animation started by calling this method will be run on the thread that called this method. This thread should have a Looper on it (a runtime exception will be thrown if this is not the case). Also, if the animation will animate properties of objects in the view hierarchy, then the calling thread should be the UI thread for that view hierarchy.

public void setCurrentPlayTime(long playTime)

Sets the position of the animation to the specified point in time. This time should be between 0 and the total duration of the animation, including any repetition. If the animation has not yet been started, then it will not advance forward after it is set to this time; it will simply set the time to this value and perform any appropriate actions based on that time. If the animation is already running, then setCurrentPlayTime() will set the current playing time to this value and continue playing from that point.

Parameters:

playTime: The time, in milliseconds, to which the animation is advanced or rewound.

public void setTimeListener(TimeAnimator.TimeListener listener)

Sets a listener that is sent update events throughout the life of an animation.

Parameters:

listener: the listener to be set.

Source

/*
 * Copyright 2018 The Android Open Source Project
 *
 * 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
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

package androidx.core.animation;

import android.view.animation.AnimationUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * This class provides a simple callback mechanism to listeners that is synchronized with all
 * other animators in the system. There is no duration, interpolation, or object value-setting
 * with this Animator. Instead, it is simply started, after which it proceeds to send out events
 * on every animation frame to its TimeListener (if set), with information about this animator,
 * the total elapsed time, and the elapsed time since the previous animation frame.
 */
public class TimeAnimator extends ValueAnimator {

    private TimeListener mListener;
    private long mPreviousTime = -1;

    @Override
    public void start() {
        mPreviousTime = -1;
        super.start();
    }

    @Override
    boolean animateBasedOnTime(long currentTime) {
        if (mListener != null) {
            long totalTime = currentTime - mStartTime;
            long deltaTime = (mPreviousTime < 0) ? 0 : (currentTime - mPreviousTime);
            mPreviousTime = currentTime;
            mListener.onTimeUpdate(this, totalTime, deltaTime);
        }
        return false;
    }

    @Override
    public void setCurrentPlayTime(long playTime) {
        long currentTime = AnimationUtils.currentAnimationTimeMillis();
        mStartTime = Math.max(mStartTime, currentTime - playTime);
        animateBasedOnTime(currentTime);
    }

    /**
     * Sets a listener that is sent update events throughout the life of
     * an animation.
     *
     * @param listener the listener to be set.
     */
    public void setTimeListener(@Nullable TimeListener listener) {
        mListener = listener;
    }

    @Override
    @SuppressWarnings("MissingSuperCall")
    void animateValue(float fraction) {
        // Noop
    }

    @Override
    @SuppressWarnings("MissingSuperCall")
    void initAnimation() {
        // noop
    }

    /**
     * Implementors of this interface can set themselves as update listeners
     * to a <code>TimeAnimator</code> instance to receive callbacks on every animation
     * frame to receive the total time since the animator started and the delta time
     * since the last frame. The first time the listener is called,
     * deltaTime will be zero. The same is true for totalTime, unless the animator was
     * set to a specific {@link ValueAnimator#setCurrentPlayTime(long) currentPlayTime}
     * prior to starting.
     */
    public interface TimeListener {
        /**
         * <p>Notifies listeners of the occurrence of another frame of the animation,
         * along with information about the elapsed time.</p>
         *
         * @param animation The animator sending out the notification.
         * @param totalTimeMs The total time elapsed since the animator started, in milliseconds.
         * @param deltaTimeMs The time elapsed since the previous frame, in milliseconds.
         */
        void onTimeUpdate(@NonNull TimeAnimator animation, long totalTimeMs, long deltaTimeMs);

    }
}