Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 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 | |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 17 | package android.animation; |
| 18 | |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 19 | import android.view.animation.AnimationUtils; |
| 20 | |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 21 | /** |
Chet Haase | a33de55 | 2012-02-03 16:28:24 -0800 | [diff] [blame] | 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. |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 27 | */ |
| 28 | public class TimeAnimator extends ValueAnimator { |
| 29 | |
| 30 | private TimeListener mListener; |
| 31 | private long mPreviousTime = -1; |
| 32 | |
| 33 | @Override |
Daniel Sandler | 08d05e3 | 2012-08-08 16:39:54 -0400 | [diff] [blame] | 34 | public void start() { |
| 35 | mPreviousTime = -1; |
| 36 | super.start(); |
| 37 | } |
| 38 | |
| 39 | @Override |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 40 | boolean animateBasedOnTime(long currentTime) { |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 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 | |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 50 | @Override |
| 51 | public void setCurrentPlayTime(long playTime) { |
| 52 | long currentTime = AnimationUtils.currentAnimationTimeMillis(); |
| 53 | mStartTime = Math.max(mStartTime, currentTime - playTime); |
Jeff Brown | c42b28d | 2015-04-06 19:49:02 -0700 | [diff] [blame] | 54 | mStartTimeCommitted = true; // do not allow start time to be compensated for jank |
Doris Liu | 3618d30 | 2015-08-14 11:11:08 -0700 | [diff] [blame] | 55 | animateBasedOnTime(currentTime); |
Chet Haase | 0d1c27a | 2014-11-03 18:35:16 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 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 | */ |
| 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 Haase | a33de55 | 2012-02-03 16:28:24 -0800 | [diff] [blame] | 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. |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 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. |
Chet Haase | a33de55 | 2012-02-03 16:28:24 -0800 | [diff] [blame] | 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. |
Chet Haase | 051d35e | 2010-12-14 07:20:58 -0800 | [diff] [blame] | 95 | */ |
| 96 | void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime); |
| 97 | |
| 98 | } |
| 99 | } |