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