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.Property;
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 ALL_APPS_TRANSITION_MS = 320;
31     public static final int OVERVIEW_TRANSITION_MS = 250;
32     public static final int SPRING_LOADED_TRANSITION_MS = 150;
33     public static final int SPRING_LOADED_EXIT_DELAY = 500;
34 
35     // The progress of an animation to all apps must be at least this far along to snap to all apps.
36     public static final float MIN_PROGRESS_TO_ALL_APPS = 0.5f;
37 
38     public static final Property<Drawable, Integer> DRAWABLE_ALPHA =
39             new Property<Drawable, Integer>(Integer.TYPE, "drawableAlpha") {
40                 @Override
41                 public Integer get(Drawable drawable) {
42                     return drawable.getAlpha();
43                 }
44 
45                 @Override
46                 public void set(Drawable drawable, Integer alpha) {
47                     drawable.setAlpha(alpha);
48                 }
49             };
50 
51     public static final FloatProperty<View> SCALE_PROPERTY =
52             new FloatProperty<View>("scale") {
53                 @Override
54                 public Float get(View view) {
55                     return view.getScaleX();
56                 }
57 
58                 @Override
59                 public void setValue(View view, float scale) {
60                     view.setScaleX(scale);
61                     view.setScaleY(scale);
62                 }
63             };
64 
65     /** Increase the duration if we prevented the fling, as we are going against a high velocity. */
blockedFlingDurationFactor(float velocity)66     public static int blockedFlingDurationFactor(float velocity) {
67         return (int) Utilities.boundToRange(Math.abs(velocity) / 2, 2f, 6f);
68     }
69 
70     public static final Property<LayoutParams, Integer> LAYOUT_WIDTH =
71             new Property<LayoutParams, Integer>(Integer.TYPE, "width") {
72                 @Override
73                 public Integer get(LayoutParams lp) {
74                     return lp.width;
75                 }
76 
77                 @Override
78                 public void set(LayoutParams lp, Integer width) {
79                     lp.width = width;
80                 }
81             };
82 
83     public static final Property<LayoutParams, Integer> LAYOUT_HEIGHT =
84             new Property<LayoutParams, Integer>(Integer.TYPE, "height") {
85                 @Override
86                 public Integer get(LayoutParams lp) {
87                     return lp.height;
88                 }
89 
90                 @Override
91                 public void set(LayoutParams lp, Integer height) {
92                     lp.height = height;
93                 }
94             };
95 
96     public static final FloatProperty<View> VIEW_TRANSLATE_X =
97             View.TRANSLATION_X instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_X
98                     : new FloatProperty<View>("translateX") {
99                         @Override
100                         public void setValue(View view, float v) {
101                             view.setTranslationX(v);
102                         }
103 
104                         @Override
105                         public Float get(View view) {
106                             return view.getTranslationX();
107                         }
108                     };
109 
110     public static final FloatProperty<View> VIEW_TRANSLATE_Y =
111             View.TRANSLATION_Y instanceof FloatProperty ? (FloatProperty) View.TRANSLATION_Y
112                     : new FloatProperty<View>("translateY") {
113                         @Override
114                         public void setValue(View view, float v) {
115                             view.setTranslationY(v);
116                         }
117 
118                         @Override
119                         public Float get(View view) {
120                             return view.getTranslationY();
121                         }
122                     };
123 }
124