10d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase/* 20d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * Copyright (C) 2010 The Android Open Source Project 30d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * 40d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * Licensed under the Apache License, Version 2.0 (the "License"); 50d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * you may not use this file except in compliance with the License. 60d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * You may obtain a copy of the License at 70d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * 80d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * http://www.apache.org/licenses/LICENSE-2.0 90d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * 100d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * Unless required by applicable law or agreed to in writing, software 110d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * distributed under the License is distributed on an "AS IS" BASIS, 120d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * See the License for the specific language governing permissions and 140d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase * limitations under the License. 150d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase */ 160d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase 17051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haasepackage android.animation; 18051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase 190d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haaseimport android.view.animation.AnimationUtils; 200d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase 21051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase/** 22a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * This class provides a simple callback mechanism to listeners that is synchronized with all 23a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * other animators in the system. There is no duration, interpolation, or object value-setting 24a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * with this Animator. Instead, it is simply started, after which it proceeds to send out events 25a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * on every animation frame to its TimeListener (if set), with information about this animator, 26a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * the total elapsed time, and the elapsed time since the previous animation frame. 27051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase */ 28051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haasepublic class TimeAnimator extends ValueAnimator { 29051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase 30051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase private TimeListener mListener; 31051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase private long mPreviousTime = -1; 32051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase 33051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase @Override 3408d05e3d1d6ade6924266296033981a96b47d5fbDaniel Sandler public void start() { 3508d05e3d1d6ade6924266296033981a96b47d5fbDaniel Sandler mPreviousTime = -1; 3608d05e3d1d6ade6924266296033981a96b47d5fbDaniel Sandler super.start(); 3708d05e3d1d6ade6924266296033981a96b47d5fbDaniel Sandler } 3808d05e3d1d6ade6924266296033981a96b47d5fbDaniel Sandler 3908d05e3d1d6ade6924266296033981a96b47d5fbDaniel Sandler @Override 403618d30f8ab6018025b11869676b309c3b4961cfDoris Liu boolean animateBasedOnTime(long currentTime) { 41051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase if (mListener != null) { 42051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase long totalTime = currentTime - mStartTime; 43051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase long deltaTime = (mPreviousTime < 0) ? 0 : (currentTime - mPreviousTime); 44051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase mPreviousTime = currentTime; 45051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase mListener.onTimeUpdate(this, totalTime, deltaTime); 46051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase } 47051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase return false; 48051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase } 49051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase 500d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase @Override 510d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase public void setCurrentPlayTime(long playTime) { 520d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase long currentTime = AnimationUtils.currentAnimationTimeMillis(); 530d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase mStartTime = Math.max(mStartTime, currentTime - playTime); 54c42b28dda45347b05826dc3e04f5605a60867a63Jeff Brown mStartTimeCommitted = true; // do not allow start time to be compensated for jank 553618d30f8ab6018025b11869676b309c3b4961cfDoris Liu animateBasedOnTime(currentTime); 560d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase } 570d1c27a713cb49de8f6f4fd0a129baa883153921Chet Haase 58051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase /** 59051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase * Sets a listener that is sent update events throughout the life of 60051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase * an animation. 61051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase * 62051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase * @param listener the listener to be set. 63051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase */ 64051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase public void setTimeListener(TimeListener listener) { 65051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase mListener = listener; 66051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase } 67051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase 68051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase @Override 69051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase void animateValue(float fraction) { 70051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase // Noop 71051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase } 72051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase 73051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase @Override 74051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase void initAnimation() { 75051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase // noop 76051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase } 77051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase 78051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase /** 79051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase * Implementors of this interface can set themselves as update listeners 80051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase * to a <code>TimeAnimator</code> instance to receive callbacks on every animation 81051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase * frame to receive the total time since the animator started and the delta time 82a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * since the last frame. The first time the listener is called, 83a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * deltaTime will be zero. The same is true for totalTime, unless the animator was 84a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * set to a specific {@link ValueAnimator#setCurrentPlayTime(long) currentPlayTime} 85a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * prior to starting. 86051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase */ 87051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase public static interface TimeListener { 88051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase /** 89051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase * <p>Notifies listeners of the occurrence of another frame of the animation, 90051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase * along with information about the elapsed time.</p> 91051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase * 92051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase * @param animation The animator sending out the notification. 93a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * @param totalTime The total time elapsed since the animator started, in milliseconds. 94a33de55404eb2133d1bae2add3f6e8708459f56dChet Haase * @param deltaTime The time elapsed since the previous frame, in milliseconds. 95051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase */ 96051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase void onTimeUpdate(TimeAnimator animation, long totalTime, long deltaTime); 97051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase 98051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase } 99051d35e41f7b21cd8a1608bdce10cf70952c6be4Chet Haase} 100