AI2 Component  (Version nb184)
AnimationUtil.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime.util;
8 
9 import android.app.Activity;
10 import android.util.Log;
11 import android.view.View;
12 import android.view.animation.Animation;
13 import android.view.animation.AnimationSet;
14 import android.view.animation.TranslateAnimation;
15 
24 public final class AnimationUtil {
25 
26  private AnimationUtil() {
27  }
28 
29  /*
30  * Animates a component moving it horizontally.
31  */
32  private static void ApplyHorizontalScrollAnimation(View view, boolean left, int speed) {
33  float sign = left ? 1f : -1f;
34  AnimationSet animationSet = new AnimationSet(true);
35  animationSet.setRepeatCount(Animation.INFINITE);
36  animationSet.setRepeatMode(Animation.RESTART);
37 
38  TranslateAnimation move = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, sign * 0.70f,
39  Animation.RELATIVE_TO_PARENT, sign * -0.70f, Animation.RELATIVE_TO_PARENT, 0,
40  Animation.RELATIVE_TO_PARENT, 0);
41  move.setStartOffset(0);
42  move.setDuration(speed);
43  move.setFillAfter(true);
44  animationSet.addAnimation(move);
45  view.startAnimation(animationSet);
46  }
47 
54  public static void ApplyAnimation(View view, String animation) {
55  // TODO(user): These string constants need to be extracted and defined somewhere else!
56  // TODO(user): Also, the endless else-if is inefficient
57  if (animation.equals("ScrollRightSlow")) {
58  ApplyHorizontalScrollAnimation(view, false, 8000);
59  } else if (animation.equals("ScrollRight")) {
60  ApplyHorizontalScrollAnimation(view, false, 4000);
61  } else if (animation.equals("ScrollRightFast")) {
62  ApplyHorizontalScrollAnimation(view, false, 1000);
63  } else if (animation.equals("ScrollLeftSlow")) {
64  ApplyHorizontalScrollAnimation(view, true, 8000);
65  } else if (animation.equals("ScrollLeft")) {
66  ApplyHorizontalScrollAnimation(view, true, 4000);
67  } else if (animation.equals("ScrollLeftFast")) {
68  ApplyHorizontalScrollAnimation(view, true, 1000);
69  } else if (animation.equals("Stop")) {
70  view.clearAnimation();
71  }
72  }
73 
81  public static void ApplyOpenScreenAnimation(Activity activity, String animType) {
82  if (animType == null) {
83  return;
84  }
86  Log.e("AnimationUtil", "Screen animations are not available on android versions less than 2.0.");
87  return;
88  }
89  int enter = 0;
90  int exit = 0;
91 
92  if (animType.equalsIgnoreCase("fade")) {
93  enter = activity.getResources().getIdentifier("fadein", "anim", activity.getPackageName());
94  exit = activity.getResources().getIdentifier("hold", "anim", activity.getPackageName());
95  } else if (animType.equalsIgnoreCase("zoom")) {
96  exit = activity.getResources().getIdentifier("zoom_exit", "anim", activity.getPackageName());
97  enter = activity.getResources().getIdentifier("zoom_enter", "anim", activity.getPackageName());
98  } else if (animType.equalsIgnoreCase("slidehorizontal")) {
99  exit = activity.getResources().getIdentifier("slide_exit", "anim", activity.getPackageName());
100  enter = activity.getResources().getIdentifier("slide_enter", "anim", activity.getPackageName());
101  } else if (animType.equalsIgnoreCase("slidevertical")) {
102  exit = activity.getResources().getIdentifier("slide_v_exit", "anim", activity.getPackageName());
103  enter = activity.getResources().getIdentifier("slide_v_enter", "anim", activity.getPackageName());
104  } else if (animType.equalsIgnoreCase("none")) {
105  // enter and exit are already set to 0, so
106  // no animations will be played.
107  } else {
108  // Return here so overridePendingTransitions isn't run, and android
109  // does it's default thing.
110  return;
111  }
112  EclairUtil.overridePendingTransitions(activity, enter, exit);
113  }
114 
121  public static void ApplyCloseScreenAnimation(Activity activity, String animType) {
122  if (animType == null) {
123  return;
124  }
126  Log.e("AnimationUtil", "Screen animations are not available on android versions less than 2.0.");
127  return;
128  }
129  int enter = 0;
130  int exit = 0;
131  if (animType.equalsIgnoreCase("fade")) {
132  exit = activity.getResources().getIdentifier("fadeout", "anim", activity.getPackageName());
133  enter = activity.getResources().getIdentifier("hold", "anim", activity.getPackageName());
134  } else if (animType.equalsIgnoreCase("zoom")) {
135  exit = activity.getResources().getIdentifier("zoom_exit_reverse", "anim", activity.getPackageName());
136  enter = activity.getResources().getIdentifier("zoom_enter_reverse", "anim", activity.getPackageName());
137  } else if (animType.equalsIgnoreCase("slidehorizontal")) {
138  exit = activity.getResources().getIdentifier("slide_exit_reverse", "anim", activity.getPackageName());
139  enter = activity.getResources().getIdentifier("slide_enter_reverse", "anim", activity.getPackageName());
140  } else if (animType.equalsIgnoreCase("slidevertical")) {
141  exit = activity.getResources().getIdentifier("slide_v_exit_reverse", "anim", activity.getPackageName());
142  enter = activity.getResources().getIdentifier("slide_v_enter_reverse", "anim", activity.getPackageName());
143  } else if (animType.equalsIgnoreCase("none")) {
144  // enter and exit are already set to 0, so
145  // no animations will be played.
146  } else {
147  // Return here so overridePendingTransitions isn't run, and android
148  // does it's default thing.
149  return;
150  }
151  EclairUtil.overridePendingTransitions(activity, enter, exit);
152  }
153 
154 }
com.google.appinventor.components.runtime.util.EclairUtil.overridePendingTransitions
static void overridePendingTransitions(Activity activity, int enterAnim, int exitAnim)
Definition: EclairUtil.java:44
com.google.appinventor.components.runtime.util.AnimationUtil
Definition: AnimationUtil.java:24
com.google.appinventor.components.runtime.util.AnimationUtil.ApplyAnimation
static void ApplyAnimation(View view, String animation)
Definition: AnimationUtil.java:54
com.google.appinventor.components.runtime.util.SdkLevel.LEVEL_DONUT
static final int LEVEL_DONUT
Definition: SdkLevel.java:21
com.google.appinventor.components.runtime.util.AnimationUtil.ApplyOpenScreenAnimation
static void ApplyOpenScreenAnimation(Activity activity, String animType)
Definition: AnimationUtil.java:81
com.google.appinventor.components.runtime.util.SdkLevel
Definition: SdkLevel.java:19
com.google.appinventor.components.runtime.util.SdkLevel.getLevel
static int getLevel()
Definition: SdkLevel.java:45
com.google.appinventor.components.runtime.util.EclairUtil
Definition: EclairUtil.java:31
com.google.appinventor.components.runtime.util.AnimationUtil.ApplyCloseScreenAnimation
static void ApplyCloseScreenAnimation(Activity activity, String animType)
Definition: AnimationUtil.java:121