Using FloatProperty for spring animation, instead of a interface

to allow easier generalization of animation definitions

Change-Id: I37b1a604003ec007aa390eabdfe8c1ab733b7471
This commit is contained in:
Sunny Goyal
2019-06-19 21:30:40 -07:00
parent 676a795ebd
commit b80941bb24
7 changed files with 63 additions and 101 deletions

View File

@@ -21,6 +21,7 @@ import static com.android.launcher3.LauncherState.BACKGROUND_APP;
import static com.android.launcher3.LauncherState.NORMAL;
import static com.android.launcher3.LauncherState.OVERVIEW;
import static com.android.launcher3.LauncherStateManager.ANIM_ALL;
import static com.android.launcher3.allapps.AllAppsTransitionController.ALL_APPS_PROGRESS;
import static com.android.launcher3.allapps.AllAppsTransitionController.SPRING_DAMPING_RATIO;
import static com.android.launcher3.allapps.AllAppsTransitionController.SPRING_STIFFNESS;
import static com.android.launcher3.anim.Interpolators.ACCEL_2;
@@ -380,7 +381,7 @@ public final class LauncherActivityControllerHelper implements ActivityControlHe
private Animator createShelfAnim(Launcher activity, float ... progressValues) {
Animator shiftAnim = new SpringObjectAnimator<>(activity.getAllAppsController(),
"allAppsSpringFromACH", activity.getAllAppsController().getShiftRange(),
ALL_APPS_PROGRESS, activity.getAllAppsController().getShiftRange(),
SPRING_DAMPING_RATIO, SPRING_STIFFNESS, progressValues);
return shiftAnim;
}

View File

