1 /* 2 * Copyright (C) 2012 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 17 package com.android.launcher3; 18 19 import android.animation.Animator; 20 import android.animation.AnimatorSet; 21 import android.animation.ObjectAnimator; 22 import android.animation.PropertyValuesHolder; 23 import android.animation.ValueAnimator; 24 import android.util.Property; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.view.ViewTreeObserver; 28 import android.widget.ViewAnimator; 29 30 import java.util.HashSet; 31 import java.util.WeakHashMap; 32 33 public class LauncherAnimUtils { 34 static WeakHashMap<Animator, Object> sAnimators = new WeakHashMap<Animator, Object>(); 35 static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() { 36 public void onAnimationStart(Animator animation) { 37 sAnimators.put(animation, null); 38 } 39 40 public void onAnimationRepeat(Animator animation) { 41 } 42 43 public void onAnimationEnd(Animator animation) { 44 sAnimators.remove(animation); 45 } 46 47 public void onAnimationCancel(Animator animation) { 48 sAnimators.remove(animation); 49 } 50 }; 51 cancelOnDestroyActivity(Animator a)52 public static void cancelOnDestroyActivity(Animator a) { 53 a.addListener(sEndAnimListener); 54 } 55 56 // Helper method. Assumes a draw is pending, and that if the animation's duration is 0 57 // it should be cancelled startAnimationAfterNextDraw(final Animator animator, final View view)58 public static void startAnimationAfterNextDraw(final Animator animator, final View view) { 59 view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() { 60 private boolean mStarted = false; 61 62 public void onDraw() { 63 if (mStarted) return; 64 mStarted = true; 65 // Use this as a signal that the animation was cancelled 66 if (animator.getDuration() == 0) { 67 return; 68 } 69 animator.start(); 70 71 final ViewTreeObserver.OnDrawListener listener = this; 72 view.post(new Runnable() { 73 public void run() { 74 view.getViewTreeObserver().removeOnDrawListener(listener); 75 } 76 }); 77 } 78 }); 79 } 80 onDestroyActivity()81 public static void onDestroyActivity() { 82 HashSet<Animator> animators = new HashSet<Animator>(sAnimators.keySet()); 83 for (Animator a : animators) { 84 if (a.isRunning()) { 85 a.cancel(); 86 } 87 sAnimators.remove(a); 88 } 89 } 90 createAnimatorSet()91 public static AnimatorSet createAnimatorSet() { 92 AnimatorSet anim = new AnimatorSet(); 93 cancelOnDestroyActivity(anim); 94 return anim; 95 } 96 ofFloat(float... values)97 public static ValueAnimator ofFloat(float... values) { 98 ValueAnimator anim = new ValueAnimator(); 99 anim.setFloatValues(values); 100 cancelOnDestroyActivity(anim); 101 return anim; 102 } 103 ofFloat(View target, Property<View, Float> property, float... values)104 public static ObjectAnimator ofFloat(View target, Property<View, Float> property, 105 float... values) { 106 ObjectAnimator anim = ObjectAnimator.ofFloat(target, property, values); 107 cancelOnDestroyActivity(anim); 108 new FirstFrameAnimatorHelper(anim, target); 109 return anim; 110 } 111 ofViewAlphaAndScale(View target, float alpha, float scaleX, float scaleY)112 public static ObjectAnimator ofViewAlphaAndScale(View target, 113 float alpha, float scaleX, float scaleY) { 114 return ofPropertyValuesHolder(target, 115 PropertyValuesHolder.ofFloat(View.ALPHA, alpha), 116 PropertyValuesHolder.ofFloat(View.SCALE_X, scaleX), 117 PropertyValuesHolder.ofFloat(View.SCALE_Y, scaleY)); 118 } 119 ofPropertyValuesHolder(View target, PropertyValuesHolder... values)120 public static ObjectAnimator ofPropertyValuesHolder(View target, 121 PropertyValuesHolder... values) { 122 return ofPropertyValuesHolder(target, target, values); 123 } 124 ofPropertyValuesHolder(Object target, View view, PropertyValuesHolder... values)125 public static ObjectAnimator ofPropertyValuesHolder(Object target, 126 View view, PropertyValuesHolder... values) { 127 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(target, values); 128 cancelOnDestroyActivity(anim); 129 new FirstFrameAnimatorHelper(anim, view); 130 return anim; 131 } 132 133 } 134