Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 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 | |
Vinit Nayak | 2a93bc5 | 2021-12-07 16:54:38 -0800 | [diff] [blame] | 19 | import android.animation.Animator; |
Sunny Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 20 | import android.animation.AnimatorSet; |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 21 | import android.animation.TimeInterpolator; |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 22 | import android.util.FloatProperty; |
| 23 | import android.util.IntProperty; |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 24 | import android.view.View; |
| 25 | |
Sunny Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 26 | import androidx.annotation.NonNull; |
| 27 | |
| 28 | import java.util.function.Consumer; |
| 29 | |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Utility class for setting a property with or without animation |
| 32 | */ |
Sunny Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 33 | public abstract class PropertySetter { |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 34 | |
Sunny Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 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(); |
Luca Zuccarini | 05d19dc | 2022-10-27 09:30:22 +0000 | [diff] [blame] | 41 | animatorSet.end(); |
Sunny Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 42 | } |
| 43 | }; |
| 44 | |
| 45 | protected static final AnimatorSet NO_OP = new AnimatorSet(); |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 46 | |
Sunny Goyal | f3ac703 | 2020-03-13 13:01:33 -0700 | [diff] [blame] | 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 | */ |
Sunny Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 52 | @NonNull |
| 53 | public Animator setViewAlpha(View view, float alpha, TimeInterpolator interpolator) { |
Tony Wickham | bd6f05e | 2018-03-21 08:16:33 -0700 | [diff] [blame] | 54 | if (view != null) { |
| 55 | view.setAlpha(alpha); |
Sunny Goyal | 18d7184 | 2018-03-27 13:44:00 -0700 | [diff] [blame] | 56 | AlphaUpdateListener.updateVisibility(view); |
Tony Wickham | bd6f05e | 2018-03-21 08:16:33 -0700 | [diff] [blame] | 57 | } |
Sunny Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 58 | return NO_OP; |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Sunny Goyal | f3ac703 | 2020-03-13 13:01:33 -0700 | [diff] [blame] | 61 | /** |
Zak Cohen | 4d35ac3 | 2021-04-23 16:28:12 -0700 | [diff] [blame] | 62 | * Sets the background color of the provided view using the provided interpolator. |
| 63 | */ |
Sunny Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 64 | @NonNull |
| 65 | public Animator setViewBackgroundColor(View view, int color, TimeInterpolator interpolator) { |
Zak Cohen | 4d35ac3 | 2021-04-23 16:28:12 -0700 | [diff] [blame] | 66 | if (view != null) { |
| 67 | view.setBackgroundColor(color); |
| 68 | } |
Sunny Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 69 | return NO_OP; |
Zak Cohen | 4d35ac3 | 2021-04-23 16:28:12 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
Sunny Goyal | f3ac703 | 2020-03-13 13:01:33 -0700 | [diff] [blame] | 73 | * Updates the float property of the target using the provided interpolator |
| 74 | */ |
Sunny Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 75 | @NonNull |
| 76 | public <T> Animator setFloat(T target, FloatProperty<T> property, float value, |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 77 | TimeInterpolator interpolator) { |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 78 | property.setValue(target, value); |
Sunny Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 79 | return NO_OP; |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Sunny Goyal | f3ac703 | 2020-03-13 13:01:33 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Updates the int property of the target using the provided interpolator |
| 84 | */ |
Sunny Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 85 | @NonNull |
| 86 | public <T> Animator setInt(T target, IntProperty<T> property, int value, |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 87 | TimeInterpolator interpolator) { |
Sunny Goyal | dbcc63e | 2020-03-06 15:35:55 -0800 | [diff] [blame] | 88 | property.setValue(target, value); |
Sunny Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 89 | return NO_OP; |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 90 | } |
Vinit Nayak | 2a93bc5 | 2021-12-07 16:54:38 -0800 | [diff] [blame] | 91 | |
Sunny Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 92 | /** |
Luca Zuccarini | 8009c1d | 2022-05-24 16:17:19 +0000 | [diff] [blame] | 93 | * 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 Goyal | 52bbef9 | 2022-05-19 09:27:26 -0700 | [diff] [blame] | 103 | * 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 Nayak | 2a93bc5 | 2021-12-07 16:54:38 -0800 | [diff] [blame] | 120 | } |
Sunny Goyal | 7185dd6 | 2018-03-14 17:51:49 -0700 | [diff] [blame] | 121 | } |