@@ -17,19 +17,15 @@ package com.android.quickstep.util;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.dynamicanimation.animation.SpringForce;
import com.android.launcher3.CellLayout;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.Launcher;
import com.android.launcher3.LauncherAnimUtils.ViewProgressProperty;
import com.android.launcher3.LauncherState;
import com.android.launcher3.LauncherStateManager;
import com.android.launcher3.LauncherStateManager.AnimationConfig;
import com.android.launcher3.R;
import com.android.launcher3.ShortcutAndWidgetContainer;
@@ -40,6 +36,7 @@ import com.android.launcher3.anim.SpringObjectAnimator;
import java.util.ArrayList;
import java.util.List;
import static com.android.launcher3.LauncherAnimUtils.VIEW_TRANSLATE_Y;
import static com.android.launcher3.LauncherState.BACKGROUND_APP;
import static com.android.launcher3.LauncherState.NORMAL;
import static com.android.launcher3.anim.Interpolators.LINEAR;
@@ -147,9 +144,8 @@ public class StaggeredWorkspaceAnim {
long startDelay = (long) ((invertedRow + 1) * APP_CLOSE_ROW_START_DELAY_MS);
v.setTranslationY(mSpringTransY);
SpringObjectAnimator springTransY = new SpringObjectAnimator<>(
new ViewProgressProperty(v, View.TRANSLATION_Y), "staggeredSpringTransY", 1f,
DAMPING_RATIO, STIFFNESS, mSpringTransY, 0);
SpringObjectAnimator springTransY = new SpringObjectAnimator<>(v, VIEW_TRANSLATE_Y,
1f, DAMPING_RATIO, STIFFNESS, mSpringTransY, 0);
springTransY.setStartDelay(startDelay);
mAnimators.add(springTransY);

View File

@@ -19,6 +19,8 @@ package com.android.quickstep.views;
import static com.android.launcher3.BaseActivity.STATE_HANDLER_INVISIBILITY_FLAGS;
import static com.android.launcher3.InvariantDeviceProfile.CHANGE_FLAG_ICON_PARAMS;
import static com.android.launcher3.LauncherAnimUtils.SCALE_PROPERTY;
import static com.android.launcher3.LauncherAnimUtils.VIEW_TRANSLATE_X;
import static com.android.launcher3.LauncherAnimUtils.VIEW_TRANSLATE_Y;
import static com.android.launcher3.Utilities.EDGE_NAV_BAR;
import static com.android.launcher3.Utilities.squaredHypot;
import static com.android.launcher3.Utilities.squaredTouchSlop;
@@ -79,7 +81,6 @@ import com.android.launcher3.BaseActivity;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.Insettable;
import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.LauncherAnimUtils.ViewProgressProperty;
import com.android.launcher3.LauncherState;
import com.android.launcher3.PagedView;
import com.android.launcher3.R;
@@ -1031,9 +1032,9 @@ public abstract class RecentsView<T extends BaseActivity> extends PagedView impl
private void addDismissedTaskAnimations(View taskView, AnimatorSet anim, long duration) {
addAnim(ObjectAnimator.ofFloat(taskView, ALPHA, 0), duration, ACCEL_2, anim);
if (QUICKSTEP_SPRINGS.get() && taskView instanceof TaskView)
addAnim(new SpringObjectAnimator<>(new ViewProgressProperty(taskView,
View.TRANSLATION_Y), "taskViewTransY", SPRING_MIN_VISIBLE_CHANGE,
SPRING_DAMPING_RATIO, SPRING_STIFFNESS, 0, -taskView.getHeight()),
addAnim(new SpringObjectAnimator<>(taskView, VIEW_TRANSLATE_Y,
SPRING_MIN_VISIBLE_CHANGE, SPRING_DAMPING_RATIO, SPRING_STIFFNESS,
0, -taskView.getHeight()),
duration, LINEAR, anim);
else {
addAnim(ObjectAnimator.ofFloat(taskView, TRANSLATION_Y, -taskView.getHeight()),
@@ -1109,10 +1110,9 @@ public abstract class RecentsView<T extends BaseActivity> extends PagedView impl
int scrollDiff = newScroll[i] - oldScroll[i] + offset;
if (scrollDiff != 0) {
if (QUICKSTEP_SPRINGS.get() && child instanceof TaskView) {
addAnim(new SpringObjectAnimator<>(
new ViewProgressProperty(child, View.TRANSLATION_X),
"taskViewTransX", SPRING_MIN_VISIBLE_CHANGE, SPRING_DAMPING_RATIO,
SPRING_STIFFNESS, 0, scrollDiff), duration, ACCEL, anim);
addAnim(new SpringObjectAnimator<>(child, VIEW_TRANSLATE_X,
SPRING_MIN_VISIBLE_CHANGE, SPRING_DAMPING_RATIO, SPRING_STIFFNESS,
0, scrollDiff), duration, ACCEL, anim);
} else {
addAnim(ObjectAnimator.ofFloat(child, TRANSLATION_X, scrollDiff), duration,
ACCEL, anim);

View File

@@ -17,6 +17,7 @@
package com.android.launcher3;
import android.graphics.drawable.Drawable;
import android.util.FloatProperty;
import android.util.Property;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
@@ -47,15 +48,15 @@ public class LauncherAnimUtils {
}
};
public static final Property<View, Float> SCALE_PROPERTY =
new Property<View, Float>(Float.class, "scale") {
public static final FloatProperty<View> SCALE_PROPERTY =
new FloatProperty<View>("scale") {
@Override
public Float get(View view) {
return view.getScaleX();
}
@Override
public void set(View view, Float scale) {
public void setValue(View view, float scale) {
view.setScaleX(scale);
view.setScaleY(scale);
}
@@ -92,23 +93,31 @@ public class LauncherAnimUtils {
}
};
public static class ViewProgressProperty implements ProgressInterface {
View mView;
Property<View, Float> mProperty;
public static final FloatProperty<View> VIEW_TRANSLATE_X =
View.TRANSLATION_X instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_X
: new FloatProperty<View>("translateX") {
@Override
public void setValue(View view, float v) {
view.setTranslationX(v);
}
public ViewProgressProperty(View view, Property<View, Float> property) {
mView = view;
mProperty = property;
}
@Override
public Float get(View view) {
return view.getTranslationX();
}
};
@Override
public void setProgress(float progress) {
mProperty.set(mView, progress);
}
public static final FloatProperty<View> VIEW_TRANSLATE_Y =
View.TRANSLATION_Y instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_Y
: new FloatProperty<View>("translateY") {
@Override
public void setValue(View view, float v) {
view.setTranslationY(v);
}
@Override
public float getProgress() {
return mProperty.get(mView);
}
}
@Override
public Float get(View view) {
return view.getTranslationY();
}
};
}

View File

@@ -1,26 +0,0 @@
/*
* Copyright (C) 2019 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.
*/
package com.android.launcher3;
/**
* Progress is defined as a value with range [0, 1], and is specific to each implementor.
* It is used when there is a transition from one state of the UI to another.
*/
public interface ProgressInterface {
void setProgress(float progress);
float getProgress();
}

View File

@@ -16,8 +16,8 @@ import static com.android.launcher3.util.SystemUiController.UI_STATE_ALL_APPS;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.util.FloatProperty;
import android.util.Log;
import android.util.Property;
import android.view.animation.Interpolator;
import com.android.launcher3.DeviceProfile;
@@ -26,7 +26,6 @@ import com.android.launcher3.Launcher;
import com.android.launcher3.LauncherState;
import com.android.launcher3.LauncherStateManager.AnimationConfig;
import com.android.launcher3.LauncherStateManager.StateHandler;
import com.android.launcher3.ProgressInterface;
import com.android.launcher3.R;
import com.android.launcher3.anim.AnimationSuccessListener;
import com.android.launcher3.anim.AnimatorSetBuilder;
@@ -46,14 +45,13 @@ import com.android.launcher3.views.ScrimView;
* If release velocity < THRES1, snap according to either top or bottom depending on whether it's
* closer to top or closer to the page indicator.
*/
public class AllAppsTransitionController implements StateHandler, OnDeviceProfileChangeListener,
ProgressInterface {
public class AllAppsTransitionController implements StateHandler, OnDeviceProfileChangeListener {
public static final float SPRING_DAMPING_RATIO = 0.9f;
public static final float SPRING_STIFFNESS = 600f;
public static final Property<AllAppsTransitionController, Float> ALL_APPS_PROGRESS =
new Property<AllAppsTransitionController, Float>(Float.class, "allAppsProgress") {
public static final FloatProperty<AllAppsTransitionController> ALL_APPS_PROGRESS =
new FloatProperty<AllAppsTransitionController>("allAppsProgress") {
@Override
public Float get(AllAppsTransitionController controller) {
@@ -61,7 +59,7 @@ public class AllAppsTransitionController implements StateHandler, OnDeviceProfil
}
@Override
public void set(AllAppsTransitionController controller, Float progress) {
public void setValue(AllAppsTransitionController controller, float progress) {
controller.setProgress(progress);
}
};
@@ -121,7 +119,6 @@ public class AllAppsTransitionController implements StateHandler, OnDeviceProfil
* @see #setState(LauncherState)
* @see #setStateWithAnimation(LauncherState, AnimatorSetBuilder, AnimationConfig)
*/
@Override
public void setProgress(float progress) {
mProgress = progress;
mScrimView.setProgress(progress);
@@ -149,7 +146,6 @@ public class AllAppsTransitionController implements StateHandler, OnDeviceProfil
}
}
@Override
public float getProgress() {
return mProgress;
}
@@ -192,7 +188,7 @@ public class AllAppsTransitionController implements StateHandler, OnDeviceProfil
Interpolator interpolator = config.userControlled ? LINEAR : toState == OVERVIEW
? builder.getInterpolator(ANIM_OVERVIEW_SCALE, FAST_OUT_SLOW_IN)
: FAST_OUT_SLOW_IN;
Animator anim = new SpringObjectAnimator<>(this, "allAppsSpringFromAATC", 1f / mShiftRange,
Animator anim = new SpringObjectAnimator<>(this, ALL_APPS_PROGRESS, 1f / mShiftRange,
SPRING_DAMPING_RATIO, SPRING_STIFFNESS, mProgress, targetProgress);
anim.setDuration(config.duration);
anim.setInterpolator(builder.getInterpolator(ANIM_VERTICAL_PROGRESS, interpolator));

View File

@@ -15,6 +15,8 @@
*/
package com.android.launcher3.anim;
import static androidx.dynamicanimation.animation.FloatPropertyCompat.createFloatPropertyCompat;
import static com.android.launcher3.config.FeatureFlags.QUICKSTEP_SPRINGS;
import android.animation.Animator;
@@ -24,15 +26,12 @@ import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.os.Handler;
import android.os.Looper;
import android.util.FloatProperty;
import android.util.Log;
import android.util.Property;
import com.android.launcher3.ProgressInterface;
import java.util.ArrayList;
import androidx.dynamicanimation.animation.DynamicAnimation.OnAnimationEndListener;
import androidx.dynamicanimation.animation.FloatPropertyCompat;
import androidx.dynamicanimation.animation.SpringAnimation;
import androidx.dynamicanimation.animation.SpringForce;
@@ -40,12 +39,11 @@ import androidx.dynamicanimation.animation.SpringForce;
* This animator allows for an object's property to be be controlled by an {@link ObjectAnimator} or
* a {@link SpringAnimation}. It extends ValueAnimator so it can be used in an AnimatorSet.
*/
public class SpringObjectAnimator<T extends ProgressInterface> extends ValueAnimator {
public class SpringObjectAnimator<T> extends ValueAnimator {
private static final String TAG = "SpringObjectAnimator";
private static boolean DEBUG = false;
private T mObject;
private ObjectAnimator mObjectAnimator;
private float[] mValues;
@@ -57,29 +55,15 @@ public class SpringObjectAnimator<T extends ProgressInterface> extends ValueAnim
private boolean mAnimatorEnded = true;
private boolean mEnded = true;
private static final FloatPropertyCompat<ProgressInterface> sFloatProperty =
new FloatPropertyCompat<ProgressInterface>("springObjectAnimator") {
@Override
public float getValue(ProgressInterface object) {
return object.getProgress();
}
@Override
public void setValue(ProgressInterface object, float progress) {
object.setProgress(progress);
}
};
public SpringObjectAnimator(T object, String name, float minimumVisibleChange, float damping,
float stiffness, float... values) {
mObject = object;
mSpring = new SpringAnimation(object, sFloatProperty);
public SpringObjectAnimator(T object, FloatProperty<T> property, float minimumVisibleChange,
float damping, float stiffness, float... values) {
mSpring = new SpringAnimation(object, createFloatPropertyCompat(property));
mSpring.setMinimumVisibleChange(minimumVisibleChange);
mSpring.setSpring(new SpringForce(0)
.setDampingRatio(damping)
.setStiffness(stiffness));
mSpring.setStartVelocity(0.01f);
mProperty = new SpringProperty<T>(name, mSpring);
mProperty = new SpringProperty<>(property, mSpring);
mObjectAnimator = ObjectAnimator.ofFloat(object, mProperty, values);
mValues = values;
mListeners = new ArrayList<>();
@@ -285,13 +269,15 @@ public class SpringObjectAnimator<T extends ProgressInterface> extends ValueAnim
mObjectAnimator.setCurrentPlayTime(playTime);
}
public static class SpringProperty<T extends ProgressInterface> extends Property<T, Float> {
public static class SpringProperty<T> extends FloatProperty<T> {
boolean useSpring = false;
final FloatProperty<T> mProperty;
final SpringAnimation mSpring;
public SpringProperty(String name, SpringAnimation spring) {
super(Float.class, name);
public SpringProperty(FloatProperty<T> property, SpringAnimation spring) {
super(property.getName());
mProperty = property;
mSpring = spring;
}
@@ -301,15 +287,15 @@ public class SpringObjectAnimator<T extends ProgressInterface> extends ValueAnim
@Override
public Float get(T object) {
return object.getProgress();
return mProperty.get(object);
}
@Override
public void set(T object, Float progress) {
public void setValue(T object, float progress) {
if (useSpring) {
mSpring.animateToFinalPosition(progress);
} else {
object.setProgress(progress);
mProperty.setValue(object, progress);
}
}
}