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(long duration); 104 abstract void cancel(); 105 abstract float getAnimatedFraction(); 106 abstract void end(); 107 abstract long getDuration(); 108 } 109 110 private final Impl mImpl; 111 112 ValueAnimatorCompat(Impl impl) { 113 mImpl = impl; 114 } 115 116 public void start() { 117 mImpl.start(); 118 } 119 120 public boolean isRunning() { 121 return mImpl.isRunning(); 122 } 123 124 public void setInterpolator(Interpolator interpolator) { 125 mImpl.setInterpolator(interpolator); 126 } 127 128 public void setUpdateListener(final AnimatorUpdateListener updateListener) { 129 if (updateListener != null) { 130 mImpl.setUpdateListener(new Impl.AnimatorUpdateListenerProxy() { 131 @Override 132 public void onAnimationUpdate() { 133 updateListener.onAnimationUpdate(ValueAnimatorCompat.this); 134 } 135 }); 136 } else { 137 mImpl.setUpdateListener(null); 138 } 139 } 140 141 public void setListener(final AnimatorListener listener) { 142 if (listener != null) { 143 mImpl.setListener(new Impl.AnimatorListenerProxy() { 144 @Override 145 public void onAnimationStart() { 146 listener.onAnimationStart(ValueAnimatorCompat.this); 147 } 148 149 @Override 150 public void onAnimationEnd() { 151 listener.onAnimationEnd(ValueAnimatorCompat.this); 152 } 153 154 @Override 155 public void onAnimationCancel() { 156 listener.onAnimationCancel(ValueAnimatorCompat.this); 157 } 158 }); 159 } else { 160 mImpl.setListener(null); 161 } 162 } 163 164 public void setIntValues(int from, int to) { 165 mImpl.setIntValues(from, to); 166 } 167 168 public int getAnimatedIntValue() { 169 return mImpl.getAnimatedIntValue(); 170 } 171 172 public void setFloatValues(float from, float to) { 173 mImpl.setFloatValues(from, to); 174 } 175 176 public float getAnimatedFloatValue() { 177 return mImpl.getAnimatedFloatValue(); 178 } 179 180 public void setDuration(long duration) { 181 mImpl.setDuration(duration); 182 } 183 184 public void cancel() { 185 mImpl.cancel(); 186 } 187 188 public float getAnimatedFraction() { 189 return mImpl.getAnimatedFraction(); 190 } 191 192 public void end() { 193 mImpl.end(); 194 } 195 196 public long getDuration() { 197 return mImpl.getDuration(); 198 } 199} 200