Transformation.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
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
17package android.view.animation;
18
19import android.graphics.Matrix;
20
21/**
22 * Defines the transformation to be applied at
23 * one point in time of an Animation.
24 *
25 */
26public class Transformation {
27    /**
28     * Indicates a transformation that has no effect (alpha = 1 and identity matrix.)
29     */
30    public static int TYPE_IDENTITY = 0x0;
31    /**
32     * Indicates a transformation that applies an alpha only (uses an identity matrix.)
33     */
34    public static int TYPE_ALPHA = 0x1;
35    /**
36     * Indicates a transformation that applies a matrix only (alpha = 1.)
37     */
38    public static int TYPE_MATRIX = 0x2;
39    /**
40     * Indicates a transformation that applies an alpha and a matrix.
41     */
42    public static int TYPE_BOTH = TYPE_ALPHA | TYPE_MATRIX;
43
44    protected Matrix mMatrix;
45    protected float mAlpha;
46    protected int mTransformationType;
47
48    /**
49     * Creates a new transformation with alpha = 1 and the identity matrix.
50     */
51    public Transformation() {
52        clear();
53    }
54
55    /**
56     * Reset the transformation to a state that leaves the object
57     * being animated in an unmodified state. The transformation type is
58     * {@link #TYPE_BOTH} by default.
59     */
60    public void clear() {
61        if (mMatrix == null) {
62            mMatrix = new Matrix();
63        } else {
64            mMatrix.reset();
65        }
66        mAlpha = 1.0f;
67        mTransformationType = TYPE_BOTH;
68    }
69
70    /**
71     * Indicates the nature of this transformation.
72     *
73     * @return {@link #TYPE_ALPHA}, {@link #TYPE_MATRIX},
74     *         {@link #TYPE_BOTH} or {@link #TYPE_IDENTITY}.
75     */
76    public int getTransformationType() {
77        return mTransformationType;
78    }
79
80    /**
81     * Sets the transformation type.
82     *
83     * @param transformationType One of {@link #TYPE_ALPHA},
84     *        {@link #TYPE_MATRIX}, {@link #TYPE_BOTH} or
85     *        {@link #TYPE_IDENTITY}.
86     */
87    public void setTransformationType(int transformationType) {
88        mTransformationType = transformationType;
89    }
90
91    /**
92     * Clones the specified transformation.
93     *
94     * @param t The transformation to clone.
95     */
96    public void set(Transformation t) {
97        mAlpha = t.getAlpha();
98        mMatrix.set(t.getMatrix());
99        mTransformationType = t.getTransformationType();
100    }
101
102    /**
103     * Apply this Transformation to an existing Transformation, e.g. apply
104     * a scale effect to something that has already been rotated.
105     * @param t
106     */
107    public void compose(Transformation t) {
108        mAlpha *= t.getAlpha();
109        mMatrix.preConcat(t.getMatrix());
110    }
111
112    /**
113     * @return The 3x3 Matrix representing the trnasformation to apply to the
114     * coordinates of the object being animated
115     */
116    public Matrix getMatrix() {
117        return mMatrix;
118    }
119
120    /**
121     * Sets the degree of transparency
122     * @param alpha 1.0 means fully opaqe and 0.0 means fully transparent
123     */
124    public void setAlpha(float alpha) {
125        mAlpha = alpha;
126    }
127
128    /**
129     * @return The degree of transparency
130     */
131    public float getAlpha() {
132        return mAlpha;
133    }
134
135    @Override
136    public String toString() {
137        return "Transformation{alpha=" + mAlpha + " matrix="
138                + mMatrix.toShortString() + "}";
139    }
140
141    /**
142     * Return a string representation of the transformation in a compact form.
143     */
144    public String toShortString() {
145        return "{alpha=" + mAlpha + " matrix=" + mMatrix.toShortString() + "}";
146    }
147}
148