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;
|
2017-05-04 11:47:53 -07:00
|
|
|
import android.graphics.drawable.Drawable;
|
2015-07-06 22:52:49 -07:00
|
|
|
import android.util.Property;
|
2013-03-13 12:55:46 +01:00
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewTreeObserver;
|
2015-05-26 23:03:31 -07:00
|
|
|
|
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 {
|
2017-10-16 11:46:41 -07:00
|
|
|
/**
|
|
|
|
|
* Durations for various state animations. These are not defined in resources to allow
|
|
|
|
|
* easier access from static classes and enums
|
|
|
|
|
*/
|
|
|
|
|
public static final int ALL_APPS_TRANSITION_MS = 320;
|
|
|
|
|
public static final int OVERVIEW_TRANSITION_MS = 250;
|
|
|
|
|
public static final int SPRING_LOADED_TRANSITION_MS = 150;
|
|
|
|
|
public static final int SPRING_LOADED_EXIT_SHORT_TIMEOUT = 500;
|
|
|
|
|
public static final int SPRING_LOADED_EXIT_NEXT_FRAME = 0;
|
|
|
|
|
|
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() {
|
2015-10-02 16:22:08 -07:00
|
|
|
private boolean mStarted = false;
|
2013-04-03 16:25:02 -07:00
|
|
|
|
2015-10-02 16:22:08 -07:00
|
|
|
public void onDraw() {
|
|
|
|
|
if (mStarted) return;
|
|
|
|
|
mStarted = true;
|
|
|
|
|
// Use this as a signal that the animation was cancelled
|
|
|
|
|
if (animator.getDuration() == 0) {
|
|
|
|
|
return;
|
2013-03-13 12:55:46 +01:00
|
|
|
}
|
2015-10-02 16:22:08 -07:00
|
|
|
animator.start();
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-06 15:54:41 -08:00
|
|
|
public static ValueAnimator ofFloat(float... values) {
|
2012-06-18 12:52:28 -07:00
|
|
|
ValueAnimator anim = new ValueAnimator();
|
|
|
|
|
anim.setFloatValues(values);
|
|
|
|
|
cancelOnDestroyActivity(anim);
|
|
|
|
|
return anim;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-06 22:52:49 -07:00
|
|
|
public static ObjectAnimator ofFloat(View target, Property<View, Float> property,
|
|
|
|
|
float... values) {
|
|
|
|
|
ObjectAnimator anim = ObjectAnimator.ofFloat(target, property, values);
|
2012-06-18 12:52:28 -07:00
|
|
|
cancelOnDestroyActivity(anim);
|
2013-03-13 12:55:46 +01:00
|
|
|
new FirstFrameAnimatorHelper(anim, target);
|
2012-06-18 12:52:28 -07:00
|
|
|
return anim;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-06 22:52:49 -07:00
|
|
|
public static ObjectAnimator ofViewAlphaAndScale(View target,
|
|
|
|
|
float alpha, float scaleX, float scaleY) {
|
|
|
|
|
return ofPropertyValuesHolder(target,
|
|
|
|
|
PropertyValuesHolder.ofFloat(View.ALPHA, alpha),
|
|
|
|
|
PropertyValuesHolder.ofFloat(View.SCALE_X, scaleX),
|
|
|
|
|
PropertyValuesHolder.ofFloat(View.SCALE_Y, scaleY));
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-13 12:55:46 +01:00
|
|
|
public static ObjectAnimator ofPropertyValuesHolder(View target,
|
2012-06-18 12:52:28 -07:00
|
|
|
PropertyValuesHolder... values) {
|
2015-07-06 22:52:49 -07:00
|
|
|
return ofPropertyValuesHolder(target, target, values);
|
2013-03-13 12:55:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ObjectAnimator ofPropertyValuesHolder(Object target,
|
|
|
|
|
View view, PropertyValuesHolder... values) {
|
2015-07-06 22:52:49 -07:00
|
|
|
ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(target, values);
|
2013-03-13 12:55:46 +01:00
|
|
|
cancelOnDestroyActivity(anim);
|
|
|
|
|
new FirstFrameAnimatorHelper(anim, view);
|
2012-06-18 12:52:28 -07:00
|
|
|
return anim;
|
|
|
|
|
}
|
2017-01-20 09:38:25 -08:00
|
|
|
|
2017-05-04 11:47:53 -07:00
|
|
|
public static final Property<Drawable, Integer> DRAWABLE_ALPHA =
|
|
|
|
|
new Property<Drawable, Integer>(Integer.TYPE, "drawableAlpha") {
|
|
|
|
|
@Override
|
|
|
|
|
public Integer get(Drawable drawable) {
|
|
|
|
|
return drawable.getAlpha();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void set(Drawable drawable, Integer alpha) {
|
|
|
|
|
drawable.setAlpha(alpha);
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-10-16 11:46:41 -07:00
|
|
|
|
|
|
|
|
public static final Property<View, Float> SCALE_PROPERTY =
|
|
|
|
|
new Property<View, Float>(Float.class, "scale") {
|
|
|
|
|
@Override
|
|
|
|
|
public Float get(View view) {
|
|
|
|
|
return view.getScaleX();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void set(View view, Float scale) {
|
|
|
|
|
view.setScaleX(scale);
|
|
|
|
|
view.setScaleY(scale);
|
|
|
|
|
}
|
|
|
|
|
};
|
2012-06-18 12:52:28 -07:00
|
|
|
}
|