TaskViewTransform.java revision 23afcae84422ac7c9687b17a6193f74264361303
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.ValueAnimator;
20import android.graphics.RectF;
21import android.view.View;
22import android.view.ViewPropertyAnimator;
23import android.view.animation.Interpolator;
24
25
26/* The transform state for a task view */
27public class TaskViewTransform {
28    public int startDelay = 0;
29    public int translationX = 0;
30    public int translationY = 0;
31    public float translationZ = 0;
32    public float scale = 1f;
33    public float alpha = 1f;
34    public boolean visible = false;
35    float p = 0f;
36
37    // This is a window-space rect that is purely used for coordinating the animation of an app
38    // window into Recents.
39    public RectF rect = new RectF();
40
41    public TaskViewTransform() {
42        // Do nothing
43    }
44
45    public TaskViewTransform(TaskViewTransform o) {
46        startDelay = o.startDelay;
47        translationX = o.translationX;
48        translationY = o.translationY;
49        translationZ = o.translationZ;
50        scale = o.scale;
51        alpha = o.alpha;
52        visible = o.visible;
53        rect.set(o.rect);
54        p = o.p;
55    }
56
57    /**
58     * Resets the current transform.
59     */
60    public void reset() {
61        startDelay = 0;
62        translationX = 0;
63        translationY = 0;
64        translationZ = 0;
65        scale = 1f;
66        alpha = 1f;
67        visible = false;
68        rect.setEmpty();
69        p = 0f;
70    }
71
72    /** Convenience functions to compare against current property values */
73    public boolean hasAlphaChangedFrom(float v) {
74        return (Float.compare(alpha, v) != 0);
75    }
76    public boolean hasScaleChangedFrom(float v) {
77        return (Float.compare(scale, v) != 0);
78    }
79    public boolean hasTranslationXChangedFrom(float v) {
80        return (Float.compare(translationX, v) != 0);
81    }
82    public boolean hasTranslationYChangedFrom(float v) {
83        return (Float.compare(translationY, v) != 0);
84    }
85    public boolean hasTranslationZChangedFrom(float v) {
86        return (Float.compare(translationZ, v) != 0);
87    }
88
89    /** Applies this transform to a view. */
90    public void applyToTaskView(View v, int duration, Interpolator interp, boolean allowLayers,
91            boolean allowShadows, ValueAnimator.AnimatorUpdateListener updateCallback) {
92        // Check to see if any properties have changed, and update the task view
93        if (duration > 0) {
94            ViewPropertyAnimator anim = v.animate();
95            boolean requiresLayers = false;
96
97            // Animate to the final state
98            if (hasTranslationXChangedFrom(v.getTranslationX())) {
99                anim.translationX(translationX);
100            }
101            if (hasTranslationYChangedFrom(v.getTranslationY())) {
102                anim.translationY(translationY);
103            }
104            if (allowShadows && hasTranslationZChangedFrom(v.getTranslationZ())) {
105                anim.translationZ(translationZ);
106            }
107            if (hasScaleChangedFrom(v.getScaleX())) {
108                anim.scaleX(scale)
109                    .scaleY(scale);
110                requiresLayers = true;
111            }
112            if (hasAlphaChangedFrom(v.getAlpha())) {
113                // Use layers if we animate alpha
114                anim.alpha(alpha);
115                requiresLayers = true;
116            }
117            if (requiresLayers && allowLayers) {
118                anim.withLayer();
119            }
120            if (updateCallback != null) {
121                anim.setUpdateListener(updateCallback);
122            } else {
123                anim.setUpdateListener(null);
124            }
125            anim.setStartDelay(startDelay)
126                    .setDuration(duration)
127                    .setInterpolator(interp)
128                    .start();
129        } else {
130            // Set the changed properties
131            if (hasTranslationXChangedFrom(v.getTranslationX())) {
132                v.setTranslationX(translationX);
133            }
134            if (hasTranslationYChangedFrom(v.getTranslationY())) {
135                v.setTranslationY(translationY);
136            }
137            if (allowShadows && hasTranslationZChangedFrom(v.getTranslationZ())) {
138                v.setTranslationZ(translationZ);
139            }
140            if (hasScaleChangedFrom(v.getScaleX())) {
141                v.setScaleX(scale);
142                v.setScaleY(scale);
143            }
144            if (hasAlphaChangedFrom(v.getAlpha())) {
145                v.setAlpha(alpha);
146            }
147        }
148    }
149
150    /** Reset the transform on a view. */
151    public static void reset(View v) {
152        // Cancel any running animations
153        v.animate().cancel();
154        v.setTranslationX(0f);
155        v.setTranslationY(0f);
156        v.setTranslationZ(0f);
157        v.setScaleX(1f);
158        v.setScaleY(1f);
159        v.setAlpha(1f);
160    }
161
162    @Override
163    public String toString() {
164        return "TaskViewTransform delay: " + startDelay +
165                " x: " + translationX + " y: " + translationY + " z: " + translationZ +
166                " scale: " + scale + " alpha: " + alpha + " visible: " + visible + " rect: " + rect +
167                " p: " + p;
168    }
169}
170