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.content.Context;
20import android.content.res.TypedArray;
21import android.util.AttributeSet;
22
23/**
24 * An animation that controls the scale of an object. You can specify the point
25 * to use for the center of scaling.
26 *
27 */
28public class ScaleAnimation extends Animation {
29    private float mFromX;
30    private float mToX;
31    private float mFromY;
32    private float mToY;
33
34    private int mPivotXType = ABSOLUTE;
35    private int mPivotYType = ABSOLUTE;
36    private float mPivotXValue = 0.0f;
37    private float mPivotYValue = 0.0f;
38
39    private float mPivotX;
40    private float mPivotY;
41
42    /**
43     * Constructor used when a ScaleAnimation is loaded from a resource.
44     *
45     * @param context Application context to use
46     * @param attrs Attribute set from which to read values
47     */
48    public ScaleAnimation(Context context, AttributeSet attrs) {
49        super(context, attrs);
50
51        TypedArray a = context.obtainStyledAttributes(attrs,
52                com.android.internal.R.styleable.ScaleAnimation);
53
54        mFromX = a.getFloat(com.android.internal.R.styleable.ScaleAnimation_fromXScale, 0.0f);
55        mToX = a.getFloat(com.android.internal.R.styleable.ScaleAnimation_toXScale, 0.0f);
56
57        mFromY = a.getFloat(com.android.internal.R.styleable.ScaleAnimation_fromYScale, 0.0f);
58        mToY = a.getFloat(com.android.internal.R.styleable.ScaleAnimation_toYScale, 0.0f);
59
60        Description d = Description.parseValue(a.peekValue(
61                com.android.internal.R.styleable.ScaleAnimation_pivotX));
62        mPivotXType = d.type;
63        mPivotXValue = d.value;
64
65        d = Description.parseValue(a.peekValue(
66            com.android.internal.R.styleable.ScaleAnimation_pivotY));
67        mPivotYType = d.type;
68        mPivotYValue = d.value;
69
70        a.recycle();
71    }
72
73    /**
74     * Constructor to use when building a ScaleAnimation from code
75     *
76     * @param fromX Horizontal scaling factor to apply at the start of the
77     *        animation
78     * @param toX Horizontal scaling factor to apply at the end of the animation
79     * @param fromY Vertical scaling factor to apply at the start of the
80     *        animation
81     * @param toY Vertical scaling factor to apply at the end of the animation
82     */
83    public ScaleAnimation(float fromX, float toX, float fromY, float toY) {
84        mFromX = fromX;
85        mToX = toX;
86        mFromY = fromY;
87        mToY = toY;
88        mPivotX = 0;
89        mPivotY = 0;
90    }
91
92    /**
93     * Constructor to use when building a ScaleAnimation from code
94     *
95     * @param fromX Horizontal scaling factor to apply at the start of the
96     *        animation
97     * @param toX Horizontal scaling factor to apply at the end of the animation
98     * @param fromY Vertical scaling factor to apply at the start of the
99     *        animation
100     * @param toY Vertical scaling factor to apply at the end of the animation
101     * @param pivotX The X coordinate of the point about which the object is
102     *        being scaled, specified as an absolute number where 0 is the left
103     *        edge. (This point remains fixed while the object changes size.)
104     * @param pivotY The Y coordinate of the point about which the object is
105     *        being scaled, specified as an absolute number where 0 is the top
106     *        edge. (This point remains fixed while the object changes size.)
107     */
108    public ScaleAnimation(float fromX, float toX, float fromY, float toY,
109            float pivotX, float pivotY) {
110        mFromX = fromX;
111        mToX = toX;
112        mFromY = fromY;
113        mToY = toY;
114
115        mPivotXType = ABSOLUTE;
116        mPivotYType = ABSOLUTE;
117        mPivotXValue = pivotX;
118        mPivotYValue = pivotY;
119    }
120
121    /**
122     * Constructor to use when building a ScaleAnimation from code
123     *
124     * @param fromX Horizontal scaling factor to apply at the start of the
125     *        animation
126     * @param toX Horizontal scaling factor to apply at the end of the animation
127     * @param fromY Vertical scaling factor to apply at the start of the
128     *        animation
129     * @param toY Vertical scaling factor to apply at the end of the animation
130     * @param pivotXType Specifies how pivotXValue should be interpreted. One of
131     *        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
132     *        Animation.RELATIVE_TO_PARENT.
133     * @param pivotXValue The X coordinate of the point about which the object
134     *        is being scaled, specified as an absolute number where 0 is the
135     *        left edge. (This point remains fixed while the object changes
136     *        size.) This value can either be an absolute number if pivotXType
137     *        is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
138     * @param pivotYType Specifies how pivotYValue should be interpreted. One of
139     *        Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
140     *        Animation.RELATIVE_TO_PARENT.
141     * @param pivotYValue The Y coordinate of the point about which the object
142     *        is being scaled, specified as an absolute number where 0 is the
143     *        top edge. (This point remains fixed while the object changes
144     *        size.) This value can either be an absolute number if pivotYType
145     *        is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
146     */
147    public ScaleAnimation(float fromX, float toX, float fromY, float toY,
148            int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {
149        mFromX = fromX;
150        mToX = toX;
151        mFromY = fromY;
152        mToY = toY;
153
154        mPivotXValue = pivotXValue;
155        mPivotXType = pivotXType;
156        mPivotYValue = pivotYValue;
157        mPivotYType = pivotYType;
158    }
159
160    @Override
161    protected void applyTransformation(float interpolatedTime, Transformation t) {
162        float sx = 1.0f;
163        float sy = 1.0f;
164
165        if (mFromX != 1.0f || mToX != 1.0f) {
166            sx = mFromX + ((mToX - mFromX) * interpolatedTime);
167        }
168        if (mFromY != 1.0f || mToY != 1.0f) {
169            sy = mFromY + ((mToY - mFromY) * interpolatedTime);
170        }
171
172        if (mPivotX == 0 && mPivotY == 0) {
173            t.getMatrix().setScale(sx, sy);
174        } else {
175            t.getMatrix().setScale(sx, sy, mPivotX, mPivotY);
176        }
177    }
178
179    @Override
180    public void initialize(int width, int height, int parentWidth, int parentHeight) {
181        super.initialize(width, height, parentWidth, parentHeight);
182
183        mPivotX = resolveSize(mPivotXType, mPivotXValue, width, parentWidth);
184        mPivotY = resolveSize(mPivotYType, mPivotYValue, height, parentHeight);
185    }
186}
187