blob: 1dd250f94f5d5de427ccf8f32ead33937dea4571 [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.content.Context;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080020import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.res.TypedArray;
22import android.util.AttributeSet;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080023import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25/**
26 * An animation that controls the scale of an object. You can specify the point
27 * to use for the center of scaling.
28 *
29 */
30public class ScaleAnimation extends Animation {
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080031 private final Resources mResources;
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 private float mFromX;
34 private float mToX;
35 private float mFromY;
36 private float mToY;
37
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080038 private int mFromXType = TypedValue.TYPE_NULL;
39 private int mToXType = TypedValue.TYPE_NULL;
40 private int mFromYType = TypedValue.TYPE_NULL;
41 private int mToYType = TypedValue.TYPE_NULL;
42
43 private int mFromXData = 0;
44 private int mToXData = 0;
45 private int mFromYData = 0;
46 private int mToYData = 0;
47
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 private int mPivotXType = ABSOLUTE;
49 private int mPivotYType = ABSOLUTE;
50 private float mPivotXValue = 0.0f;
51 private float mPivotYValue = 0.0f;
52
53 private float mPivotX;
54 private float mPivotY;
55
56 /**
The Android Open Source Projectbdbdc4f2009-03-03 21:00:54 -080057 * Constructor used when a ScaleAnimation is loaded from a resource.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 *
59 * @param context Application context to use
60 * @param attrs Attribute set from which to read values
61 */
62 public ScaleAnimation(Context context, AttributeSet attrs) {
63 super(context, attrs);
64
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080065 mResources = context.getResources();
66
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 TypedArray a = context.obtainStyledAttributes(attrs,
68 com.android.internal.R.styleable.ScaleAnimation);
69
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080070 TypedValue tv = a.peekValue(
71 com.android.internal.R.styleable.ScaleAnimation_fromXScale);
72 mFromX = 0.0f;
73 if (tv != null) {
74 if (tv.type == TypedValue.TYPE_FLOAT) {
75 // This is a scaling factor.
76 mFromX = tv.getFloat();
77 } else {
78 mFromXType = tv.type;
79 mFromXData = tv.data;
80 }
81 }
82 tv = a.peekValue(
83 com.android.internal.R.styleable.ScaleAnimation_toXScale);
84 mToX = 0.0f;
85 if (tv != null) {
86 if (tv.type == TypedValue.TYPE_FLOAT) {
87 // This is a scaling factor.
88 mToX = tv.getFloat();
89 } else {
90 mToXType = tv.type;
91 mToXData = tv.data;
92 }
93 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080095 tv = a.peekValue(
96 com.android.internal.R.styleable.ScaleAnimation_fromYScale);
97 mFromY = 0.0f;
98 if (tv != null) {
99 if (tv.type == TypedValue.TYPE_FLOAT) {
100 // This is a scaling factor.
101 mFromY = tv.getFloat();
102 } else {
103 mFromYType = tv.type;
104 mFromYData = tv.data;
105 }
106 }
107 tv = a.peekValue(
108 com.android.internal.R.styleable.ScaleAnimation_toYScale);
109 mToY = 0.0f;
110 if (tv != null) {
111 if (tv.type == TypedValue.TYPE_FLOAT) {
112 // This is a scaling factor.
113 mToY = tv.getFloat();
114 } else {
115 mToYType = tv.type;
116 mToYData = tv.data;
117 }
118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
120 Description d = Description.parseValue(a.peekValue(
121 com.android.internal.R.styleable.ScaleAnimation_pivotX));
122 mPivotXType = d.type;
123 mPivotXValue = d.value;
124
125 d = Description.parseValue(a.peekValue(
126 com.android.internal.R.styleable.ScaleAnimation_pivotY));
127 mPivotYType = d.type;
128 mPivotYValue = d.value;
129
130 a.recycle();
131 }
132
133 /**
134 * Constructor to use when building a ScaleAnimation from code
135 *
136 * @param fromX Horizontal scaling factor to apply at the start of the
137 * animation
138 * @param toX Horizontal scaling factor to apply at the end of the animation
139 * @param fromY Vertical scaling factor to apply at the start of the
140 * animation
141 * @param toY Vertical scaling factor to apply at the end of the animation
142 */
143 public ScaleAnimation(float fromX, float toX, float fromY, float toY) {
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800144 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 mFromX = fromX;
146 mToX = toX;
147 mFromY = fromY;
148 mToY = toY;
149 mPivotX = 0;
150 mPivotY = 0;
151 }
152
153 /**
154 * Constructor to use when building a ScaleAnimation from code
155 *
156 * @param fromX Horizontal scaling factor to apply at the start of the
157 * animation
158 * @param toX Horizontal scaling factor to apply at the end of the animation
159 * @param fromY Vertical scaling factor to apply at the start of the
160 * animation
161 * @param toY Vertical scaling factor to apply at the end of the animation
162 * @param pivotX The X coordinate of the point about which the object is
163 * being scaled, specified as an absolute number where 0 is the left
164 * edge. (This point remains fixed while the object changes size.)
165 * @param pivotY The Y coordinate of the point about which the object is
166 * being scaled, specified as an absolute number where 0 is the top
167 * edge. (This point remains fixed while the object changes size.)
168 */
169 public ScaleAnimation(float fromX, float toX, float fromY, float toY,
170 float pivotX, float pivotY) {
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800171 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 mFromX = fromX;
173 mToX = toX;
174 mFromY = fromY;
175 mToY = toY;
176
177 mPivotXType = ABSOLUTE;
178 mPivotYType = ABSOLUTE;
179 mPivotXValue = pivotX;
180 mPivotYValue = pivotY;
181 }
182
183 /**
184 * Constructor to use when building a ScaleAnimation from code
185 *
186 * @param fromX Horizontal scaling factor to apply at the start of the
187 * animation
188 * @param toX Horizontal scaling factor to apply at the end of the animation
189 * @param fromY Vertical scaling factor to apply at the start of the
190 * animation
191 * @param toY Vertical scaling factor to apply at the end of the animation
192 * @param pivotXType Specifies how pivotXValue should be interpreted. One of
193 * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
194 * Animation.RELATIVE_TO_PARENT.
195 * @param pivotXValue The X coordinate of the point about which the object
196 * is being scaled, specified as an absolute number where 0 is the
197 * left edge. (This point remains fixed while the object changes
198 * size.) This value can either be an absolute number if pivotXType
199 * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
200 * @param pivotYType Specifies how pivotYValue should be interpreted. One of
201 * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
202 * Animation.RELATIVE_TO_PARENT.
203 * @param pivotYValue The Y coordinate of the point about which the object
204 * is being scaled, specified as an absolute number where 0 is the
205 * top edge. (This point remains fixed while the object changes
206 * size.) This value can either be an absolute number if pivotYType
207 * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
208 */
209 public ScaleAnimation(float fromX, float toX, float fromY, float toY,
210 int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800211 mResources = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 mFromX = fromX;
213 mToX = toX;
214 mFromY = fromY;
215 mToY = toY;
216
217 mPivotXValue = pivotXValue;
218 mPivotXType = pivotXType;
219 mPivotYValue = pivotYValue;
220 mPivotYType = pivotYType;
221 }
222
223 @Override
224 protected void applyTransformation(float interpolatedTime, Transformation t) {
225 float sx = 1.0f;
226 float sy = 1.0f;
Chet Haase48460322010-06-11 14:22:25 -0700227 float scale = getScaleFactor();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
229 if (mFromX != 1.0f || mToX != 1.0f) {
230 sx = mFromX + ((mToX - mFromX) * interpolatedTime);
231 }
232 if (mFromY != 1.0f || mToY != 1.0f) {
233 sy = mFromY + ((mToY - mFromY) * interpolatedTime);
234 }
235
236 if (mPivotX == 0 && mPivotY == 0) {
237 t.getMatrix().setScale(sx, sy);
238 } else {
Chet Haase48460322010-06-11 14:22:25 -0700239 t.getMatrix().setScale(sx, sy, scale * mPivotX, scale * mPivotY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 }
241 }
242
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800243 float resolveScale(float scale, int type, int data, int size, int psize) {
244 float targetSize;
245 if (type == TypedValue.TYPE_FRACTION) {
246 targetSize = TypedValue.complexToFraction(data, size, psize);
247 } else if (type == TypedValue.TYPE_DIMENSION) {
248 targetSize = TypedValue.complexToDimension(data, mResources.getDisplayMetrics());
249 } else {
250 return scale;
251 }
252
253 if (size == 0) {
254 return 1;
255 }
256
257 return targetSize/(float)size;
258 }
259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 @Override
261 public void initialize(int width, int height, int parentWidth, int parentHeight) {
262 super.initialize(width, height, parentWidth, parentHeight);
263
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800264 mFromX = resolveScale(mFromX, mFromXType, mFromXData, width, parentWidth);
265 mToX = resolveScale(mToX, mToXType, mToXData, width, parentWidth);
266 mFromY = resolveScale(mFromY, mFromYType, mFromYData, height, parentHeight);
267 mToY = resolveScale(mToY, mToYType, mToYData, height, parentHeight);
268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 mPivotX = resolveSize(mPivotXType, mPivotXValue, width, parentWidth);
270 mPivotY = resolveSize(mPivotYType, mPivotYValue, height, parentHeight);
271 }
272}