1/* 2 * Copyright (C) 2011 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 com.android.internal.widget.multiwaveview; 18 19import java.util.ArrayList; 20import java.util.HashMap; 21import java.util.Iterator; 22import java.util.Map.Entry; 23 24import android.animation.Animator.AnimatorListener; 25import android.animation.Animator; 26import android.animation.AnimatorListenerAdapter; 27import android.animation.ObjectAnimator; 28import android.animation.PropertyValuesHolder; 29import android.animation.TimeInterpolator; 30import android.animation.ValueAnimator.AnimatorUpdateListener; 31import android.util.Log; 32 33class Tweener { 34 private static final String TAG = "Tweener"; 35 private static final boolean DEBUG = false; 36 37 ObjectAnimator animator; 38 private static HashMap<Object, Tweener> sTweens = new HashMap<Object, Tweener>(); 39 40 public Tweener(ObjectAnimator anim) { 41 animator = anim; 42 } 43 44 private static void remove(Animator animator) { 45 Iterator<Entry<Object, Tweener>> iter = sTweens.entrySet().iterator(); 46 while (iter.hasNext()) { 47 Entry<Object, Tweener> entry = iter.next(); 48 if (entry.getValue().animator == animator) { 49 if (DEBUG) Log.v(TAG, "Removing tweener " + sTweens.get(entry.getKey()) 50 + " sTweens.size() = " + sTweens.size()); 51 iter.remove(); 52 break; // an animator can only be attached to one object 53 } 54 } 55 } 56 57 public static Tweener to(Object object, long duration, Object... vars) { 58 long delay = 0; 59 AnimatorUpdateListener updateListener = null; 60 AnimatorListener listener = null; 61 TimeInterpolator interpolator = null; 62 63 // Iterate through arguments and discover properties to animate 64 ArrayList<PropertyValuesHolder> props = new ArrayList<PropertyValuesHolder>(vars.length/2); 65 for (int i = 0; i < vars.length; i+=2) { 66 if (!(vars[i] instanceof String)) { 67 throw new IllegalArgumentException("Key must be a string: " + vars[i]); 68 } 69 String key = (String) vars[i]; 70 Object value = vars[i+1]; 71 if ("simultaneousTween".equals(key)) { 72 // TODO 73 } else if ("ease".equals(key)) { 74 interpolator = (TimeInterpolator) value; // TODO: multiple interpolators? 75 } else if ("onUpdate".equals(key) || "onUpdateListener".equals(key)) { 76 updateListener = (AnimatorUpdateListener) value; 77 } else if ("onComplete".equals(key) || "onCompleteListener".equals(key)) { 78 listener = (AnimatorListener) value; 79 } else if ("delay".equals(key)) { 80 delay = ((Number) value).longValue(); 81 } else if ("syncWith".equals(key)) { 82 // TODO 83 } else if (value instanceof float[]) { 84 props.add(PropertyValuesHolder.ofFloat(key, 85 ((float[])value)[0], ((float[])value)[1])); 86 } else if (value instanceof Number) { 87 float floatValue = ((Number)value).floatValue(); 88 props.add(PropertyValuesHolder.ofFloat(key, floatValue)); 89 } else { 90 throw new IllegalArgumentException( 91 "Bad argument for key \"" + key + "\" with value " + value.getClass()); 92 } 93 } 94 95 // Re-use existing tween, if present 96 Tweener tween = sTweens.get(object); 97 ObjectAnimator anim = null; 98 if (tween == null) { 99 anim = ObjectAnimator.ofPropertyValuesHolder(object, 100 props.toArray(new PropertyValuesHolder[props.size()])); 101 tween = new Tweener(anim); 102 sTweens.put(object, tween); 103 if (DEBUG) Log.v(TAG, "Added new Tweener " + tween); 104 } else { 105 anim = sTweens.get(object).animator; 106 replace(props, object); // Cancel all animators for given object 107 } 108 109 if (interpolator != null) { 110 anim.setInterpolator(interpolator); 111 } 112 113 // Update animation with properties discovered in loop above 114 anim.setStartDelay(delay); 115 anim.setDuration(duration); 116 if (updateListener != null) { 117 anim.removeAllUpdateListeners(); // There should be only one 118 anim.addUpdateListener(updateListener); 119 } 120 if (listener != null) { 121 anim.removeAllListeners(); // There should be only one. 122 anim.addListener(listener); 123 } 124 anim.addListener(mCleanupListener); 125 anim.start(); 126 127 return tween; 128 } 129 130 Tweener from(Object object, long duration, Object... vars) { 131 // TODO: for v of vars 132 // toVars[v] = object[v] 133 // object[v] = vars[v] 134 return Tweener.to(object, duration, vars); 135 } 136 137 // Listener to watch for completed animations and remove them. 138 private static AnimatorListener mCleanupListener = new AnimatorListenerAdapter() { 139 140 @Override 141 public void onAnimationEnd(Animator animation) { 142 remove(animation); 143 } 144 145 @Override 146 public void onAnimationCancel(Animator animation) { 147 remove(animation); 148 } 149 }; 150 151 public static void reset() { 152 if (DEBUG) { 153 Log.v(TAG, "Reset()"); 154 if (sTweens.size() > 0) { 155 Log.v(TAG, "Cleaning up " + sTweens.size() + " animations"); 156 } 157 } 158 sTweens.clear(); 159 } 160 161 private static void replace(ArrayList<PropertyValuesHolder> props, Object... args) { 162 for (final Object killobject : args) { 163 Tweener tween = sTweens.get(killobject); 164 if (tween != null) { 165 tween.animator.cancel(); 166 if (props != null) { 167 tween.animator.setValues( 168 props.toArray(new PropertyValuesHolder[props.size()])); 169 } else { 170 sTweens.remove(tween); 171 } 172 } 173 } 174 } 175} 176