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.TimeInterpolator;
20 import android.util.FloatProperty;
21 import android.util.IntProperty;
22 import android.view.View;
23 
24 /**
25  * Utility class for setting a property with or without animation
26  */
27 public interface PropertySetter {
28 
29     PropertySetter NO_ANIM_PROPERTY_SETTER = new PropertySetter() { };
30 
31     /**
32      * Sets the view alpha using the provided interpolator.
33      * Unlike {@link #setFloat}, this also updates the visibility of the view as alpha changes
34      * between zero and non-zero.
35      */
setViewAlpha(View view, float alpha, TimeInterpolator interpolator)36     default void setViewAlpha(View view, float alpha, TimeInterpolator interpolator) {
37         if (view != null) {
38             view.setAlpha(alpha);
39             AlphaUpdateListener.updateVisibility(view);
40         }
41     }
42 
43     /**
44      * Updates the float property of the target using the provided interpolator
45      */
setFloat(T target, FloatProperty<T> property, float value, TimeInterpolator interpolator)46     default <T> void setFloat(T target, FloatProperty<T> property, float value,
47             TimeInterpolator interpolator) {
48         property.setValue(target, value);
49     }
50 
51     /**
52      * Updates the int property of the target using the provided interpolator
53      */
setInt(T target, IntProperty<T> property, int value, TimeInterpolator interpolator)54     default <T> void setInt(T target, IntProperty<T> property, int value,
55             TimeInterpolator interpolator) {
56         property.setValue(target, value);
57     }
58 }
59