如果您希望整个应用程序的转换工作,您可以创建一个rootacivity在你需要的活动中继承它。在根活动的onCreate调用中overridePendingTransition带着想要的方向。和onStart打电话overridePendingTransition如果活动恢复到其他方向。这里我给出了完整的运行代码,如果我错了,请告诉我。
在anim文件夹上创建这个xml文件
anim_幻灯片_in_left.xml<?xml version="1.0" encoding="utf-8"?>
android:duration="600"
android:fromXDelta="100%"
android:toXDelta="0%" >
anim_幻灯片_in_right.<?xml version="1.0" encoding="utf-8"?>
android:duration="600"
android:fromXDelta="-100%"
android:toXDelta="0%" >
anim_LECT_OUT_Left.xml<?xml version="1.0" encoding="utf-8"?>
android:duration="600"
android:fromXDelta="0%"
android:toXDelta="-100%" >
anim_幻灯片_out_right.<?xml version="1.0" encoding="utf-8"?>
android:duration="600"
android:fromXDelta="0%"
android:toXDelta="100%" >
根活性import android.app.Activity;import android.os.Bundle;public class RootActivity extends Activity {
int onStartCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onStartCount = 1;
if (savedInstanceState == null) // 1st time
{
this.overridePendingTransition(R.anim.anim_slide_in_left,
R.anim.anim_slide_out_left);
} else // already created so reverse animation
{
onStartCount = 2;
}
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
if (onStartCount > 1) {
this.overridePendingTransition(R.anim.anim_slide_in_right,
R.anim.anim_slide_out_right);
} else if (onStartCount == 1) {
onStartCount++;
}
}}
第一活性import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;
import android.widget.Button;import android.widget.TextView;public class FirstActivity extends RootActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.tvTitle);
tv.setText("First Activity");
Button bt = (Button) findViewById(R.id.buttonNext);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(i);
}
});
}}
二次活动import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener
;import android.widget.Button;import android.widget.TextView;public class SecondActivity extends RootActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.tvTitle);
tv.setText("Second Activity");
Button bt = (Button) findViewById(R.id.buttonNext);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(SecondActivity.this, ThirdActivity.class);
startActivity(i);
}
});
}}
第三次活动import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;
import android.widget.TextView;public class ThirdActivity extends RootActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.tvTitle);
tv.setText("Third Activity");
Button bt = (Button) findViewById(R.id.buttonNext);
bt.setText("previous");
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}}
最后舱单<?xml version="1.0" encoding="utf-8"?>
package="com.example.transitiontest"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="18" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.example.transitiontest.FirstActivity"
android:label="@string/app_name" >
android:name="com.example.transitiontest.SecondActivity"
android:label="@string/app_name" >
android:name="com.example.transitiontest.ThirdActivity"
android:label="@string/app_name" >