1 /* 2 * Copyright (C) 2006 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 17 package android.view.animation; 18 19 import android.annotation.FloatRange; 20 import android.compat.annotation.UnsupportedAppUsage; 21 import android.graphics.Insets; 22 import android.graphics.Matrix; 23 import android.graphics.Rect; 24 25 import java.io.PrintWriter; 26 27 /** 28 * Defines the transformation to be applied at 29 * one point in time of an Animation. 30 * 31 */ 32 public class Transformation { 33 /** 34 * Indicates a transformation that has no effect (alpha = 1 and identity matrix.) 35 */ 36 public static final int TYPE_IDENTITY = 0x0; 37 /** 38 * Indicates a transformation that applies an alpha only (uses an identity matrix.) 39 */ 40 public static final int TYPE_ALPHA = 0x1; 41 /** 42 * Indicates a transformation that applies a matrix only (alpha = 1.) 43 */ 44 public static final int TYPE_MATRIX = 0x2; 45 /** 46 * Indicates a transformation that applies an alpha and a matrix. 47 */ 48 public static final int TYPE_BOTH = TYPE_ALPHA | TYPE_MATRIX; 49 50 protected Matrix mMatrix; 51 protected float mAlpha; 52 protected int mTransformationType; 53 54 private boolean mHasClipRect; 55 private Rect mClipRect = new Rect(); 56 57 private Insets mInsets = Insets.NONE; 58 59 /** 60 * Creates a new transformation with alpha = 1 and the identity matrix. 61 */ Transformation()62 public Transformation() { 63 clear(); 64 } 65 66 /** 67 * Reset the transformation to a state that leaves the object 68 * being animated in an unmodified state. The transformation type is 69 * {@link #TYPE_BOTH} by default. 70 */ clear()71 public void clear() { 72 if (mMatrix == null) { 73 mMatrix = new Matrix(); 74 } else { 75 mMatrix.reset(); 76 } 77 mClipRect.setEmpty(); 78 mHasClipRect = false; 79 mAlpha = 1.0f; 80 mTransformationType = TYPE_BOTH; 81 } 82 83 /** 84 * Indicates the nature of this transformation. 85 * 86 * @return {@link #TYPE_ALPHA}, {@link #TYPE_MATRIX}, 87 * {@link #TYPE_BOTH} or {@link #TYPE_IDENTITY}. 88 */ getTransformationType()89 public int getTransformationType() { 90 return mTransformationType; 91 } 92 93 /** 94 * Sets the transformation type. 95 * 96 * @param transformationType One of {@link #TYPE_ALPHA}, 97 * {@link #TYPE_MATRIX}, {@link #TYPE_BOTH} or 98 * {@link #TYPE_IDENTITY}. 99 */ setTransformationType(int transformationType)100 public void setTransformationType(int transformationType) { 101 mTransformationType = transformationType; 102 } 103 104 /** 105 * Clones the specified transformation. 106 * 107 * @param t The transformation to clone. 108 */ set(Transformation t)109 public void set(Transformation t) { 110 mAlpha = t.getAlpha(); 111 mMatrix.set(t.getMatrix()); 112 if (t.mHasClipRect) { 113 setClipRect(t.getClipRect()); 114 } else { 115 mHasClipRect = false; 116 mClipRect.setEmpty(); 117 } 118 mTransformationType = t.getTransformationType(); 119 } 120 121 /** 122 * Apply this Transformation to an existing Transformation, e.g. apply 123 * a scale effect to something that has already been rotated. 124 * @param t 125 */ compose(Transformation t)126 public void compose(Transformation t) { 127 mAlpha *= t.getAlpha(); 128 mMatrix.preConcat(t.getMatrix()); 129 if (t.mHasClipRect) { 130 Rect bounds = t.getClipRect(); 131 if (mHasClipRect) { 132 setClipRect(mClipRect.left + bounds.left, mClipRect.top + bounds.top, 133 mClipRect.right + bounds.right, mClipRect.bottom + bounds.bottom); 134 } else { 135 setClipRect(bounds); 136 } 137 } 138 setInsets(Insets.add(getInsets(), t.getInsets())); 139 } 140 141 /** 142 * Like {@link #compose(Transformation)} but does this.postConcat(t) of 143 * the transformation matrix. 144 * @hide 145 */ postCompose(Transformation t)146 public void postCompose(Transformation t) { 147 mAlpha *= t.getAlpha(); 148 mMatrix.postConcat(t.getMatrix()); 149 if (t.mHasClipRect) { 150 Rect bounds = t.getClipRect(); 151 if (mHasClipRect) { 152 setClipRect(mClipRect.left + bounds.left, mClipRect.top + bounds.top, 153 mClipRect.right + bounds.right, mClipRect.bottom + bounds.bottom); 154 } else { 155 setClipRect(bounds); 156 } 157 } 158 } 159 160 /** 161 * @return The 3x3 Matrix representing the transformation to apply to the 162 * coordinates of the object being animated 163 */ getMatrix()164 public Matrix getMatrix() { 165 return mMatrix; 166 } 167 168 /** 169 * Sets the degree of transparency 170 * @param alpha 1.0 means fully opaque and 0.0 means fully transparent 171 */ setAlpha(@loatRangefrom=0.0, to=1.0) float alpha)172 public void setAlpha(@FloatRange(from=0.0, to=1.0) float alpha) { 173 mAlpha = alpha; 174 } 175 176 /** 177 * @return The degree of transparency 178 */ getAlpha()179 public float getAlpha() { 180 return mAlpha; 181 } 182 183 /** 184 * Sets the current Transform's clip rect 185 * @hide 186 */ setClipRect(Rect r)187 public void setClipRect(Rect r) { 188 setClipRect(r.left, r.top, r.right, r.bottom); 189 } 190 191 /** 192 * Sets the current Transform's clip rect 193 * @hide 194 */ setClipRect(int l, int t, int r, int b)195 public void setClipRect(int l, int t, int r, int b) { 196 mClipRect.set(l, t, r, b); 197 mHasClipRect = true; 198 } 199 200 /** 201 * Returns the current Transform's clip rect 202 * @hide 203 */ getClipRect()204 public Rect getClipRect() { 205 return mClipRect; 206 } 207 208 /** 209 * Returns whether the current Transform's clip rect is set 210 * @hide 211 */ hasClipRect()212 public boolean hasClipRect() { 213 return mHasClipRect; 214 } 215 216 /** 217 * Sets the current Transform's insets 218 * @hide 219 */ setInsets(Insets insets)220 public void setInsets(Insets insets) { 221 mInsets = insets; 222 } 223 224 /** 225 * Sets the current Transform's insets 226 * @hide 227 */ setInsets(int left, int top, int right, int bottom)228 public void setInsets(int left, int top, int right, int bottom) { 229 mInsets = Insets.of(left, top, right, bottom); 230 } 231 232 /** 233 * Returns the current Transform's outset rect 234 * @hide 235 */ getInsets()236 public Insets getInsets() { 237 return mInsets; 238 } 239 240 @Override toString()241 public String toString() { 242 StringBuilder sb = new StringBuilder(64); 243 sb.append("Transformation"); 244 toShortString(sb); 245 return sb.toString(); 246 } 247 248 /** 249 * Return a string representation of the transformation in a compact form. 250 */ toShortString()251 public String toShortString() { 252 StringBuilder sb = new StringBuilder(64); 253 toShortString(sb); 254 return sb.toString(); 255 } 256 257 /** 258 * @hide 259 */ toShortString(StringBuilder sb)260 public void toShortString(StringBuilder sb) { 261 sb.append("{alpha="); sb.append(mAlpha); 262 sb.append(" matrix="); sb.append(mMatrix.toShortString()); 263 sb.append('}'); 264 } 265 266 /** 267 * Print short string, to optimize dumping. 268 * @hide 269 */ 270 @UnsupportedAppUsage printShortString(PrintWriter pw)271 public void printShortString(PrintWriter pw) { 272 pw.print("{alpha="); pw.print(mAlpha); 273 pw.print(" matrix="); 274 mMatrix.dump(pw); 275 pw.print('}'); 276 } 277 } 278