blob: 113a21f41a004886ff412accd77b4643914ce794 [file] [log] [blame]
Chet Haase0d1c27a2014-11-03 18:35:16 +00001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Chet Haase051d35e2010-12-14 07:20:58 -080017package android.animation;
18
Chet Haase0d1c27a2014-11-03 18:35:16 +000019import android.view.animation.AnimationUtils;
20
Chet Haase051d35e2010-12-14 07:20:58 -080021/**
Chet Haasea33de552012-02-03 16:28:24 -080022 * This class provides a simple callback mechanism to listeners that is synchronized with all
23 * other animators in the system. There is no duration, interpolation, or object value-setting
24 * with this Animator. Instead, it is simply started, after which it proceeds to send out events
25 * on every animation frame to its TimeListener (if set), with information about this animator,
26 * the total elapsed time, and the elapsed time since the previous animation frame.
Chet Haase051d35e2010-12-14 07:20:58 -080027 */
28public class TimeAnimator extends ValueAnimator {
29
30 private TimeListener mListener;
31 private long mPreviousTime = -1;
32
33 @Override
Daniel Sandler08d05e32012-08-08 16:39:54 -040034 public void start() {
35 mPreviousTime = -1;
36 super.start();
37 }
38
39 @Override
Doris Liu3618d302015-08-14 11:11:08 -070040 boolean animateBasedOnTime(long currentTime) {
Chet Haase051d35e2010-12-14 07:20:58 -080041 if (mListener != null) {
42 long totalTime = currentTime - mStartTime;
43 long deltaTime = (mPreviousTime < 0) ? 0 : (currentTime - mPreviousTime);
44 mPreviousTime = currentTime;
45 mListener.onTimeUpdate(this, totalTime, deltaTime);
46 }
47 return false;
48 }
49
Chet Haase0d1c27a2014-11-03 18:35:16 +000050 @Override
51 public void setCurrentPlayTime(long playTime) {
52 long currentTime = AnimationUtils.currentAnimationTimeMillis();
53 mStartTime = Math.max(mStartTime, currentTime - playTime);
Jeff Brownc42b28d2015-04-06 19:49:02 -070054 mStartTimeCommitted = true; // do not allow start time to be compensated for jank
Doris Liu3618d302015-08-14 11:11:08 -070055 animateBasedOnTime(currentTime);
Chet Haase0d1c27a2014-11-03 18:35:16 +000056 }
57
Chet Haase051d35e2010-12-14 07:20:58 -080058 /**
59 * Sets a listener that is sent update events throughout the life of
60 * an animation.
61 *
62 * @param listener the listener to be set.
63 */
64 public void setTimeListener(TimeListener listener) {
65 mListener = listener;
66 }
67
68 @Override
69 void animateValue(float fraction) {
70 // Noop
71 }
72
73 @Override
74 void initAnimation() {
75 // noop
76 }
77
78 /**
79 * Implementors of this interface can set themselves as update listeners
80 * to a <code>TimeAnimator</code> instance to receive callbacks on every animation
81 * frame to receive the total time since the animator started and the delta time
Chet Haasea33de552012-02-03 16:28:24 -080082 * since the last frame. The first time the listener is called,
83 * deltaTime will be zero. The same is true for totalTime, unless the animator was
84 * set to a specific {@link ValueAnimator#setCurrentPlayTime(long) currentPlayTime}
85 * prior to starting.
Chet Haase051d35e2010-12-14 07:20:58 -080086 */
87 public static interface TimeListener {
88 /**
89 * <p>Notifies listeners of the occurrence of another frame of the animation,
90 * along with information about the elapsed time.</p>
91 *
92 * @param animation The animator sending out the notification.
Chet Haasea33de552012-02-03 16:28:24 -080093 * @param totalTime The total time elapsed since the animator started, in milliseconds.
94 * @param deltaTime The time elapsed since the previous frame, in milliseconds.
Chet Haase051d35e2010-12-14 07:20:58 -080095 */
96 void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime);
97
98 }
99}