TaskViewTransform.java revision 5b7dd536aa6cb8ce323b47cee109f879feb0d33a
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.RectF; 21import android.view.ViewPropertyAnimator; 22import android.view.animation.Interpolator; 23 24 25/* The transform state for a task view */ 26public class TaskViewTransform { 27 28 // TODO: Move this out of the transform 29 public int startDelay = 0; 30 31 public int translationX = 0; 32 public int translationY = 0; 33 public float translationZ = 0; 34 public float scale = 1f; 35 public float alpha = 1f; 36 37 // Clip and thumbnail scale are untransformed layout-space properties 38 // The bottom clip is only used for freeform workspace tasks 39 public int clipBottom = 0; 40 public int clipRight = 0; 41 public float thumbnailScale = 1f; 42 43 public boolean visible = false; 44 float p = 0f; 45 46 // This is a window-space rect that is purely used for coordinating the animation of an app 47 // window into Recents. 48 public RectF rect = new RectF(); 49 50 public TaskViewTransform() { 51 // Do nothing 52 } 53 54 /** 55 * Resets the current transform. 56 */ 57 public void reset() { 58 startDelay = 0; 59 translationX = 0; 60 translationY = 0; 61 translationZ = 0; 62 scale = 1f; 63 alpha = 1f; 64 clipBottom = 0; 65 clipRight = 0; 66 thumbnailScale = 1f; 67 visible = false; 68 rect.setEmpty(); 69 p = 0f; 70 } 71 72 /** Convenience functions to compare against current property values */ 73 public boolean hasAlphaChangedFrom(float v) { 74 return (Float.compare(alpha, v) != 0); 75 } 76 public boolean hasScaleChangedFrom(float v) { 77 return (Float.compare(scale, v) != 0); 78 } 79 public boolean hasTranslationXChangedFrom(float v) { 80 return (Float.compare(translationX, v) != 0); 81 } 82 public boolean hasTranslationYChangedFrom(float v) { 83 return (Float.compare(translationY, v) != 0); 84 } 85 public boolean hasTranslationZChangedFrom(float v) { 86 return (Float.compare(translationZ, v) != 0); 87 } 88 89 /** Applies this transform to a view. */ 90 public void applyToTaskView(TaskView v, int duration, Interpolator interp, boolean allowLayers, 91 boolean allowShadows, ValueAnimator.AnimatorUpdateListener updateCallback) { 92 // Check to see if any properties have changed, and update the task view 93 if (duration > 0) { 94 ViewPropertyAnimator anim = v.animate(); 95 boolean requiresLayers = false; 96 97 // Animate to the final state 98 if (hasTranslationXChangedFrom(v.getTranslationX())) { 99 anim.translationX(translationX); 100 } 101 if (hasTranslationYChangedFrom(v.getTranslationY())) { 102 anim.translationY(translationY); 103 } 104 if (allowShadows && hasTranslationZChangedFrom(v.getTranslationZ())) { 105 anim.translationZ(translationZ); 106 } 107 if (hasScaleChangedFrom(v.getScaleX())) { 108 anim.scaleX(scale) 109 .scaleY(scale); 110 requiresLayers = true; 111 } 112 if (hasAlphaChangedFrom(v.getAlpha())) { 113 // Use layers if we animate alpha 114 anim.alpha(alpha); 115 requiresLayers = true; 116 } 117 if (requiresLayers && allowLayers) { 118 anim.withLayer(); 119 } 120 if (updateCallback != null) { 121 anim.setUpdateListener(updateCallback); 122 } else { 123 anim.setUpdateListener(null); 124 } 125 anim.setListener(null); 126 anim.setStartDelay(startDelay) 127 .setDuration(duration) 128 .setInterpolator(interp) 129 .start(); 130 } else { 131 // Set the changed properties 132 if (hasTranslationXChangedFrom(v.getTranslationX())) { 133 v.setTranslationX(translationX); 134 } 135 if (hasTranslationYChangedFrom(v.getTranslationY())) { 136 v.setTranslationY(translationY); 137 } 138 if (allowShadows && hasTranslationZChangedFrom(v.getTranslationZ())) { 139 v.setTranslationZ(translationZ); 140 } 141 if (hasScaleChangedFrom(v.getScaleX())) { 142 v.setScaleX(scale); 143 v.setScaleY(scale); 144 } 145 if (hasAlphaChangedFrom(v.getAlpha())) { 146 v.setAlpha(alpha); 147 } 148 } 149 } 150 151 /** Reset the transform on a view. */ 152 public static void reset(TaskView v) { 153 // Cancel any running animations 154 v.animate().cancel(); 155 v.setTranslationX(0f); 156 v.setTranslationY(0f); 157 v.setTranslationZ(0f); 158 v.setScaleX(1f); 159 v.setScaleY(1f); 160 v.setAlpha(1f); 161 v.getViewBounds().setClipRight(0, false /* forceUpdate */); 162 v.getViewBounds().setClipBottom(0, false /* forceUpdate */); 163 v.mThumbnailView.setBitmapScale(1f); 164 } 165 166 @Override 167 public String toString() { 168 return "TaskViewTransform delay: " + startDelay + 169 " x: " + translationX + " y: " + translationY + " z: " + translationZ + 170 " scale: " + scale + " alpha: " + alpha + " visible: " + visible + " rect: " + rect + 171 " p: " + p; 172 } 173} 174