Adding support for customiting the animation in PropertySetter

Bug: 233223446
Test: Manual
Change-Id: I53fc39fa4871c9ea5b6eaf324ec1054140ccc292
This commit is contained in:
Sunny Goyal
2022-05-23 09:55:56 -07:00
parent 2940565595
commit 52bbef9d94
3 changed files with 194 additions and 106 deletions
@@ -17,57 +17,94 @@
package com.android.launcher3.anim;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.TimeInterpolator;
import android.util.FloatProperty;
import android.util.IntProperty;
import android.view.View;
import androidx.annotation.NonNull;
import java.util.function.Consumer;
/**
* Utility class for setting a property with or without animation
*/
public interface PropertySetter {
public abstract class PropertySetter {
PropertySetter NO_ANIM_PROPERTY_SETTER = new PropertySetter() { };
public static final PropertySetter NO_ANIM_PROPERTY_SETTER = new PropertySetter() {
@Override
public void add(Animator animatorSet) {
animatorSet.setDuration(0);
animatorSet.start();
}
};
protected static final AnimatorSet NO_OP = new AnimatorSet();
/**
* Sets the view alpha using the provided interpolator.
* Unlike {@link #setFloat}, this also updates the visibility of the view as alpha changes
* between zero and non-zero.
*/
default void setViewAlpha(View view, float alpha, TimeInterpolator interpolator) {
@NonNull
public Animator setViewAlpha(View view, float alpha, TimeInterpolator interpolator) {
if (view != null) {
view.setAlpha(alpha);
AlphaUpdateListener.updateVisibility(view);
}
return NO_OP;
}
/**
* Sets the background color of the provided view using the provided interpolator.
*/
default void setViewBackgroundColor(View view, int color, TimeInterpolator interpolator) {
@NonNull
public Animator setViewBackgroundColor(View view, int color, TimeInterpolator interpolator) {
if (view != null) {
view.setBackgroundColor(color);
}
return NO_OP;
}
/**
* Updates the float property of the target using the provided interpolator
*/
default <T> void setFloat(T target, FloatProperty<T> property, float value,
@NonNull
public <T> Animator setFloat(T target, FloatProperty<T> property, float value,
TimeInterpolator interpolator) {
property.setValue(target, value);
return NO_OP;
}
/**
* Updates the int property of the target using the provided interpolator
*/
default <T> void setInt(T target, IntProperty<T> property, int value,
@NonNull
public <T> Animator setInt(T target, IntProperty<T> property, int value,
TimeInterpolator interpolator) {
property.setValue(target, value);
return NO_OP;
}
default void add(Animator animatorSet) {
animatorSet.setDuration(0);
animatorSet.start();
/**
* Runs the animation as part of setting the property
*/
public abstract void add(Animator animatorSet);
/**
* Add a listener of receiving the success/failure callback in the end.
*/
public void addEndListener(Consumer<Boolean> listener) {
listener.accept(true);
}
/**
* Creates and returns the AnimatorSet that can be run to apply the properties
*/
@NonNull
public AnimatorSet buildAnim() {
return NO_OP;
}
}