44import android .graphics .Paint ;
55import android .os .Bundle ;
66import android .view .View ;
7+ import android .view .ViewGroup ;
8+ import android .view .animation .AlphaAnimation ;
79import android .view .animation .Animation ;
810import android .view .animation .Animation .AnimationListener ;
911import android .view .animation .AnimationUtils ;
12+ import android .view .animation .BounceInterpolator ;
13+ import android .view .animation .LayoutAnimationController ;
1014import android .view .animation .TranslateAnimation ;
11- import android .widget .Button ;
1215import android .widget .TextView ;
16+ import android .widget .Toast ;
1317
1418public class AnimationExample extends Activity implements AnimationListener {
19+ private TextView animatedView3 ;
20+
1521 /** Called when the activity is first created. */
1622 @ Override
1723 public void onCreate (Bundle savedInstanceState ) {
@@ -23,6 +29,7 @@ public void onCreate(Bundle savedInstanceState) {
2329 public void startAnimation (View view ) {
2430 switch (view .getId ()) {
2531 case R .id .Button01 :
32+ // Show how to load an animation from XML
2633 Animation animation1 = AnimationUtils .loadAnimation (this ,
2734 R .anim .myanimation );
2835 animation1 .setAnimationListener (this );
@@ -31,16 +38,52 @@ public void startAnimation(View view) {
3138 break ;
3239
3340 case R .id .Button02 :
34- // Get the window manager
35- // Display display = getWindowManager().getDefaultDisplay();
36- // int width = display.getWidth();
37- Paint paint = new Paint ();
38- TextView animatedView2 = (TextView ) findViewById (R .id .scrolltext );
39- float measureTextCenter = paint .measureText (animatedView2 .getText ().toString ());
40- Animation animation2 = new TranslateAnimation (0f , -measureTextCenter , 0.0f , 0.0f );
41- animation2 .setDuration (2000 );
42- // animatedView.setAnimation(animation2);
43- animatedView2 .startAnimation (animation2 );
41+ // Shows how to define a animation via code
42+ // Also use an Interpolator (BounceInterpolator)
43+ Paint paint = new Paint ();
44+ TextView animatedView2 = (TextView ) findViewById (R .id .scrolltext );
45+ float measureTextCenter = paint .measureText (animatedView2 .getText ()
46+ .toString ());
47+ Animation animation2 = new TranslateAnimation (0f ,
48+ -measureTextCenter , 0.0f , 0.0f );
49+ animation2 .setDuration (5000 );
50+ animation2 .setInterpolator (new BounceInterpolator ());
51+ animatedView2 .startAnimation (animation2 );
52+ break ;
53+
54+ case R .id .Button03 :
55+ // Demonstrate fading and adding an AnimationListener
56+ animatedView3 = (TextView ) findViewById (R .id .fadeout );
57+ float from = 1.0f ;
58+ float to = 0.0f ;
59+
60+ if (animatedView3 .getVisibility () == View .INVISIBLE ) {
61+ from = to ;
62+ to = 1.0f ;
63+ }
64+
65+ Animation animation3 = new AlphaAnimation (from , to );
66+ animation3 .setDuration (5000 );
67+ animation3 .setAnimationListener (this );
68+ animatedView3 .startAnimation (animation3 );
69+
70+ break ;
71+
72+ case R .id .Button04 :
73+ // Demonstrate LayoutAnimation
74+ ViewGroup layout = (ViewGroup ) findViewById (R .id .layout );
75+ Animation animation4 = new AlphaAnimation (0.0f , 1.0f );
76+ animation4 .setDuration (5000 );
77+ LayoutAnimationController controller = new LayoutAnimationController (
78+ animation4 , 0 );
79+ layout .setLayoutAnimation (controller );
80+
81+ View button = findViewById (R .id .Button03 );
82+ if (button == null ) {
83+ layout .addView (button );
84+ } else {
85+ layout .removeView (button );
86+ }
4487 break ;
4588
4689 default :
@@ -51,20 +94,22 @@ public void startAnimation(View view) {
5194
5295 @ Override
5396 public void onAnimationStart (Animation animation ) {
54-
97+ Toast . makeText ( this , "Animation started" , Toast . LENGTH_SHORT ). show ();
5598 }
5699
57100 @ Override
58101 public void onAnimationEnd (Animation animation ) {
59- Animation inAnimation = AnimationUtils .makeInAnimation (this , false );
60- Button button = new Button (this );
61- button .setText ("This is the new view" );
62- button .setAnimation (inAnimation );
102+ Toast .makeText (this , "Animation ended" , Toast .LENGTH_SHORT ).show ();
103+ if (animatedView3 .getVisibility () == View .VISIBLE ) {
104+ animatedView3 .setVisibility (View .INVISIBLE );
105+ } else {
106+ animatedView3 .setVisibility (View .VISIBLE );
107+ }
63108 }
64109
65110 @ Override
66111 public void onAnimationRepeat (Animation animation ) {
67- // TODO Auto-generated method stub
112+ Toast . makeText ( this , "Animation rep" , Toast . LENGTH_SHORT ). show ();
68113
69114 }
70115}
0 commit comments