1 /* 2 * Copyright (C) 2016 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.anim; 18 19 import android.animation.Animator; 20 import android.animation.AnimatorSet; 21 import android.animation.TimeInterpolator; 22 import android.util.FloatProperty; 23 import android.util.IntProperty; 24 import android.view.View; 25 26 import androidx.annotation.NonNull; 27 28 import java.util.function.Consumer; 29 30 /** 31 * Utility class for setting a property with or without animation 32 */ 33 public abstract class PropertySetter { 34 35 public static final PropertySetter NO_ANIM_PROPERTY_SETTER = new PropertySetter() { 36 37 @Override 38 public void add(Animator animatorSet) { 39 animatorSet.setDuration(0); 40 animatorSet.start(); 41 animatorSet.end(); 42 } 43 }; 44 45 protected static final AnimatorSet NO_OP = new AnimatorSet(); 46 47 /** 48 * Sets the view alpha using the provided interpolator. 49 * Unlike {@link #setFloat}, this also updates the visibility of the view as alpha changes 50 * between zero and non-zero. 51 */ 52 @NonNull setViewAlpha(View view, float alpha, TimeInterpolator interpolator)53 public Animator setViewAlpha(View view, float alpha, TimeInterpolator interpolator) { 54 if (view != null) { 55 view.setAlpha(alpha); 56 AlphaUpdateListener.updateVisibility(view); 57 } 58 return NO_OP; 59 } 60 61 /** 62 * Sets the background color of the provided view using the provided interpolator. 63 */ 64 @NonNull setViewBackgroundColor(View view, int color, TimeInterpolator interpolator)65 public Animator setViewBackgroundColor(View view, int color, TimeInterpolator interpolator) { 66 if (view != null) { 67 view.setBackgroundColor(color); 68 } 69 return NO_OP; 70 } 71 72 /** 73 * Updates the float property of the target using the provided interpolator 74 */ 75 @NonNull setFloat(T target, FloatProperty<T> property, float value, TimeInterpolator interpolator)76 public <T> Animator setFloat(T target, FloatProperty<T> property, float value, 77 TimeInterpolator interpolator) { 78 property.setValue(target, value); 79 return NO_OP; 80 } 81 82 /** 83 * Updates the int property of the target using the provided interpolator 84 */ 85 @NonNull setInt(T target, IntProperty<T> property, int value, TimeInterpolator interpolator)86 public <T> Animator setInt(T target, IntProperty<T> property, int value, 87 TimeInterpolator interpolator) { 88 property.setValue(target, value); 89 return NO_OP; 90 } 91 92 /** 93 * Updates a color property of the target using the provided interpolator 94 */ 95 @NonNull setColor(T target, IntProperty<T> property, int value, TimeInterpolator interpolator)96 public <T> Animator setColor(T target, IntProperty<T> property, int value, 97 TimeInterpolator interpolator) { 98 property.setValue(target, value); 99 return NO_OP; 100 } 101 102 /** 103 * Runs the animation as part of setting the property 104 */ add(Animator animatorSet)105 public abstract void add(Animator animatorSet); 106 107 /** 108 * Add a listener of receiving the success/failure callback in the end. 109 */ addEndListener(Consumer<Boolean> listener)110 public void addEndListener(Consumer<Boolean> listener) { 111 listener.accept(true); 112 } 113 114 /** 115 * Creates and returns the AnimatorSet that can be run to apply the properties 116 */ 117 @NonNull buildAnim()118 public AnimatorSet buildAnim() { 119 return NO_OP; 120 } 121 } 122