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