• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ObjectAnimator;
21 import android.animation.TimeInterpolator;
22 import android.util.Property;
23 import android.view.View;
24 
25 /**
26  * Utility class for setting a property with or without animation
27  */
28 public class PropertySetter {
29 
30     public static final PropertySetter NO_ANIM_PROPERTY_SETTER = new PropertySetter();
31 
setViewAlpha(View view, float alpha, TimeInterpolator interpolator)32     public void setViewAlpha(View view, float alpha, TimeInterpolator interpolator) {
33         if (view != null) {
34             view.setAlpha(alpha);
35             AlphaUpdateListener.updateVisibility(view);
36         }
37     }
38 
setFloat(T target, Property<T, Float> property, float value, TimeInterpolator interpolator)39     public <T> void setFloat(T target, Property<T, Float> property, float value,
40             TimeInterpolator interpolator) {
41         property.set(target, value);
42     }
43 
setInt(T target, Property<T, Integer> property, int value, TimeInterpolator interpolator)44     public <T> void setInt(T target, Property<T, Integer> property, int value,
45             TimeInterpolator interpolator) {
46         property.set(target, value);
47     }
48 
49     public static class AnimatedPropertySetter extends PropertySetter {
50 
51         private final long mDuration;
52         private final AnimatorSetBuilder mStateAnimator;
53 
AnimatedPropertySetter(long duration, AnimatorSetBuilder builder)54         public AnimatedPropertySetter(long duration, AnimatorSetBuilder builder) {
55             mDuration = duration;
56             mStateAnimator = builder;
57         }
58 
59         @Override
setViewAlpha(View view, float alpha, TimeInterpolator interpolator)60         public void setViewAlpha(View view, float alpha, TimeInterpolator interpolator) {
61             if (view == null || view.getAlpha() == alpha) {
62                 return;
63             }
64             ObjectAnimator anim = ObjectAnimator.ofFloat(view, View.ALPHA, alpha);
65             anim.addListener(new AlphaUpdateListener(view));
66             anim.setDuration(mDuration).setInterpolator(interpolator);
67             mStateAnimator.play(anim);
68         }
69 
70         @Override
setFloat(T target, Property<T, Float> property, float value, TimeInterpolator interpolator)71         public <T> void setFloat(T target, Property<T, Float> property, float value,
72                 TimeInterpolator interpolator) {
73             if (property.get(target) == value) {
74                 return;
75             }
76             Animator anim = ObjectAnimator.ofFloat(target, property, value);
77             anim.setDuration(mDuration).setInterpolator(interpolator);
78             mStateAnimator.play(anim);
79         }
80 
81         @Override
setInt(T target, Property<T, Integer> property, int value, TimeInterpolator interpolator)82         public <T> void setInt(T target, Property<T, Integer> property, int value,
83                 TimeInterpolator interpolator) {
84             if (property.get(target) == value) {
85                 return;
86             }
87             Animator anim = ObjectAnimator.ofInt(target, property, value);
88             anim.setDuration(mDuration).setInterpolator(interpolator);
89             mStateAnimator.play(anim);
90         }
91     }
92 }
93