| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class Transformation { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public static final int TYPE_IDENTITY = 0x0; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public static final int TYPE_ALPHA = 0x1; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public static final int TYPE_MATRIX = 0x2; |
| 45 |
|
| 46 |
|
| 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 |
|
| 61 |
|
| 62 |
public Transformation() { |
| 63 |
clear(); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 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 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public int getTransformationType() { |
| 90 |
return mTransformationType; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public void setTransformationType(int transformationType) { |
| 101 |
mTransformationType = transformationType; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 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 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 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 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 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 |
|
| 162 |
|
| 163 |
|
| 164 |
public Matrix getMatrix() { |
| 165 |
return mMatrix; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public void setAlpha(@FloatRange(from=0.0, to=1.0) float alpha) { |
| 173 |
mAlpha = alpha; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public float getAlpha() { |
| 180 |
return mAlpha; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public void setClipRect(Rect r) { |
| 188 |
setClipRect(r.left, r.top, r.right, r.bottom); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 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 |
|
| 202 |
|
| 203 |
|
| 204 |
public Rect getClipRect() { |
| 205 |
return mClipRect; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public boolean hasClipRect() { |
| 213 |
return mHasClipRect; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public void setInsets(Insets insets) { |
| 221 |
mInsets = insets; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public void setInsets(int left, int top, int right, int bottom) { |
| 229 |
mInsets = Insets.of(left, top, right, bottom); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public Insets getInsets() { |
| 237 |
return mInsets; |
| 238 |
} |
| 239 |
|
| 240 |
@Override |
| 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 |
|
| 250 |
|
| 251 |
public String toShortString() { |
| 252 |
StringBuilder sb = new StringBuilder(64); |
| 253 |
toShortString(sb); |
| 254 |
return sb.toString(); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 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 |
|
| 268 |
|
| 269 |
|
| 270 |
@UnsupportedAppUsage |
| 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 |
} |