TaskViewTransform.java revision be8e696a1f40a61c52a74022f4f084fdc0600f90
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.Animator; 20import android.animation.ObjectAnimator; 21import android.animation.PropertyValuesHolder; 22import android.graphics.RectF; 23import android.util.IntProperty; 24import android.util.Property; 25import android.view.View; 26 27import java.util.ArrayList; 28 29/** 30 * The visual properties for a {@link TaskView}. 31 */ 32public class TaskViewTransform { 33 34 public static final Property<View, Integer> LEFT = 35 new IntProperty<View>("left") { 36 @Override 37 public void setValue(View object, int v) { 38 object.setLeft(v); 39 } 40 41 @Override 42 public Integer get(View object) { 43 return object.getLeft(); 44 } 45 }; 46 47 public static final Property<View, Integer> TOP = 48 new IntProperty<View>("top") { 49 @Override 50 public void setValue(View object, int v) { 51 object.setTop(v); 52 } 53 54 @Override 55 public Integer get(View object) { 56 return object.getTop(); 57 } 58 }; 59 60 public static final Property<View, Integer> RIGHT = 61 new IntProperty<View>("right") { 62 @Override 63 public void setValue(View object, int v) { 64 object.setRight(v); 65 } 66 67 @Override 68 public Integer get(View object) { 69 return object.getRight(); 70 } 71 }; 72 73 public static final Property<View, Integer> BOTTOM = 74 new IntProperty<View>("bottom") { 75 @Override 76 public void setValue(View object, int v) { 77 object.setBottom(v); 78 } 79 80 @Override 81 public Integer get(View object) { 82 return object.getBottom(); 83 } 84 }; 85 86 public float translationZ = 0; 87 public float scale = 1f; 88 public float alpha = 1f; 89 90 public boolean visible = false; 91 92 // This is the relative task progress of this task, relative to the stack scroll at which this 93 // transform was computed 94 public float p = 0f; 95 96 // This is a window-space rect used for positioning the task in the stack and freeform workspace 97 public RectF rect = new RectF(); 98 99 /** 100 * Fills int this transform from the state of the given TaskView. 101 */ 102 public void fillIn(TaskView tv) { 103 translationZ = tv.getTranslationZ(); 104 scale = tv.getScaleX(); 105 alpha = tv.getAlpha(); 106 visible = true; 107 p = tv.getTaskProgress(); 108 rect.set(tv.getLeft(), tv.getTop(), tv.getRight(), tv.getBottom()); 109 } 110 111 /** 112 * Copies the transform state from another {@link TaskViewTransform}. 113 */ 114 public void copyFrom(TaskViewTransform other) { 115 translationZ = other.translationZ; 116 scale = other.scale; 117 alpha = other.alpha; 118 visible = other.visible; 119 p = other.p; 120 rect.set(other.rect); 121 } 122 123 /** 124 * Resets the current transform. 125 */ 126 public void reset() { 127 translationZ = 0; 128 scale = 1f; 129 alpha = 1f; 130 visible = false; 131 rect.setEmpty(); 132 p = 0f; 133 } 134 135 /** Convenience functions to compare against current property values */ 136 public boolean hasAlphaChangedFrom(float v) { 137 return (Float.compare(alpha, v) != 0); 138 } 139 140 public boolean hasScaleChangedFrom(float v) { 141 return (Float.compare(scale, v) != 0); 142 } 143 144 public boolean hasTranslationZChangedFrom(float v) { 145 return (Float.compare(translationZ, v) != 0); 146 } 147 148 public boolean hasRectChangedFrom(View v) { 149 return ((int) rect.left != v.getLeft()) || ((int) rect.right != v.getRight()) || 150 ((int) rect.top != v.getTop()) || ((int) rect.bottom != v.getBottom()); 151 } 152 153 /** 154 * Applies this transform to a view. 155 */ 156 public void applyToTaskView(TaskView v, ArrayList<Animator> animators, 157 AnimationProps animation, boolean allowShadows) { 158 // Return early if not visible 159 if (!visible) { 160 return; 161 } 162 163 if (animation.isImmediate()) { 164 if (allowShadows && hasTranslationZChangedFrom(v.getTranslationZ())) { 165 v.setTranslationZ(translationZ); 166 } 167 if (hasScaleChangedFrom(v.getScaleX())) { 168 v.setScaleX(scale); 169 v.setScaleY(scale); 170 } 171 if (hasAlphaChangedFrom(v.getAlpha())) { 172 v.setAlpha(alpha); 173 } 174 if (hasRectChangedFrom(v)) { 175 v.setLeftTopRightBottom((int) rect.left, (int) rect.top, (int) rect.right, 176 (int) rect.bottom); 177 } 178 } else { 179 if (allowShadows && hasTranslationZChangedFrom(v.getTranslationZ())) { 180 ObjectAnimator anim = ObjectAnimator.ofFloat(v, View.TRANSLATION_Z, 181 v.getTranslationZ(), translationZ); 182 animators.add(animation.apply(AnimationProps.TRANSLATION_Z, anim)); 183 } 184 if (hasScaleChangedFrom(v.getScaleX())) { 185 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(v, 186 PropertyValuesHolder.ofFloat(View.SCALE_X, v.getScaleX(), scale), 187 PropertyValuesHolder.ofFloat(View.SCALE_Y, v.getScaleX(), scale)); 188 animators.add(animation.apply(AnimationProps.SCALE, anim)); 189 } 190 if (hasAlphaChangedFrom(v.getAlpha())) { 191 ObjectAnimator anim = ObjectAnimator.ofFloat(v, View.ALPHA, v.getAlpha(), alpha); 192 animators.add(animation.apply(AnimationProps.ALPHA, anim)); 193 } 194 if (hasRectChangedFrom(v)) { 195 ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(v, 196 PropertyValuesHolder.ofInt(LEFT, v.getLeft(), (int) rect.left), 197 PropertyValuesHolder.ofInt(TOP, v.getTop(), (int) rect.top), 198 PropertyValuesHolder.ofInt(RIGHT, v.getRight(), (int) rect.right), 199 PropertyValuesHolder.ofInt(BOTTOM, v.getBottom(), (int) rect.bottom)); 200 animators.add(animation.apply(AnimationProps.BOUNDS, anim)); 201 } 202 } 203 } 204 205 /** Reset the transform on a view. */ 206 public static void reset(TaskView v) { 207 v.setTranslationX(0f); 208 v.setTranslationY(0f); 209 v.setTranslationZ(0f); 210 v.setScaleX(1f); 211 v.setScaleY(1f); 212 v.setAlpha(1f); 213 v.getViewBounds().setClipBottom(0); 214 v.setLeftTopRightBottom(0, 0, 0, 0); 215 } 216} 217