Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | package android.animation; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.content.res.Resources; |
| 20 | import android.content.res.TypedArray; |
| 21 | import android.content.res.XmlResourceParser; |
| 22 | import android.content.res.Resources.NotFoundException; |
| 23 | import android.util.AttributeSet; |
Chet Haase | 5bed88e1 | 2010-11-30 14:43:33 -0800 | [diff] [blame] | 24 | import android.util.TypedValue; |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 25 | import android.util.Xml; |
| 26 | import android.view.animation.AnimationUtils; |
| 27 | import org.xmlpull.v1.XmlPullParser; |
| 28 | import org.xmlpull.v1.XmlPullParserException; |
| 29 | |
| 30 | import java.io.IOException; |
| 31 | import java.util.ArrayList; |
| 32 | |
| 33 | /** |
Chet Haase | 6cfdf45 | 2011-04-22 16:42:10 -0700 | [diff] [blame] | 34 | * This class is used to instantiate animator XML files into Animator objects. |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 35 | * <p> |
Chet Haase | 6cfdf45 | 2011-04-22 16:42:10 -0700 | [diff] [blame] | 36 | * For performance reasons, inflation relies heavily on pre-processing of |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 37 | * XML files that is done at build time. Therefore, it is not currently possible |
Chet Haase | 6cfdf45 | 2011-04-22 16:42:10 -0700 | [diff] [blame] | 38 | * to use this inflater with an XmlPullParser over a plain XML file at runtime; |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 39 | * it only works with an XmlPullParser returned from a compiled resource (R. |
| 40 | * <em>something</em> file.) |
| 41 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 42 | public class AnimatorInflater { |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 45 | * These flags are used when parsing AnimatorSet objects |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 46 | */ |
| 47 | private static final int TOGETHER = 0; |
| 48 | private static final int SEQUENTIALLY = 1; |
| 49 | |
| 50 | /** |
| 51 | * Enum values used in XML attributes to indicate the value for mValueType |
| 52 | */ |
| 53 | private static final int VALUE_TYPE_FLOAT = 0; |
| 54 | private static final int VALUE_TYPE_INT = 1; |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 55 | private static final int VALUE_TYPE_COLOR = 4; |
| 56 | private static final int VALUE_TYPE_CUSTOM = 5; |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 59 | * Loads an {@link Animator} object from a resource |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 60 | * |
| 61 | * @param context Application context used to access resources |
| 62 | * @param id The resource id of the animation to load |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 63 | * @return The animator object reference by the specified id |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 64 | * @throws android.content.res.Resources.NotFoundException when the animation cannot be loaded |
| 65 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 66 | public static Animator loadAnimator(Context context, int id) |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 67 | throws NotFoundException { |
| 68 | |
| 69 | XmlResourceParser parser = null; |
| 70 | try { |
| 71 | parser = context.getResources().getAnimation(id); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 72 | return createAnimatorFromXml(context, parser); |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 73 | } catch (XmlPullParserException ex) { |
| 74 | Resources.NotFoundException rnf = |
| 75 | new Resources.NotFoundException("Can't load animation resource ID #0x" + |
| 76 | Integer.toHexString(id)); |
| 77 | rnf.initCause(ex); |
| 78 | throw rnf; |
| 79 | } catch (IOException ex) { |
| 80 | Resources.NotFoundException rnf = |
| 81 | new Resources.NotFoundException("Can't load animation resource ID #0x" + |
| 82 | Integer.toHexString(id)); |
| 83 | rnf.initCause(ex); |
| 84 | throw rnf; |
| 85 | } finally { |
| 86 | if (parser != null) parser.close(); |
| 87 | } |
| 88 | } |
| 89 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 90 | private static Animator createAnimatorFromXml(Context c, XmlPullParser parser) |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 91 | throws XmlPullParserException, IOException { |
| 92 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 93 | return createAnimatorFromXml(c, parser, Xml.asAttributeSet(parser), null, 0); |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 96 | private static Animator createAnimatorFromXml(Context c, XmlPullParser parser, |
| 97 | AttributeSet attrs, AnimatorSet parent, int sequenceOrdering) |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 98 | throws XmlPullParserException, IOException { |
| 99 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 100 | Animator anim = null; |
| 101 | ArrayList<Animator> childAnims = null; |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 102 | |
| 103 | // Make sure we are on a start tag. |
| 104 | int type; |
| 105 | int depth = parser.getDepth(); |
| 106 | |
| 107 | while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) |
| 108 | && type != XmlPullParser.END_DOCUMENT) { |
| 109 | |
| 110 | if (type != XmlPullParser.START_TAG) { |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | String name = parser.getName(); |
| 115 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 116 | if (name.equals("objectAnimator")) { |
| 117 | anim = loadObjectAnimator(c, attrs); |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 118 | } else if (name.equals("animator")) { |
| 119 | anim = loadAnimator(c, attrs, null); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 120 | } else if (name.equals("set")) { |
| 121 | anim = new AnimatorSet(); |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 122 | TypedArray a = c.obtainStyledAttributes(attrs, |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 123 | com.android.internal.R.styleable.AnimatorSet); |
| 124 | int ordering = a.getInt(com.android.internal.R.styleable.AnimatorSet_ordering, |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 125 | TOGETHER); |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 126 | createAnimatorFromXml(c, parser, attrs, (AnimatorSet) anim, ordering); |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 127 | a.recycle(); |
| 128 | } else { |
| 129 | throw new RuntimeException("Unknown animator name: " + parser.getName()); |
| 130 | } |
| 131 | |
| 132 | if (parent != null) { |
| 133 | if (childAnims == null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 134 | childAnims = new ArrayList<Animator>(); |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 135 | } |
| 136 | childAnims.add(anim); |
| 137 | } |
| 138 | } |
| 139 | if (parent != null && childAnims != null) { |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 140 | Animator[] animsArray = new Animator[childAnims.size()]; |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 141 | int index = 0; |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 142 | for (Animator a : childAnims) { |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 143 | animsArray[index++] = a; |
| 144 | } |
| 145 | if (sequenceOrdering == TOGETHER) { |
| 146 | parent.playTogether(animsArray); |
| 147 | } else { |
| 148 | parent.playSequentially(animsArray); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return anim; |
| 153 | |
| 154 | } |
| 155 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 156 | private static ObjectAnimator loadObjectAnimator(Context context, AttributeSet attrs) |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 157 | throws NotFoundException { |
| 158 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 159 | ObjectAnimator anim = new ObjectAnimator(); |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 160 | |
| 161 | loadAnimator(context, attrs, anim); |
| 162 | |
| 163 | TypedArray a = |
| 164 | context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.PropertyAnimator); |
| 165 | |
| 166 | String propertyName = a.getString(com.android.internal.R.styleable.PropertyAnimator_propertyName); |
| 167 | |
| 168 | anim.setPropertyName(propertyName); |
| 169 | |
| 170 | a.recycle(); |
| 171 | |
| 172 | return anim; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Creates a new animation whose parameters come from the specified context and |
| 177 | * attributes set. |
| 178 | * |
| 179 | * @param context the application environment |
| 180 | * @param attrs the set of attributes holding the animation parameters |
| 181 | */ |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 182 | private static ValueAnimator loadAnimator(Context context, AttributeSet attrs, ValueAnimator anim) |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 183 | throws NotFoundException { |
| 184 | |
| 185 | TypedArray a = |
| 186 | context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Animator); |
| 187 | |
Chet Haase | d9e4a5c | 2013-03-15 14:33:24 -0700 | [diff] [blame] | 188 | long duration = a.getInt(com.android.internal.R.styleable.Animator_duration, 300); |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 189 | |
| 190 | long startDelay = a.getInt(com.android.internal.R.styleable.Animator_startOffset, 0); |
| 191 | |
| 192 | int valueType = a.getInt(com.android.internal.R.styleable.Animator_valueType, |
| 193 | VALUE_TYPE_FLOAT); |
| 194 | |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 195 | if (anim == null) { |
| 196 | anim = new ValueAnimator(); |
| 197 | } |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 198 | TypeEvaluator evaluator = null; |
| 199 | |
Chet Haase | 5bed88e1 | 2010-11-30 14:43:33 -0800 | [diff] [blame] | 200 | int valueFromIndex = com.android.internal.R.styleable.Animator_valueFrom; |
| 201 | int valueToIndex = com.android.internal.R.styleable.Animator_valueTo; |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 202 | |
Chet Haase | 5bed88e1 | 2010-11-30 14:43:33 -0800 | [diff] [blame] | 203 | boolean getFloats = (valueType == VALUE_TYPE_FLOAT); |
| 204 | |
| 205 | TypedValue tvFrom = a.peekValue(valueFromIndex); |
| 206 | boolean hasFrom = (tvFrom != null); |
| 207 | int fromType = hasFrom ? tvFrom.type : 0; |
| 208 | TypedValue tvTo = a.peekValue(valueToIndex); |
| 209 | boolean hasTo = (tvTo != null); |
| 210 | int toType = hasTo ? tvTo.type : 0; |
| 211 | |
| 212 | if ((hasFrom && (fromType >= TypedValue.TYPE_FIRST_COLOR_INT) && |
| 213 | (fromType <= TypedValue.TYPE_LAST_COLOR_INT)) || |
| 214 | (hasTo && (toType >= TypedValue.TYPE_FIRST_COLOR_INT) && |
| 215 | (toType <= TypedValue.TYPE_LAST_COLOR_INT))) { |
| 216 | // special case for colors: ignore valueType and get ints |
| 217 | getFloats = false; |
George Mount | 1ffb280 | 2013-10-09 16:13:54 -0700 | [diff] [blame] | 218 | evaluator = ArgbEvaluator.getInstance(); |
Chet Haase | 5bed88e1 | 2010-11-30 14:43:33 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | if (getFloats) { |
| 222 | float valueFrom; |
| 223 | float valueTo; |
| 224 | if (hasFrom) { |
| 225 | if (fromType == TypedValue.TYPE_DIMENSION) { |
| 226 | valueFrom = a.getDimension(valueFromIndex, 0f); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 227 | } else { |
Chet Haase | 5bed88e1 | 2010-11-30 14:43:33 -0800 | [diff] [blame] | 228 | valueFrom = a.getFloat(valueFromIndex, 0f); |
Romain Guy | 83d6e82 | 2010-10-14 10:13:53 -0700 | [diff] [blame] | 229 | } |
Chet Haase | 5bed88e1 | 2010-11-30 14:43:33 -0800 | [diff] [blame] | 230 | if (hasTo) { |
| 231 | if (toType == TypedValue.TYPE_DIMENSION) { |
| 232 | valueTo = a.getDimension(valueToIndex, 0f); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 233 | } else { |
Chet Haase | 5bed88e1 | 2010-11-30 14:43:33 -0800 | [diff] [blame] | 234 | valueTo = a.getFloat(valueToIndex, 0f); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 235 | } |
Chet Haase | 5bed88e1 | 2010-11-30 14:43:33 -0800 | [diff] [blame] | 236 | anim.setFloatValues(valueFrom, valueTo); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 237 | } else { |
Chet Haase | 5bed88e1 | 2010-11-30 14:43:33 -0800 | [diff] [blame] | 238 | anim.setFloatValues(valueFrom); |
| 239 | } |
| 240 | } else { |
| 241 | if (toType == TypedValue.TYPE_DIMENSION) { |
| 242 | valueTo = a.getDimension(valueToIndex, 0f); |
| 243 | } else { |
| 244 | valueTo = a.getFloat(valueToIndex, 0f); |
| 245 | } |
| 246 | anim.setFloatValues(valueTo); |
| 247 | } |
| 248 | } else { |
| 249 | int valueFrom; |
| 250 | int valueTo; |
| 251 | if (hasFrom) { |
| 252 | if (fromType == TypedValue.TYPE_DIMENSION) { |
| 253 | valueFrom = (int) a.getDimension(valueFromIndex, 0f); |
| 254 | } else if ((fromType >= TypedValue.TYPE_FIRST_COLOR_INT) && |
| 255 | (fromType <= TypedValue.TYPE_LAST_COLOR_INT)) { |
| 256 | valueFrom = a.getColor(valueFromIndex, 0); |
| 257 | } else { |
| 258 | valueFrom = a.getInt(valueFromIndex, 0); |
| 259 | } |
| 260 | if (hasTo) { |
| 261 | if (toType == TypedValue.TYPE_DIMENSION) { |
| 262 | valueTo = (int) a.getDimension(valueToIndex, 0f); |
| 263 | } else if ((toType >= TypedValue.TYPE_FIRST_COLOR_INT) && |
| 264 | (toType <= TypedValue.TYPE_LAST_COLOR_INT)) { |
| 265 | valueTo = a.getColor(valueToIndex, 0); |
| 266 | } else { |
| 267 | valueTo = a.getInt(valueToIndex, 0); |
| 268 | } |
| 269 | anim.setIntValues(valueFrom, valueTo); |
| 270 | } else { |
| 271 | anim.setIntValues(valueFrom); |
| 272 | } |
| 273 | } else { |
| 274 | if (hasTo) { |
| 275 | if (toType == TypedValue.TYPE_DIMENSION) { |
| 276 | valueTo = (int) a.getDimension(valueToIndex, 0f); |
| 277 | } else if ((toType >= TypedValue.TYPE_FIRST_COLOR_INT) && |
| 278 | (toType <= TypedValue.TYPE_LAST_COLOR_INT)) { |
| 279 | valueTo = a.getColor(valueToIndex, 0); |
| 280 | } else { |
| 281 | valueTo = a.getInt(valueToIndex, 0); |
| 282 | } |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 283 | anim.setIntValues(valueTo); |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 284 | } |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 285 | } |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 288 | anim.setDuration(duration); |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 289 | anim.setStartDelay(startDelay); |
| 290 | |
| 291 | if (a.hasValue(com.android.internal.R.styleable.Animator_repeatCount)) { |
| 292 | anim.setRepeatCount( |
| 293 | a.getInt(com.android.internal.R.styleable.Animator_repeatCount, 0)); |
| 294 | } |
| 295 | if (a.hasValue(com.android.internal.R.styleable.Animator_repeatMode)) { |
| 296 | anim.setRepeatMode( |
| 297 | a.getInt(com.android.internal.R.styleable.Animator_repeatMode, |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 298 | ValueAnimator.RESTART)); |
Chet Haase | d51d368 | 2010-08-11 19:46:48 -0700 | [diff] [blame] | 299 | } |
| 300 | if (evaluator != null) { |
| 301 | anim.setEvaluator(evaluator); |
| 302 | } |
| 303 | |
| 304 | final int resID = |
| 305 | a.getResourceId(com.android.internal.R.styleable.Animator_interpolator, 0); |
| 306 | if (resID > 0) { |
| 307 | anim.setInterpolator(AnimationUtils.loadInterpolator(context, resID)); |
| 308 | } |
| 309 | a.recycle(); |
| 310 | |
| 311 | return anim; |
| 312 | } |
| 313 | } |