• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package android.animation;
18 
19 import android.view.animation.AnimationUtils;
20 
21 /**
22  * 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.
27  */
28 public class TimeAnimator extends ValueAnimator {
29 
30     private TimeListener mListener;
31     private long mPreviousTime = -1;
32 
33     @Override
start()34     public void start() {
35         mPreviousTime = -1;
36         super.start();
37     }
38 
39     @Override
animateBasedOnTime(long currentTime)40     boolean animateBasedOnTime(long currentTime) {
41         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 
50     @Override
setCurrentPlayTime(long playTime)51     public void setCurrentPlayTime(long playTime) {
52         long currentTime = AnimationUtils.currentAnimationTimeMillis();
53         mStartTime = Math.max(mStartTime, currentTime - playTime);
54         mStartTimeCommitted = true; // do not allow start time to be compensated for jank
55         animateBasedOnTime(currentTime);
56     }
57 
58     /**
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      */
setTimeListener(TimeListener listener)64     public void setTimeListener(TimeListener listener) {
65         mListener = listener;
66     }
67 
68     @Override
animateValue(float fraction)69     void animateValue(float fraction) {
70         // Noop
71     }
72 
73     @Override
initAnimation()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
82      * 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.
86      */
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.
93          * @param totalTime The total time elapsed since the animator started, in milliseconds.
94          * @param deltaTime The time elapsed since the previous frame, in milliseconds.
95          */
onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime)96         void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime);
97 
98     }
99 }
100