【Android】Activity之间的动画切换
不多说,直接上代码,第一个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; } }
第二个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 }
两个界面分别如下:
四个动画文件,位于(res/anim)下面:
1)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 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.android.com/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.android.com/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.android.com/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>
注:调用通过以下一行代码实现:
1 overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);