The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package android.view.animation; |
| 18 | |
| 19 | import android.content.Context; |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 20 | import android.content.res.Resources; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.res.TypedArray; |
| 22 | import android.util.AttributeSet; |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 23 | import android.util.TypedValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 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 | */ |
| 30 | public class ScaleAnimation extends Animation { |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 31 | private final Resources mResources; |
| 32 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | private float mFromX; |
| 34 | private float mToX; |
| 35 | private float mFromY; |
| 36 | private float mToY; |
| 37 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 38 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | 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 Project | bdbdc4f | 2009-03-03 21:00:54 -0800 | [diff] [blame] | 57 | * Constructor used when a ScaleAnimation is loaded from a resource. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | * |
| 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 Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 65 | mResources = context.getResources(); |
| 66 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 68 | com.android.internal.R.styleable.ScaleAnimation); |
| 69 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 70 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 95 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | |
| 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 Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 144 | mResources = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | 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 Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 171 | mResources = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | 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 Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 211 | mResources = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | 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 Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 227 | float scale = getScaleFactor(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | |
| 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 Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 239 | t.getMatrix().setScale(sx, sy, scale * mPivotX, scale * mPivotY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 243 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | @Override |
| 261 | public void initialize(int width, int height, int parentWidth, int parentHeight) { |
| 262 | super.initialize(width, height, parentWidth, parentHeight); |
| 263 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 264 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | mPivotX = resolveSize(mPivotXType, mPivotXValue, width, parentWidth); |
| 270 | mPivotY = resolveSize(mPivotYType, mPivotYValue, height, parentHeight); |
| 271 | } |
| 272 | } |