blob: 6ba58b6a98a3da550bcad24f801fcc13ea271b92 [file] [log] [blame]
Sunny Goyal7185dd62018-03-14 17:51:49 -07001/*
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
17package com.android.launcher3.anim;
18
Vinit Nayak2a93bc52021-12-07 16:54:38 -080019import android.animation.Animator;
Sunny Goyal52bbef92022-05-19 09:27:26 -070020import android.animation.AnimatorSet;
Sunny Goyal7185dd62018-03-14 17:51:49 -070021import android.animation.TimeInterpolator;
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080022import android.util.FloatProperty;
23import android.util.IntProperty;
Sunny Goyal7185dd62018-03-14 17:51:49 -070024import android.view.View;
25
Sunny Goyal52bbef92022-05-19 09:27:26 -070026import androidx.annotation.NonNull;
27
28import java.util.function.Consumer;
29
Sunny Goyal7185dd62018-03-14 17:51:49 -070030/**
31 * Utility class for setting a property with or without animation
32 */
Sunny Goyal52bbef92022-05-19 09:27:26 -070033public abstract class PropertySetter {
Sunny Goyal7185dd62018-03-14 17:51:49 -070034
Sunny Goyal52bbef92022-05-19 09:27:26 -070035 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();
Luca Zuccarini05d19dc2022-10-27 09:30:22 +000041 animatorSet.end();
Sunny Goyal52bbef92022-05-19 09:27:26 -070042 }
43 };
44
45 protected static final AnimatorSet NO_OP = new AnimatorSet();
Sunny Goyal7185dd62018-03-14 17:51:49 -070046
Sunny Goyalf3ac7032020-03-13 13:01:33 -070047 /**
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 */
Sunny Goyal52bbef92022-05-19 09:27:26 -070052 @NonNull
53 public Animator setViewAlpha(View view, float alpha, TimeInterpolator interpolator) {
Tony Wickhambd6f05e2018-03-21 08:16:33 -070054 if (view != null) {
55 view.setAlpha(alpha);
Sunny Goyal18d71842018-03-27 13:44:00 -070056 AlphaUpdateListener.updateVisibility(view);
Tony Wickhambd6f05e2018-03-21 08:16:33 -070057 }
Sunny Goyal52bbef92022-05-19 09:27:26 -070058 return NO_OP;
Sunny Goyal7185dd62018-03-14 17:51:49 -070059 }
60
Sunny Goyalf3ac7032020-03-13 13:01:33 -070061 /**
Zak Cohen4d35ac32021-04-23 16:28:12 -070062 * Sets the background color of the provided view using the provided interpolator.
63 */
Sunny Goyal52bbef92022-05-19 09:27:26 -070064 @NonNull
65 public Animator setViewBackgroundColor(View view, int color, TimeInterpolator interpolator) {
Zak Cohen4d35ac32021-04-23 16:28:12 -070066 if (view != null) {
67 view.setBackgroundColor(color);
68 }
Sunny Goyal52bbef92022-05-19 09:27:26 -070069 return NO_OP;
Zak Cohen4d35ac32021-04-23 16:28:12 -070070 }
71
72 /**
Sunny Goyalf3ac7032020-03-13 13:01:33 -070073 * Updates the float property of the target using the provided interpolator
74 */
Sunny Goyal52bbef92022-05-19 09:27:26 -070075 @NonNull
76 public <T> Animator setFloat(T target, FloatProperty<T> property, float value,
Sunny Goyal7185dd62018-03-14 17:51:49 -070077 TimeInterpolator interpolator) {
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080078 property.setValue(target, value);
Sunny Goyal52bbef92022-05-19 09:27:26 -070079 return NO_OP;
Sunny Goyal7185dd62018-03-14 17:51:49 -070080 }
81
Sunny Goyalf3ac7032020-03-13 13:01:33 -070082 /**
83 * Updates the int property of the target using the provided interpolator
84 */
Sunny Goyal52bbef92022-05-19 09:27:26 -070085 @NonNull
86 public <T> Animator setInt(T target, IntProperty<T> property, int value,
Sunny Goyal7185dd62018-03-14 17:51:49 -070087 TimeInterpolator interpolator) {
Sunny Goyaldbcc63e2020-03-06 15:35:55 -080088 property.setValue(target, value);
Sunny Goyal52bbef92022-05-19 09:27:26 -070089 return NO_OP;
Sunny Goyal7185dd62018-03-14 17:51:49 -070090 }
Vinit Nayak2a93bc52021-12-07 16:54:38 -080091
Sunny Goyal52bbef92022-05-19 09:27:26 -070092 /**
Luca Zuccarini8009c1d2022-05-24 16:17:19 +000093 * Updates a color property of the target using the provided interpolator
94 */
95 @NonNull
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 /**
Sunny Goyal52bbef92022-05-19 09:27:26 -0700103 * Runs the animation as part of setting the property
104 */
105 public abstract void add(Animator animatorSet);
106
107 /**
108 * Add a listener of receiving the success/failure callback in the end.
109 */
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
118 public AnimatorSet buildAnim() {
119 return NO_OP;
Vinit Nayak2a93bc52021-12-07 16:54:38 -0800120 }
Sunny Goyal7185dd62018-03-14 17:51:49 -0700121}