ValueAnimatorCompat.java revision 631f64ec9c6255f38a7f746d7949b6a537c1180f
1/* 2 * Copyright (C) 2015 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 17package android.support.design.widget; 18 19import android.view.animation.Interpolator; 20 21/** 22 * This class offers a very small subset of {@code ValueAnimator}'s API, but works pre-v11 too. 23 * <p> 24 * You shouldn't not instantiate this directly. Instead use {@code ViewUtils.createAnimator()}. 25 */ 26class ValueAnimatorCompat { 27 28 interface AnimatorUpdateListener { 29 /** 30 * <p>Notifies the occurrence of another frame of the animation.</p> 31 * 32 * @param animator The animation which was repeated. 33 */ 34 void onAnimationUpdate(ValueAnimatorCompat animator); 35 } 36 37 /** 38 * An animation listener receives notifications from an animation. 39 * Notifications indicate animation related events, such as the end or the 40 * repetition of the animation. 41 */ 42 interface AnimatorListener { 43 /** 44 * <p>Notifies the start of the animation.</p> 45 * 46 * @param animator The started animation. 47 */ 48 void onAnimationStart(ValueAnimatorCompat animator); 49 /** 50 * <p>Notifies the end of the animation. This callback is not invoked 51 * for animations with repeat count set to INFINITE.</p> 52 * 53 * @param animator The animation which reached its end. 54 */ 55 void onAnimationEnd(ValueAnimatorCompat animator); 56 /** 57 * <p>Notifies the cancellation of the animation. This callback is not invoked 58 * for animations with repeat count set to INFINITE.</p> 59 * 60 * @param animator The animation which was canceled. 61 */ 62 void onAnimationCancel(ValueAnimatorCompat animator); 63 } 64 65 static class AnimatorListenerAdapter implements AnimatorListener { 66 @Override 67 public void onAnimationStart(ValueAnimatorCompat animator) { 68 } 69 70 @Override 71 public void onAnimationEnd(ValueAnimatorCompat animator) { 72 } 73 74 @Override 75 public void onAnimationCancel(ValueAnimatorCompat animator) { 76 } 77 } 78 79 interface Creator { 80 ValueAnimatorCompat createAnimator(); 81 } 82 83 static abstract class Impl { 84 interface AnimatorUpdateListenerProxy { 85 void onAnimationUpdate(); 86 } 87 88 interface AnimatorListenerProxy { 89 void onAnimationStart(); 90 void onAnimationEnd(); 91 void onAnimationCancel(); 92 } 93 94 abstract void start(); 95 abstract boolean isRunning(); 96 abstract void setInterpolator(Interpolator interpolator); 97 abstract void setListener(AnimatorListenerProxy listener); 98 abstract void setUpdateListener(AnimatorUpdateListenerProxy updateListener); 99 abstract void setIntValues(int from, int to); 100 abstract int getAnimatedIntValue(); 101 abstract void setFloatValues(float from, float to); 102 abstract float getAnimatedFloatValue(); 103 abstract void setDuration(int duration); 104 abstract void cancel(); 105 abstract float getAnimatedFraction(); 106 abstract void end(); 107 } 108 109 private final Impl mImpl; 110 111 ValueAnimatorCompat(Impl impl) { 112 mImpl = impl; 113 } 114 115 public void start() { 116 mImpl.start(); 117 } 118 119 public boolean isRunning() { 120 return mImpl.isRunning(); 121 } 122 123 public void setInterpolator(Interpolator interpolator) { 124 mImpl.setInterpolator(interpolator); 125 } 126 127 public void setUpdateListener(final AnimatorUpdateListener updateListener) { 128 if (updateListener != null) { 129 mImpl.setUpdateListener(new Impl.AnimatorUpdateListenerProxy() { 130 @Override 131 public void onAnimationUpdate() { 132 updateListener.onAnimationUpdate(ValueAnimatorCompat.this); 133 } 134 }); 135 } else { 136 mImpl.setUpdateListener(null); 137 } 138 } 139 140 public void setListener(final AnimatorListener listener) { 141 if (listener != null) { 142 mImpl.setListener(new Impl.AnimatorListenerProxy() { 143 @Override 144 public void onAnimationStart() { 145 listener.onAnimationStart(ValueAnimatorCompat.this); 146 } 147 148 @Override 149 public void onAnimationEnd() { 150 listener.onAnimationEnd(ValueAnimatorCompat.this); 151 } 152 153 @Override 154 public void onAnimationCancel() { 155 listener.onAnimationCancel(ValueAnimatorCompat.this); 156 } 157 }); 158 } else { 159 mImpl.setListener(null); 160 } 161 } 162 163 public void setIntValues(int from, int to) { 164 mImpl.setIntValues(from, to); 165 } 166 167 public int getAnimatedIntValue() { 168 return mImpl.getAnimatedIntValue(); 169 } 170 171 public void setFloatValues(float from, float to) { 172 mImpl.setFloatValues(from, to); 173 } 174 175 public float getAnimatedFloatValue() { 176 return mImpl.getAnimatedFloatValue(); 177 } 178 179 public void setDuration(int duration) { 180 mImpl.setDuration(duration); 181 } 182 183 public void cancel() { 184 mImpl.cancel(); 185 } 186 187 public float getAnimatedFraction() { 188 return mImpl.getAnimatedFraction(); 189 } 190 191 public void end() { 192 mImpl.end(); 193 } 194} 195