android中的简单animation(二)push up,push left,cross fade,hyperspace

animation_2.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3         android:orientation="vertical"
 4         android:padding="10dip"
 5         android:layout_width="match_parent"
 6         android:layout_height="wrap_content">
 7 
 8     <ViewFlipper android:id="@+id/flipper"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:flipInterval="2000"
12         android:layout_marginBottom="20dip" >
13                 <TextView
14                         android:layout_width="match_parent"
15                         android:layout_height="wrap_content"
16                         android:gravity="center_horizontal"
17                         android:textSize="26sp"
18                         android:text="@string/animation_2_text_1"/>
19                 <TextView
20                         android:layout_width="match_parent"
21                         android:layout_height="wrap_content"
22                         android:gravity="center_horizontal"
23                         android:textSize="26sp"
24                         android:text="@string/animation_2_text_2"/>
25                 <TextView
26                         android:layout_width="match_parent"
27                         android:layout_height="wrap_content"
28                         android:gravity="center_horizontal"
29                         android:textSize="26sp"
30                         android:text="@string/animation_2_text_3"/>
31                 <TextView
32                         android:layout_width="match_parent"
33                         android:layout_height="wrap_content"
34                         android:gravity="center_horizontal"
35                         android:textSize="26sp"
36                         android:text="@string/animation_2_text_4"/>
37     </ViewFlipper>
38 
39     <TextView
40         android:layout_width="match_parent"
41         android:layout_height="wrap_content"
42         android:layout_marginBottom="5dip"
43         android:text="@string/animation_2_instructions"
44     />
45 
46     <Spinner android:id="@+id/spinner"
47         android:layout_width="match_parent"
48         android:layout_height="wrap_content"
49     />
50 
51 </LinearLayout>
View Code

push_up_in.xml(100%p表示相对于父控件100%):

1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android">
3     <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="300"/>
4     <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
5 </set>

push_up_out.xml:

1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android">
3     <translate android:fromYDelta="0" android:toYDelta="-100%p" android:duration="300"/>
4     <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" />
5 </set>

push_left_in.xml:

1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android">
3     <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="300"/>
4     <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
5 </set>

push_left_out.xml:

1 <?xml version="1.0" encoding="utf-8"?>
2 <set xmlns:android="http://schemas.android.com/apk/res/android">
3     <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="300"/>
4     <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" />
5 </set>

Animation2.java:

 1 package com.example.android.apis.view;
 2 
 3 // Need the following import to get access to the app resources, since this
 4 // class is in a sub-package.
 5 import com.example.android.apis.R;
 6 
 7 import android.app.Activity;
 8 import android.os.Bundle;
 9 import android.view.View;
10 import android.view.animation.AnimationUtils;
11 import android.widget.AdapterView;
12 import android.widget.ArrayAdapter;
13 import android.widget.Spinner;
14 import android.widget.ViewFlipper;
15 
16 
17 public class Animation2 extends Activity implements
18         AdapterView.OnItemSelectedListener {
19 
20     @Override
21     public void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.animation_2);
24 
25         mFlipper = ((ViewFlipper) this.findViewById(R.id.flipper));
26         mFlipper.startFlipping();
27 
28         Spinner s = (Spinner) findViewById(R.id.spinner);
29         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
30                 android.R.layout.simple_spinner_item, mStrings );
31         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
32         s.setAdapter(adapter);
33         s.setOnItemSelectedListener(this);
34     }
35 
36     public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
37         switch (position) {
38 
39         case 0:
40             mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
41                     R.anim.push_up_in));
42             mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
43                     R.anim.push_up_out));
44             break;
45         case 1:
46             mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
47                     R.anim.push_left_in));
48             mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
49                     R.anim.push_left_out));
50             break;
51         case 2:
52             mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
53                     android.R.anim.fade_in));
54             mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
55                     android.R.anim.fade_out));
56             break;
57         default:
58             mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
59                     R.anim.hyperspace_in));                              //hyperspace多维空间
60             mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
61                     R.anim.hyperspace_out));
62             break;
63         }
64     }
65 
66     public void onNothingSelected(AdapterView<?> parent) {
67     }
68 
69     private String[] mStrings = {
70             "Push up", "Push left", "Cross fade", "Hyperspace"};
71 
72     private ViewFlipper mFlipper;
73 
74 }
View Code

显示效果如下,选择不同的显示方式,文字将会按照显示方式作不同的显示:

原文地址:https://www.cnblogs.com/leihupqrst/p/3254504.html