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