TaskViewTransform.java revision bf5dbf1c151eb64f4068f233e72b0a867348bf8c
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            boolean allowShadows, ValueAnimator.AnimatorUpdateListener updateCallback) {
82        // Check to see if any properties have changed, and update the task view
83        if (duration > 0) {
84            ViewPropertyAnimator anim = v.animate();
85            boolean requiresLayers = false;
86
87            // Animate to the final state
88            if (hasTranslationYChangedFrom(v.getTranslationY())) {
89                anim.translationY(translationY);
90            }
91            if (allowShadows && 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            if (updateCallback != null) {
108                anim.setUpdateListener(updateCallback);
109            } else {
110                anim.setUpdateListener(null);
111            }
112            anim.setStartDelay(startDelay)
113                    .setDuration(duration)
114                    .setInterpolator(interp)
115                    .start();
116        } else {
117            // Set the changed properties
118            if (hasTranslationYChangedFrom(v.getTranslationY())) {
119                v.setTranslationY(translationY);
120            }
121            if (allowShadows && hasTranslationZChangedFrom(v.getTranslationZ())) {
122                v.setTranslationZ(translationZ);
123            }
124            if (hasScaleChangedFrom(v.getScaleX())) {
125                v.setScaleX(scale);
126                v.setScaleY(scale);
127            }
128            if (hasAlphaChangedFrom(v.getAlpha())) {
129                v.setAlpha(alpha);
130            }
131        }
132    }
133
134    /** Reset the transform on a view. */
135    public static void reset(View v) {
136        v.setTranslationX(0f);
137        v.setTranslationY(0f);
138        v.setTranslationZ(0f);
139        v.setScaleX(1f);
140        v.setScaleY(1f);
141        v.setAlpha(1f);
142    }
143
144    @Override
145    public String toString() {
146        return "TaskViewTransform delay: " + startDelay + " y: " + translationY + " z: " + translationZ +
147                " scale: " + scale + " alpha: " + alpha + " visible: " + visible + " rect: " + rect +
148                " p: " + p;
149    }
150}
151