TaskViewTransform.java revision 7845e8c4946f6b5dadfcd9c1d64e826bacc50edb
1/*
2 * Copyright (C) 2014 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
17package com.android.systemui.recents.views;
18
19import android.animation.Animator;
20import android.animation.ObjectAnimator;
21import android.animation.PropertyValuesHolder;
22import android.graphics.RectF;
23import android.util.IntProperty;
24import android.util.Property;
25import android.view.View;
26
27import java.util.ArrayList;
28
29/**
30 * The visual properties for a {@link TaskView}.
31 */
32public class TaskViewTransform {
33
34    public static final Property<View, Integer> LEFT =
35            new IntProperty<View>("left") {
36                @Override
37                public void setValue(View object, int v) {
38                    object.setLeft(v);
39                }
40
41                @Override
42                public Integer get(View object) {
43                    return object.getLeft();
44                }
45            };
46
47    public static final Property<View, Integer> TOP =
48            new IntProperty<View>("top") {
49                @Override
50                public void setValue(View object, int v) {
51                    object.setTop(v);
52                }
53
54                @Override
55                public Integer get(View object) {
56                    return object.getTop();
57                }
58            };
59
60    public static final Property<View, Integer> RIGHT =
61            new IntProperty<View>("right") {
62                @Override
63                public void setValue(View object, int v) {
64                    object.setRight(v);
65                }
66
67                @Override
68                public Integer get(View object) {
69                    return object.getRight();
70                }
71            };
72
73    public static final Property<View, Integer> BOTTOM =
74            new IntProperty<View>("bottom") {
75                @Override
76                public void setValue(View object, int v) {
77                    object.setBottom(v);
78                }
79
80                @Override
81                public Integer get(View object) {
82                    return object.getBottom();
83                }
84            };
85
86    public float translationZ = 0;
87    public float scale = 1f;
88    public float alpha = 1f;
89    public float dimAlpha = 0f;
90    public float viewOutlineAlpha = 0f;
91
92    public boolean visible = false;
93
94    // This is a window-space rect used for positioning the task in the stack and freeform workspace
95    public RectF rect = new RectF();
96
97    /**
98     * Fills int this transform from the state of the given TaskView.
99     */
100    public void fillIn(TaskView tv) {
101        translationZ = tv.getTranslationZ();
102        scale = tv.getScaleX();
103        alpha = tv.getAlpha();
104        visible = true;
105        dimAlpha = tv.getDimAlpha();
106        viewOutlineAlpha = tv.getViewBounds().getAlpha();
107        rect.set(tv.getLeft(), tv.getTop(), tv.getRight(), tv.getBottom());
108    }
109
110    /**
111     * Copies the transform state from another {@link TaskViewTransform}.
112     */
113    public void copyFrom(TaskViewTransform other) {
114        translationZ = other.translationZ;
115        scale = other.scale;
116        alpha = other.alpha;
117        visible = other.visible;
118        dimAlpha = other.dimAlpha;
119        viewOutlineAlpha = other.viewOutlineAlpha;
120        rect.set(other.rect);
121    }
122
123    /**
124     * @return whether {@param other} is the same transform as this
125     */
126    public boolean isSame(TaskViewTransform other) {
127        return translationZ == other.translationZ
128                && scale == other.scale
129                && other.alpha == alpha
130                && dimAlpha == other.dimAlpha
131                && visible == other.visible
132                && rect.equals(other.rect);
133    }
134
135    /**
136     * Resets the current transform.
137     */
138    public void reset() {
139        translationZ = 0;
140        scale = 1f;
141        alpha = 1f;
142        dimAlpha = 0f;
143        viewOutlineAlpha = 0f;
144        visible = false;
145        rect.setEmpty();
146    }
147
148    /** Convenience functions to compare against current property values */
149    public boolean hasAlphaChangedFrom(float v) {
150        return (Float.compare(alpha, v) != 0);
151    }
152
153    public boolean hasScaleChangedFrom(float v) {
154        return (Float.compare(scale, v) != 0);
155    }
156
157    public boolean hasTranslationZChangedFrom(float v) {
158        return (Float.compare(translationZ, v) != 0);
159    }
160
161    public boolean hasRectChangedFrom(View v) {
162        return ((int) rect.left != v.getLeft()) || ((int) rect.right != v.getRight()) ||
163                ((int) rect.top != v.getTop()) || ((int) rect.bottom != v.getBottom());
164    }
165
166    /**
167     * Applies this transform to a view.
168     */
169    public void applyToTaskView(TaskView v, ArrayList<Animator> animators,
170            AnimationProps animation, boolean allowShadows) {
171        // Return early if not visible
172        if (!visible) {
173            return;
174        }
175
176        if (animation.isImmediate()) {
177            if (allowShadows && hasTranslationZChangedFrom(v.getTranslationZ())) {
178                v.setTranslationZ(translationZ);
179            }
180            if (hasScaleChangedFrom(v.getScaleX())) {
181                v.setScaleX(scale);
182                v.setScaleY(scale);
183            }
184            if (hasAlphaChangedFrom(v.getAlpha())) {
185                v.setAlpha(alpha);
186            }
187            if (hasRectChangedFrom(v)) {
188                v.setLeftTopRightBottom((int) rect.left, (int) rect.top, (int) rect.right,
189                        (int) rect.bottom);
190            }
191        } else {
192            if (allowShadows && hasTranslationZChangedFrom(v.getTranslationZ())) {
193                ObjectAnimator anim = ObjectAnimator.ofFloat(v, View.TRANSLATION_Z,
194                        v.getTranslationZ(), translationZ);
195                animators.add(animation.apply(AnimationProps.TRANSLATION_Z, anim));
196            }
197            if (hasScaleChangedFrom(v.getScaleX())) {
198                ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(v,
199                        PropertyValuesHolder.ofFloat(View.SCALE_X, v.getScaleX(), scale),
200                        PropertyValuesHolder.ofFloat(View.SCALE_Y, v.getScaleX(), scale));
201                animators.add(animation.apply(AnimationProps.SCALE, anim));
202            }
203            if (hasAlphaChangedFrom(v.getAlpha())) {
204                ObjectAnimator anim = ObjectAnimator.ofFloat(v, View.ALPHA, v.getAlpha(), alpha);
205                animators.add(animation.apply(AnimationProps.ALPHA, anim));
206            }
207            if (hasRectChangedFrom(v)) {
208                ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(v,
209                        PropertyValuesHolder.ofInt(LEFT, v.getLeft(), (int) rect.left),
210                        PropertyValuesHolder.ofInt(TOP, v.getTop(), (int) rect.top),
211                        PropertyValuesHolder.ofInt(RIGHT, v.getRight(), (int) rect.right),
212                        PropertyValuesHolder.ofInt(BOTTOM, v.getBottom(), (int) rect.bottom));
213                animators.add(animation.apply(AnimationProps.BOUNDS, anim));
214            }
215        }
216    }
217
218    /** Reset the transform on a view. */
219    public static void reset(TaskView v) {
220        v.setTranslationX(0f);
221        v.setTranslationY(0f);
222        v.setTranslationZ(0f);
223        v.setScaleX(1f);
224        v.setScaleY(1f);
225        v.setAlpha(1f);
226        v.getViewBounds().setClipBottom(0);
227        v.setLeftTopRightBottom(0, 0, 0, 0);
228    }
229
230    @Override
231    public String toString() {
232        return "R: " + rect + " V: " + visible;
233    }
234}
235