blob: 890909bbcaf958d80f26359ec01b8d009a5f9d55 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.view.animation;
18
19import android.graphics.Matrix;
20
Dianne Hackborn1d442e02009-04-20 18:14:05 -070021import java.io.PrintWriter;
22
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023/**
24 * Defines the transformation to be applied at
25 * one point in time of an Animation.
26 *
27 */
28public class Transformation {
29 /**
30 * Indicates a transformation that has no effect (alpha = 1 and identity matrix.)
31 */
Romain Guyfadd2082013-06-10 17:03:47 -070032 public static final int TYPE_IDENTITY = 0x0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 /**
34 * Indicates a transformation that applies an alpha only (uses an identity matrix.)
35 */
Romain Guyfadd2082013-06-10 17:03:47 -070036 public static final int TYPE_ALPHA = 0x1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 /**
38 * Indicates a transformation that applies a matrix only (alpha = 1.)
39 */
Romain Guyfadd2082013-06-10 17:03:47 -070040 public static final int TYPE_MATRIX = 0x2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 /**
42 * Indicates a transformation that applies an alpha and a matrix.
43 */
Romain Guyfadd2082013-06-10 17:03:47 -070044 public static final int TYPE_BOTH = TYPE_ALPHA | TYPE_MATRIX;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
46 protected Matrix mMatrix;
47 protected float mAlpha;
48 protected int mTransformationType;
49
50 /**
51 * Creates a new transformation with alpha = 1 and the identity matrix.
52 */
53 public Transformation() {
54 clear();
55 }
56
57 /**
58 * Reset the transformation to a state that leaves the object
59 * being animated in an unmodified state. The transformation type is
60 * {@link #TYPE_BOTH} by default.
61 */
62 public void clear() {
63 if (mMatrix == null) {
64 mMatrix = new Matrix();
65 } else {
66 mMatrix.reset();
67 }
68 mAlpha = 1.0f;
69 mTransformationType = TYPE_BOTH;
70 }
71
72 /**
73 * Indicates the nature of this transformation.
74 *
75 * @return {@link #TYPE_ALPHA}, {@link #TYPE_MATRIX},
76 * {@link #TYPE_BOTH} or {@link #TYPE_IDENTITY}.
77 */
78 public int getTransformationType() {
79 return mTransformationType;
80 }
81
82 /**
83 * Sets the transformation type.
84 *
85 * @param transformationType One of {@link #TYPE_ALPHA},
86 * {@link #TYPE_MATRIX}, {@link #TYPE_BOTH} or
87 * {@link #TYPE_IDENTITY}.
88 */
89 public void setTransformationType(int transformationType) {
90 mTransformationType = transformationType;
91 }
92
93 /**
94 * Clones the specified transformation.
95 *
96 * @param t The transformation to clone.
97 */
98 public void set(Transformation t) {
99 mAlpha = t.getAlpha();
100 mMatrix.set(t.getMatrix());
101 mTransformationType = t.getTransformationType();
102 }
103
104 /**
105 * Apply this Transformation to an existing Transformation, e.g. apply
106 * a scale effect to something that has already been rotated.
107 * @param t
108 */
109 public void compose(Transformation t) {
110 mAlpha *= t.getAlpha();
111 mMatrix.preConcat(t.getMatrix());
112 }
113
114 /**
Dianne Hackborn8078d8c2012-03-20 11:11:26 -0700115 * Like {@link #compose(Transformation)} but does this.postConcat(t) of
116 * the transformation matrix.
117 * @hide
118 */
119 public void postCompose(Transformation t) {
120 mAlpha *= t.getAlpha();
121 mMatrix.postConcat(t.getMatrix());
122 }
123
124 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 * @return The 3x3 Matrix representing the trnasformation to apply to the
126 * coordinates of the object being animated
127 */
128 public Matrix getMatrix() {
129 return mMatrix;
130 }
131
132 /**
133 * Sets the degree of transparency
134 * @param alpha 1.0 means fully opaqe and 0.0 means fully transparent
135 */
136 public void setAlpha(float alpha) {
137 mAlpha = alpha;
138 }
139
140 /**
141 * @return The degree of transparency
142 */
143 public float getAlpha() {
144 return mAlpha;
145 }
146
147 @Override
148 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700149 StringBuilder sb = new StringBuilder(64);
150 sb.append("Transformation");
151 toShortString(sb);
152 return sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 }
154
155 /**
156 * Return a string representation of the transformation in a compact form.
157 */
158 public String toShortString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700159 StringBuilder sb = new StringBuilder(64);
160 toShortString(sb);
161 return sb.toString();
162 }
163
164 /**
165 * @hide
166 */
167 public void toShortString(StringBuilder sb) {
168 sb.append("{alpha="); sb.append(mAlpha);
169 sb.append(" matrix="); mMatrix.toShortString(sb);
170 sb.append('}');
171 }
172
173 /**
174 * Print short string, to optimize dumping.
175 * @hide
176 */
177 public void printShortString(PrintWriter pw) {
178 pw.print("{alpha="); pw.print(mAlpha);
179 pw.print(" matrix=");
180 mMatrix.printShortString(pw);
181 pw.print('}');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 }
183}