Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 18 | |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 21 | import android.animation.ValueAnimator; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 22 | import android.util.Log; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 23 | import android.view.View; |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 24 | import android.view.ViewPropertyAnimator; |
Michael Jurka | 032e6ba | 2013-04-22 15:08:42 +0200 | [diff] [blame] | 25 | import android.view.ViewTreeObserver; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 26 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 27 | import com.android.launcher3.util.Thunk; |
| 28 | |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 29 | /* |
| 30 | * This is a helper class that listens to updates from the corresponding animation. |
| 31 | * For the first two frames, it adjusts the current play time of the animation to |
| 32 | * prevent jank at the beginning of the animation |
| 33 | */ |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 34 | public class FirstFrameAnimatorHelper extends AnimatorListenerAdapter |
| 35 | implements ValueAnimator.AnimatorUpdateListener { |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 36 | private static final boolean DEBUG = false; |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 37 | private static final int MAX_DELAY = 1000; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 38 | private static final int IDEAL_FRAME_DURATION = 16; |
| 39 | private View mTarget; |
| 40 | private long mStartFrame; |
| 41 | private long mStartTime = -1; |
| 42 | private boolean mHandlingOnAnimationUpdate; |
| 43 | private boolean mAdjustedSecondFrameTime; |
| 44 | |
| 45 | private static ViewTreeObserver.OnDrawListener sGlobalDrawListener; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 46 | @Thunk static long sGlobalFrameCounter; |
Michael Jurka | cd496d7 | 2013-04-11 11:32:45 -0700 | [diff] [blame] | 47 | private static boolean sVisible; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 48 | |
| 49 | public FirstFrameAnimatorHelper(ValueAnimator animator, View target) { |
| 50 | mTarget = target; |
| 51 | animator.addUpdateListener(this); |
| 52 | } |
| 53 | |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 54 | public FirstFrameAnimatorHelper(ViewPropertyAnimator vpa, View target) { |
| 55 | mTarget = target; |
| 56 | vpa.setListener(this); |
| 57 | } |
| 58 | |
| 59 | // only used for ViewPropertyAnimators |
| 60 | public void onAnimationStart(Animator animation) { |
| 61 | final ValueAnimator va = (ValueAnimator) animation; |
| 62 | va.addUpdateListener(FirstFrameAnimatorHelper.this); |
| 63 | onAnimationUpdate(va); |
| 64 | } |
| 65 | |
Michael Jurka | cd496d7 | 2013-04-11 11:32:45 -0700 | [diff] [blame] | 66 | public static void setIsVisible(boolean visible) { |
| 67 | sVisible = visible; |
| 68 | } |
| 69 | |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 70 | public static void initializeDrawListener(View view) { |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 71 | if (sGlobalDrawListener != null) { |
| 72 | view.getViewTreeObserver().removeOnDrawListener(sGlobalDrawListener); |
| 73 | } |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 74 | sGlobalDrawListener = new ViewTreeObserver.OnDrawListener() { |
| 75 | private long mTime = System.currentTimeMillis(); |
| 76 | public void onDraw() { |
| 77 | sGlobalFrameCounter++; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 78 | if (DEBUG) { |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 79 | long newTime = System.currentTimeMillis(); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 80 | Log.d("FirstFrameAnimatorHelper", "TICK " + (newTime - mTime)); |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 81 | mTime = newTime; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 82 | } |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 83 | } |
| 84 | }; |
| 85 | view.getViewTreeObserver().addOnDrawListener(sGlobalDrawListener); |
Michael Jurka | cd496d7 | 2013-04-11 11:32:45 -0700 | [diff] [blame] | 86 | sVisible = true; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Michael Jurka | 98720c9 | 2013-03-28 13:39:35 -0700 | [diff] [blame] | 89 | public void onAnimationUpdate(final ValueAnimator animation) { |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 90 | final long currentTime = System.currentTimeMillis(); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 91 | if (mStartTime == -1) { |
| 92 | mStartFrame = sGlobalFrameCounter; |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 93 | mStartTime = currentTime; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Michael Jurka | 19d10a5 | 2013-11-06 17:06:57 +0100 | [diff] [blame] | 96 | final long currentPlayTime = animation.getCurrentPlayTime(); |
Adam Cohen | 5c18b80 | 2014-12-02 15:24:52 -0800 | [diff] [blame] | 97 | boolean isFinalFrame = Float.compare(1f, animation.getAnimatedFraction()) == 0; |
| 98 | |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 99 | if (!mHandlingOnAnimationUpdate && |
Michael Jurka | cd496d7 | 2013-04-11 11:32:45 -0700 | [diff] [blame] | 100 | sVisible && |
Adam Cohen | 5c18b80 | 2014-12-02 15:24:52 -0800 | [diff] [blame] | 101 | // If the current play time exceeds the duration, or the animated fraction is 1, |
| 102 | // the animation will get finished, even if we call setCurrentPlayTime -- therefore |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 103 | // don't adjust the animation in that case |
Adam Cohen | 5c18b80 | 2014-12-02 15:24:52 -0800 | [diff] [blame] | 104 | currentPlayTime < animation.getDuration() && !isFinalFrame) { |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 105 | mHandlingOnAnimationUpdate = true; |
| 106 | long frameNum = sGlobalFrameCounter - mStartFrame; |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 107 | // If we haven't drawn our first frame, reset the time to t = 0 |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 108 | // (give up after MAX_DELAY ms of waiting though - might happen, for example, if we |
| 109 | // are no longer in the foreground and no frames are being rendered ever) |
Michael Jurka | 19d10a5 | 2013-11-06 17:06:57 +0100 | [diff] [blame] | 110 | if (frameNum == 0 && currentTime < mStartTime + MAX_DELAY && currentPlayTime > 0) { |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 111 | // The first frame on animations doesn't always trigger an invalidate... |
| 112 | // force an invalidate here to make sure the animation continues to advance |
| 113 | mTarget.getRootView().invalidate(); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 114 | animation.setCurrentPlayTime(0); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 115 | // For the second frame, if the first frame took more than 16ms, |
| 116 | // adjust the start time and pretend it took only 16ms anyway. This |
| 117 | // prevents a large jump in the animation due to an expensive first frame |
Michael Jurka | 39b599e | 2013-04-08 18:28:15 -0700 | [diff] [blame] | 118 | } else if (frameNum == 1 && currentTime < mStartTime + MAX_DELAY && |
| 119 | !mAdjustedSecondFrameTime && |
Michael Jurka | 19d10a5 | 2013-11-06 17:06:57 +0100 | [diff] [blame] | 120 | currentTime > mStartTime + IDEAL_FRAME_DURATION && |
| 121 | currentPlayTime > IDEAL_FRAME_DURATION) { |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 122 | animation.setCurrentPlayTime(IDEAL_FRAME_DURATION); |
| 123 | mAdjustedSecondFrameTime = true; |
| 124 | } else { |
| 125 | if (frameNum > 1) { |
Michael Jurka | 98720c9 | 2013-03-28 13:39:35 -0700 | [diff] [blame] | 126 | mTarget.post(new Runnable() { |
| 127 | public void run() { |
| 128 | animation.removeUpdateListener(FirstFrameAnimatorHelper.this); |
| 129 | } |
| 130 | }); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 131 | } |
| 132 | if (DEBUG) print(animation); |
| 133 | } |
| 134 | mHandlingOnAnimationUpdate = false; |
| 135 | } else { |
| 136 | if (DEBUG) print(animation); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | public void print(ValueAnimator animation) { |
| 141 | float flatFraction = animation.getCurrentPlayTime() / (float) animation.getDuration(); |
| 142 | Log.d("FirstFrameAnimatorHelper", sGlobalFrameCounter + |
| 143 | "(" + (sGlobalFrameCounter - mStartFrame) + ") " + mTarget + " dirty? " + |
| 144 | mTarget.isDirty() + " " + flatFraction + " " + this + " " + animation); |
| 145 | } |
| 146 | } |