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