不多說(shuō),直接上代碼,第一個(gè)Activity: package com.example.activityanimation; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button1); button.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MainActivity.this, TestActivity.class); MainActivity.this.finish(); startActivity(intent); overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } 第二個(gè)Activity: 1 package com.example.activityanimation; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.view.Menu; 7 import android.view.View; 8 import android.widget.Button; 9 10 public class TestActivity extends Activity { 11 Button button; 12 13 @Override 14 public void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_test); 17 18 button = (Button)findViewById(R.id.button2); 19 button.setOnClickListener(new Button.OnClickListener(){ 20 @Override 21 public void onClick(View v) { 22 // TODO Auto-generated method stub 23 Intent intent = new Intent(); 24 intent.setClass(TestActivity.this, MainActivity.class); 25 TestActivity.this.finish(); 26 startActivity(intent); 27 overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out); 28 } 29 }); 30 } 31 32 @Override 33 public boolean onCreateOptionsMenu(Menu menu) { 34 getMenuInflater().inflate(R.menu.activity_test, menu); 35 return true; 36 } 37 } 兩個(gè)界面分別如下: 四個(gè)動(dòng)畫(huà)文件,位于(res/anim)下面: 1)push_left_in.xml: 1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas./apk/res/android"> 3 <translate 4 android:fromXDelta="100%p" 5 android:toXDelta="0" 6 android:duration="1000" /> 7 <alpha 8 android:fromAlpha="0.1" 9 android:toAlpha="1.0" 10 android:duration="1000" /> 11 12 <scale 13 android:fromXScale="2.0" 14 android:toXScale="1.0" 15 android:fromYScale="2.0" 16 android:toYScale="1.0" 17 android:pivotX="50%p" 18 android:pivotY="50%p" 19 android:duration="1000" 20 /> 21 </set> 2)push_left_out.xml: 1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas./apk/res/android"> 3 <translate 4 android:fromXDelta="0" 5 android:toXDelta="-100%p" 6 android:duration="1000" /> 7 <alpha 8 android:fromAlpha="1.0" 9 android:toAlpha="0.1" 10 android:duration="1000" /> 11 12 <scale 13 android:fromXScale="1.0" 14 android:toXScale="2.0" 15 android:fromYScale="1.0" 16 android:toYScale="2.0" 17 android:pivotX="50%p" 18 android:pivotY="50%p" 19 android:duration="1000" 20 /> 21 </set> 3)push_right_in.xml: 1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas./apk/res/android"> 3 <translate 4 android:fromXDelta="-100%p" 5 android:toXDelta="0" 6 android:duration="1000" /> 7 <alpha 8 android:fromAlpha="0.1" 9 android:toAlpha="1.0" 10 android:duration="1000" /> 11 12 <scale 13 android:fromXScale="2.0" 14 android:toXScale="1.0" 15 android:fromYScale="2.0" 16 android:toYScale="1.0" 17 android:pivotX="50%p" 18 android:pivotY="50%p" 19 android:duration="1000" 20 /> 21 </set> 4)push_right_out.xml: 1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas./apk/res/android"> 3 <translate 4 android:fromXDelta="0" 5 android:toXDelta="100%p" 6 android:duration="1000" /> 7 <alpha 8 android:fromAlpha="1.0" 9 android:toAlpha="0.1" 10 android:duration="1000" /> 11 12 <scale 13 android:fromXScale="1.0" 14 android:toXScale="2.0" 15 android:fromYScale="1.0" 16 android:toYScale="2.0" 17 android:pivotX="50%p" 18 android:pivotY="50%p" 19 android:duration="1000" 20 /> 21 </set> 注:調(diào)用通過(guò)以下一行代碼實(shí)現(xiàn): 1 overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out); |
|