Debian Codegraph @

core/java/android/view/animation/AnimationUtils.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 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.annotation.AnimRes;
20 import android.annotation.InterpolatorRes;
21 import android.annotation.TestApi;
22 import android.compat.annotation.UnsupportedAppUsage;
23 import android.content.Context;
24 import android.content.res.Resources;
25 import android.content.res.Resources.NotFoundException;
26 import android.content.res.Resources.Theme;
27 import android.content.res.XmlResourceParser;
28 import android.os.SystemClock;
29 import android.util.AttributeSet;
30 import android.util.Xml;
31
32 import org.xmlpull.v1.XmlPullParser;
33 import org.xmlpull.v1.XmlPullParserException;
34
35 import java.io.IOException;
36
37 /**
38 * Defines common utilities for working with animations.
39 *
40 */
41 public class AnimationUtils {
42
43 /**
44 * These flags are used when parsing AnimatorSet objects
45 */
46 private static final int TOGETHER = 0;
47 private static final int SEQUENTIALLY = 1;
48
49 private static class AnimationState {
50 boolean animationClockLocked;
51 long currentVsyncTimeMillis;
52 long lastReportedTimeMillis;
53 };
54
55 private static ThreadLocal<AnimationState> sAnimationState
56 = new ThreadLocal<AnimationState>() {
57 @Override
58 protected AnimationState initialValue() {
59 return new AnimationState();
60 }
61 };
62
63 /**
64 * Locks AnimationUtils{@link #currentAnimationTimeMillis()} to a fixed value for the current
65 * thread. This is used by {@link android.view.Choreographer} to ensure that all accesses
66 * during a vsync update are synchronized to the timestamp of the vsync.
67 *
68 * It is also exposed to tests to allow for rapid, flake-free headless testing.
69 *
70 * Must be followed by a call to {@link #unlockAnimationClock()} to allow time to
71 * progress. Failing to do this will result in stuck animations, scrolls, and flings.
72 *
73 * Note that time is not allowed to "rewind" and must perpetually flow forward. So the
74 * lock may fail if the time is in the past from a previously returned value, however
75 * time will be frozen for the duration of the lock. The clock is a thread-local, so
76 * ensure that {@link #lockAnimationClock(long)}, {@link #unlockAnimationClock()}, and
77 * {@link #currentAnimationTimeMillis()} are all called on the same thread.
78 *
79 * This is also not reference counted in any way. Any call to {@link #unlockAnimationClock()}
80 * will unlock the clock for everyone on the same thread. It is therefore recommended
81 * for tests to use their own thread to ensure that there is no collision with any existing
82 * {@link android.view.Choreographer} instance.
83 *
84 * @hide
85 * */
86 @TestApi
87 public static void lockAnimationClock(long vsyncMillis) {
88 AnimationState state = sAnimationState.get();
89 state.animationClockLocked = true;
90 state.currentVsyncTimeMillis = vsyncMillis;
91 }
92
93 /**
94 * Frees the time lock set in place by {@link #lockAnimationClock(long)}. Must be called
95 * to allow the animation clock to self-update.
96 *
97 * @hide
98 */
99 @TestApi
100 public static void unlockAnimationClock() {
101 sAnimationState.get().animationClockLocked = false;
102 }
103
104 /**
105 * Returns the current animation time in milliseconds. This time should be used when invoking
106 * {@link Animation#setStartTime(long)}. Refer to {@link android.os.SystemClock} for more
107 * information about the different available clocks. The clock used by this method is
108 * <em>not</em> the "wall" clock (it is not {@link System#currentTimeMillis}).
109 *
110 * @return the current animation time in milliseconds
111 *
112 * @see android.os.SystemClock
113 */
114 public static long currentAnimationTimeMillis() {
115 AnimationState state = sAnimationState.get();
116 if (state.animationClockLocked) {
117 // It's important that time never rewinds
118 return Math.max(state.currentVsyncTimeMillis,
119 state.lastReportedTimeMillis);
120 }
121 state.lastReportedTimeMillis = SystemClock.uptimeMillis();
122 return state.lastReportedTimeMillis;
123 }
124
125 /**
126 * Loads an {@link Animation} object from a resource
127 *
128 * @param context Application context used to access resources
129 * @param id The resource id of the animation to load
130 * @return The animation object referenced by the specified id
131 * @throws NotFoundException when the animation cannot be loaded
132 */
133 public static Animation loadAnimation(Context context, @AnimRes int id)
134 throws NotFoundException {
135
136 XmlResourceParser parser = null;
137 try {
138 parser = context.getResources().getAnimation(id);
139 return createAnimationFromXml(context, parser);
140 } catch (XmlPullParserException ex) {
141 NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +
142 Integer.toHexString(id));
143 rnf.initCause(ex);
144 throw rnf;
145 } catch (IOException ex) {
146 NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +
147 Integer.toHexString(id));
148 rnf.initCause(ex);
149 throw rnf;
150 } finally {
151 if (parser != null) parser.close();
152 }
153 }
154
155 private static Animation createAnimationFromXml(Context c, XmlPullParser parser)
156 throws XmlPullParserException, IOException {
157
158 return createAnimationFromXml(c, parser, null, Xml.asAttributeSet(parser));
159 }
160
161 @UnsupportedAppUsage
162 private static Animation createAnimationFromXml(Context c, XmlPullParser parser,
163 AnimationSet parent, AttributeSet attrs) throws XmlPullParserException, IOException {
164
165 Animation anim = null;
166
167 // Make sure we are on a start tag.
168 int type;
169 int depth = parser.getDepth();
170
171 while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
172 && type != XmlPullParser.END_DOCUMENT) {
173
174 if (type != XmlPullParser.START_TAG) {
175 continue;
176 }
177
178 String name = parser.getName();
179
180 if (name.equals("set")) {
181 anim = new AnimationSet(c, attrs);
182 createAnimationFromXml(c, parser, (AnimationSet)anim, attrs);
183 } else if (name.equals("alpha")) {
184 anim = new AlphaAnimation(c, attrs);
185 } else if (name.equals("scale")) {
186 anim = new ScaleAnimation(c, attrs);
187 } else if (name.equals("rotate")) {
188 anim = new RotateAnimation(c, attrs);
189 } else if (name.equals("translate")) {
190 anim = new TranslateAnimation(c, attrs);
191 } else if (name.equals("cliprect")) {
192 anim = new ClipRectAnimation(c, attrs);
193 } else if (name.equals("extend")) {
194 anim = new ExtendAnimation(c, attrs);
195 } else {
196 throw new RuntimeException("Unknown animation name: " + parser.getName());
197 }
198
199 if (parent != null) {
200 parent.addAnimation(anim);
201 }
202 }
203
204 return anim;
205
206 }
207
208 /**
209 * Loads a {@link LayoutAnimationController} object from a resource
210 *
211 * @param context Application context used to access resources
212 * @param id The resource id of the animation to load
213 * @return The animation controller object referenced by the specified id
214 * @throws NotFoundException when the layout animation controller cannot be loaded
215 */
216 public static LayoutAnimationController loadLayoutAnimation(Context context, @AnimRes int id)
217 throws NotFoundException {
218
219 XmlResourceParser parser = null;
220 try {
221 parser = context.getResources().getAnimation(id);
222 return createLayoutAnimationFromXml(context, parser);
223 } catch (XmlPullParserException ex) {
224 NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +
225 Integer.toHexString(id));
226 rnf.initCause(ex);
227 throw rnf;
228 } catch (IOException ex) {
229 NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +
230 Integer.toHexString(id));
231 rnf.initCause(ex);
232 throw rnf;
233 } finally {
234 if (parser != null) parser.close();
235 }
236 }
237
238 private static LayoutAnimationController createLayoutAnimationFromXml(Context c,
239 XmlPullParser parser) throws XmlPullParserException, IOException {
240
241 return createLayoutAnimationFromXml(c, parser, Xml.asAttributeSet(parser));
242 }
243
244 private static LayoutAnimationController createLayoutAnimationFromXml(Context c,
245 XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException, IOException {
246
247 LayoutAnimationController controller = null;
248
249 int type;
250 int depth = parser.getDepth();
251
252 while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
253 && type != XmlPullParser.END_DOCUMENT) {
254
255 if (type != XmlPullParser.START_TAG) {
256 continue;
257 }
258
259 String name = parser.getName();
260
261 if ("layoutAnimation".equals(name)) {
262 controller = new LayoutAnimationController(c, attrs);
263 } else if ("gridLayoutAnimation".equals(name)) {
264 controller = new GridLayoutAnimationController(c, attrs);
265 } else {
266 throw new RuntimeException("Unknown layout animation name: " + name);
267 }
268 }
269
270 return controller;
271 }
272
273 /**
274 * Make an animation for objects becoming visible. Uses a slide and fade
275 * effect.
276 *
277 * @param c Context for loading resources
278 * @param fromLeft is the object to be animated coming from the left
279 * @return The new animation
280 */
281 public static Animation makeInAnimation(Context c, boolean fromLeft) {
282 Animation a;
283 if (fromLeft) {
284 a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_in_left);
285 } else {
286 a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_in_right);
287 }
288
289 a.setInterpolator(new DecelerateInterpolator());
290 a.setStartTime(currentAnimationTimeMillis());
291 return a;
292 }
293
294 /**
295 * Make an animation for objects becoming invisible. Uses a slide and fade
296 * effect.
297 *
298 * @param c Context for loading resources
299 * @param toRight is the object to be animated exiting to the right
300 * @return The new animation
301 */
302 public static Animation makeOutAnimation(Context c, boolean toRight) {
303 Animation a;
304 if (toRight) {
305 a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_out_right);
306 } else {
307 a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_out_left);
308 }
309
310 a.setInterpolator(new AccelerateInterpolator());
311 a.setStartTime(currentAnimationTimeMillis());
312 return a;
313 }
314
315
316 /**
317 * Make an animation for objects becoming visible. Uses a slide up and fade
318 * effect.
319 *
320 * @param c Context for loading resources
321 * @return The new animation
322 */
323 public static Animation makeInChildBottomAnimation(Context c) {
324 Animation a;
325 a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_in_child_bottom);
326 a.setInterpolator(new AccelerateInterpolator());
327 a.setStartTime(currentAnimationTimeMillis());
328 return a;
329 }
330
331 /**
332 * Loads an {@link Interpolator} object from a resource
333 *
334 * @param context Application context used to access resources
335 * @param id The resource id of the animation to load
336 * @return The interpolator object referenced by the specified id
337 * @throws NotFoundException
338 */
339 public static Interpolator loadInterpolator(Context context, @AnimRes @InterpolatorRes int id)
340 throws NotFoundException {
341 XmlResourceParser parser = null;
342 try {
343 parser = context.getResources().getAnimation(id);
344 return createInterpolatorFromXml(context.getResources(), context.getTheme(), parser);
345 } catch (XmlPullParserException ex) {
346 NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +
347 Integer.toHexString(id));
348 rnf.initCause(ex);
349 throw rnf;
350 } catch (IOException ex) {
351 NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +
352 Integer.toHexString(id));
353 rnf.initCause(ex);
354 throw rnf;
355 } finally {
356 if (parser != null) parser.close();
357 }
358
359 }
360
361 /**
362 * Loads an {@link Interpolator} object from a resource
363 *
364 * @param res The resources
365 * @param id The resource id of the animation to load
366 * @return The interpolator object referenced by the specified id
367 * @throws NotFoundException
368 * @hide
369 */
370 public static Interpolator loadInterpolator(Resources res, Theme theme, int id) throws NotFoundException {
371 XmlResourceParser parser = null;
372 try {
373 parser = res.getAnimation(id);
374 return createInterpolatorFromXml(res, theme, parser);
375 } catch (XmlPullParserException ex) {
376 NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +
377 Integer.toHexString(id));
378 rnf.initCause(ex);
379 throw rnf;
380 } catch (IOException ex) {
381 NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +
382 Integer.toHexString(id));
383 rnf.initCause(ex);
384 throw rnf;
385 } finally {
386 if (parser != null)
387 parser.close();
388 }
389
390 }
391
392 private static Interpolator createInterpolatorFromXml(Resources res, Theme theme, XmlPullParser parser)
393 throws XmlPullParserException, IOException {
394
395 BaseInterpolator interpolator = null;
396
397 // Make sure we are on a start tag.
398 int type;
399 int depth = parser.getDepth();
400
401 while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
402 && type != XmlPullParser.END_DOCUMENT) {
403
404 if (type != XmlPullParser.START_TAG) {
405 continue;
406 }
407
408 AttributeSet attrs = Xml.asAttributeSet(parser);
409
410 String name = parser.getName();
411
412 if (name.equals("linearInterpolator")) {
413 interpolator = new LinearInterpolator();
414 } else if (name.equals("accelerateInterpolator")) {
415 interpolator = new AccelerateInterpolator(res, theme, attrs);
416 } else if (name.equals("decelerateInterpolator")) {
417 interpolator = new DecelerateInterpolator(res, theme, attrs);
418 } else if (name.equals("accelerateDecelerateInterpolator")) {
419 interpolator = new AccelerateDecelerateInterpolator();
420 } else if (name.equals("cycleInterpolator")) {
421 interpolator = new CycleInterpolator(res, theme, attrs);
422 } else if (name.equals("anticipateInterpolator")) {
423 interpolator = new AnticipateInterpolator(res, theme, attrs);
424 } else if (name.equals("overshootInterpolator")) {
425 interpolator = new OvershootInterpolator(res, theme, attrs);
426 } else if (name.equals("anticipateOvershootInterpolator")) {
427 interpolator = new AnticipateOvershootInterpolator(res, theme, attrs);
428 } else if (name.equals("bounceInterpolator")) {
429 interpolator = new BounceInterpolator();
430 } else if (name.equals("pathInterpolator")) {
431 interpolator = new PathInterpolator(res, theme, attrs);
432 } else {
433 throw new RuntimeException("Unknown interpolator name: " + parser.getName());
434 }
435 }
436 return interpolator;
437 }
438 }