1 /*
2  * Copyright (C) 2012 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;
18 
19 import android.graphics.drawable.Drawable;
20 import android.util.FloatProperty;
21 import android.util.IntProperty;
22 import android.view.View;
23 import android.view.ViewGroup.LayoutParams;
24 
25 public class LauncherAnimUtils {
26     /**
27      * Durations for various state animations. These are not defined in resources to allow
28      * easier access from static classes and enums
29      */
30     public static final int SPRING_LOADED_EXIT_DELAY = 500;
31 
32     // The progress of an animation to all apps must be at least this far along to snap to all apps.
33     public static final float MIN_PROGRESS_TO_ALL_APPS = 0.5f;
34 
35     public static final IntProperty<Drawable> DRAWABLE_ALPHA =
36             new IntProperty<Drawable>("drawableAlpha") {
37                 @Override
38                 public Integer get(Drawable drawable) {
39                     return drawable.getAlpha();
40                 }
41 
42                 @Override
43                 public void setValue(Drawable drawable, int alpha) {
44                     drawable.setAlpha(alpha);
45                 }
46             };
47 
48     public static final FloatProperty<View> SCALE_PROPERTY =
49             new FloatProperty<View>("scale") {
50                 @Override
51                 public Float get(View view) {
52                     return view.getScaleX();
53                 }
54 
55                 @Override
56                 public void setValue(View view, float scale) {
57                     view.setScaleX(scale);
58                     view.setScaleY(scale);
59                 }
60             };
61 
62     /** Increase the duration if we prevented the fling, as we are going against a high velocity. */
blockedFlingDurationFactor(float velocity)63     public static int blockedFlingDurationFactor(float velocity) {
64         return (int) Utilities.boundToRange(Math.abs(velocity) / 2, 2f, 6f);
65     }
66 
67     public static final IntProperty<LayoutParams> LAYOUT_WIDTH =
68             new IntProperty<LayoutParams>("width") {
69                 @Override
70                 public Integer get(LayoutParams lp) {
71                     return lp.width;
72                 }
73 
74                 @Override
75                 public void setValue(LayoutParams lp, int width) {
76                     lp.width = width;
77                 }
78             };
79 
80     public static final IntProperty<LayoutParams> LAYOUT_HEIGHT =
81             new IntProperty<LayoutParams>("height") {
82                 @Override
83                 public Integer get(LayoutParams lp) {
84                     return lp.height;
85                 }
86 
87                 @Override
88                 public void setValue(LayoutParams lp, int height) {
89                     lp.height = height;
90                 }
91             };
92 
93     public static final FloatProperty<View> VIEW_TRANSLATE_X =
94             View.TRANSLATION_X instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_X
95                     : new FloatProperty<View>("translateX") {
96                         @Override
97                         public void setValue(View view, float v) {
98                             view.setTranslationX(v);
99                         }
100 
101                         @Override
102                         public Float get(View view) {
103                             return view.getTranslationX();
104                         }
105                     };
106 
107     public static final FloatProperty<View> VIEW_TRANSLATE_Y =
108             View.TRANSLATION_Y instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_Y
109                     : new FloatProperty<View>("translateY") {
110                         @Override
111                         public void setValue(View view, float v) {
112                             view.setTranslationY(v);
113                         }
114 
115                         @Override
116                         public Float get(View view) {
117                             return view.getTranslationY();
118                         }
119                     };
120 
121     public static final FloatProperty<View> VIEW_ALPHA =
122             View.ALPHA instanceof FloatProperty ? (FloatProperty) View.ALPHA
123                     : new FloatProperty<View>("alpha") {
124                         @Override
125                         public void setValue(View view, float v) {
126                             view.setAlpha(v);
127                         }
128 
129                         @Override
130                         public Float get(View view) {
131                             return view.getAlpha();
132                         }
133                     };
134 }
135