2012-06-18 12:52:28 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
2012-06-18 12:52:28 -07:00
|
|
|
|
|
|
|
|
import android.animation.Animator;
|
|
|
|
|
import android.animation.AnimatorSet;
|
|
|
|
|
import android.animation.ObjectAnimator;
|
|
|
|
|
import android.animation.PropertyValuesHolder;
|
|
|
|
|
import android.animation.ValueAnimator;
|
2013-03-13 12:55:46 +01:00
|
|
|
import android.view.View;
|
2014-08-12 09:23:13 -07:00
|
|
|
import android.view.ViewAnimationUtils;
|
2013-03-13 12:55:46 +01:00
|
|
|
import android.view.ViewTreeObserver;
|
2012-06-18 12:52:28 -07:00
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
2013-10-23 15:21:32 +02:00
|
|
|
import java.util.WeakHashMap;
|
2012-06-18 12:52:28 -07:00
|
|
|
|
|
|
|
|
public class LauncherAnimUtils {
|
2013-10-23 15:21:32 +02:00
|
|
|
static WeakHashMap<Animator, Object> sAnimators = new WeakHashMap<Animator, Object>();
|
2012-06-29 15:38:55 -07:00
|
|
|
static Animator.AnimatorListener sEndAnimListener = new Animator.AnimatorListener() {
|
2012-06-18 12:52:28 -07:00
|
|
|
public void onAnimationStart(Animator animation) {
|
2013-10-23 15:21:32 +02:00
|
|
|
sAnimators.put(animation, null);
|
2012-06-18 12:52:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onAnimationRepeat(Animator animation) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
|
sAnimators.remove(animation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onAnimationCancel(Animator animation) {
|
|
|
|
|
sAnimators.remove(animation);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static void cancelOnDestroyActivity(Animator a) {
|
2012-06-29 15:38:55 -07:00
|
|
|
a.addListener(sEndAnimListener);
|
2012-06-18 12:52:28 -07:00
|
|
|
}
|
|
|
|
|
|
2013-03-13 12:55:46 +01:00
|
|
|
// Helper method. Assumes a draw is pending, and that if the animation's duration is 0
|
|
|
|
|
// it should be cancelled
|
|
|
|
|
public static void startAnimationAfterNextDraw(final Animator animator, final View view) {
|
2013-04-03 16:25:02 -07:00
|
|
|
view.getViewTreeObserver().addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
|
|
|
|
|
private boolean mStarted = false;
|
2013-03-13 12:55:46 +01:00
|
|
|
public void onDraw() {
|
2013-04-03 16:25:02 -07:00
|
|
|
if (mStarted) return;
|
|
|
|
|
mStarted = true;
|
2013-03-13 12:55:46 +01:00
|
|
|
// Use this as a signal that the animation was cancelled
|
|
|
|
|
if (animator.getDuration() == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
animator.start();
|
2013-04-03 16:25:02 -07:00
|
|
|
|
|
|
|
|
final ViewTreeObserver.OnDrawListener listener = this;
|
|
|
|
|
view.post(new Runnable() {
|
|
|
|
|
public void run() {
|
|
|
|
|
view.getViewTreeObserver().removeOnDrawListener(listener);
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-03-13 12:55:46 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-18 12:52:28 -07:00
|
|
|
public static void onDestroyActivity() {
|
2013-10-23 15:21:32 +02:00
|
|
|
HashSet<Animator> animators = new HashSet<Animator>(sAnimators.keySet());
|
2012-07-12 11:21:32 -07:00
|
|
|
for (Animator a : animators) {
|
2012-06-18 12:52:28 -07:00
|
|
|
if (a.isRunning()) {
|
|
|
|
|
a.cancel();
|
|
|
|
|
}
|
2013-10-23 15:21:32 +02:00
|
|
|
sAnimators.remove(a);
|
2012-06-18 12:52:28 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static AnimatorSet createAnimatorSet() {
|
|
|
|
|
AnimatorSet anim = new AnimatorSet();
|
|
|
|
|
cancelOnDestroyActivity(anim);
|
|
|
|
|
return anim;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-13 12:55:46 +01:00
|
|
|
public static ValueAnimator ofFloat(View target, float... values) {
|
2012-06-18 12:52:28 -07:00
|
|
|
ValueAnimator anim = new ValueAnimator();
|
|
|
|
|
anim.setFloatValues(values);
|
|
|
|
|
cancelOnDestroyActivity(anim);
|
|
|
|
|
return anim;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-13 12:55:46 +01:00
|
|
|
public static ObjectAnimator ofFloat(View target, String propertyName, float... values) {
|
2012-06-18 12:52:28 -07:00
|
|
|
ObjectAnimator anim = new ObjectAnimator();
|
|
|
|
|
anim.setTarget(target);
|
|
|
|
|
anim.setPropertyName(propertyName);
|
|
|
|
|
anim.setFloatValues(values);
|
|
|
|
|
cancelOnDestroyActivity(anim);
|
2013-03-13 12:55:46 +01:00
|
|
|
new FirstFrameAnimatorHelper(anim, target);
|
2012-06-18 12:52:28 -07:00
|
|
|
return anim;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-13 12:55:46 +01:00
|
|
|
public static ObjectAnimator ofPropertyValuesHolder(View target,
|
2012-06-18 12:52:28 -07:00
|
|
|
PropertyValuesHolder... values) {
|
|
|
|
|
ObjectAnimator anim = new ObjectAnimator();
|
|
|
|
|
anim.setTarget(target);
|
|
|
|
|
anim.setValues(values);
|
|
|
|
|
cancelOnDestroyActivity(anim);
|
2013-03-13 12:55:46 +01:00
|
|
|
new FirstFrameAnimatorHelper(anim, target);
|
|
|
|
|
return anim;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ObjectAnimator ofPropertyValuesHolder(Object target,
|
|
|
|
|
View view, PropertyValuesHolder... values) {
|
|
|
|
|
ObjectAnimator anim = new ObjectAnimator();
|
|
|
|
|
anim.setTarget(target);
|
|
|
|
|
anim.setValues(values);
|
|
|
|
|
cancelOnDestroyActivity(anim);
|
|
|
|
|
new FirstFrameAnimatorHelper(anim, view);
|
2012-06-18 12:52:28 -07:00
|
|
|
return anim;
|
|
|
|
|
}
|
2014-08-12 09:23:13 -07:00
|
|
|
|
|
|
|
|
public static Animator createCircularReveal(View view, int centerX,
|
|
|
|
|
int centerY, float startRadius, float endRadius) {
|
|
|
|
|
Animator anim = ViewAnimationUtils.createCircularReveal(view, centerX,
|
|
|
|
|
centerY, startRadius, endRadius);
|
|
|
|
|
if (anim instanceof ValueAnimator) {
|
|
|
|
|
new FirstFrameAnimatorHelper((ValueAnimator) anim, view);
|
|
|
|
|
}
|
|
|
|
|
return anim;
|
|
|
|
|
}
|
2012-06-18 12:52:28 -07:00
|
|
|
}
|