这个很简单.主要的就是写下动画,还有就是这个方法记得放在startActivity和finish();的后面,让他们得以立即执行
界面布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical"
tools:context="com.example.testactivityinoutanimation.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/tv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ccc"
android:gravity="center"
android:padding="10dp"
android:text="我是一个按钮"
android:textColor="#000"
android:textSize="16sp" />
<ImageView
android:id="@+id/iv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tv_main"
android:src="@drawable/image1" />
</LinearLayout>
因为界面比较简单.所以两个activity使用的是同一个xml布局,然后在程序中动态的设置显示的内容.来区分两个界面的不同
MainActivity.java
package com.example.testactivityinoutanimation;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends Activity {
private TextView tv_main;
private Activity context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
tv_main = (TextView) findViewById(R.id.tv_main);
tv_main.setText("进入第二个界面");
tv_main.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(context, OtherActivity.class);
startActivity(intent);
// 第一个参数描述的是将要跳转到的activity的进入方式,第二个参数描述的是本界面退出的方式
overridePendingTransition(R.anim.slide_in_right,
R.anim.slide_out_left);
}
});
}
}
OtherActivity.java
package com.example.testactivityinoutanimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class OtherActivity extends BaseActivity {
private TextView tv_main;
private Activity context;
private ImageView iv_main;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setNeedBackGesture(true);//
context = this;
tv_main = (TextView) findViewById(R.id.tv_main);
iv_main = (ImageView) findViewById(R.id.iv_main);
iv_main.setImageResource(R.drawable.image2);
tv_main.setText("返回第一个界面");
tv_main.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
onBackPressed();
}
});
}
@Override
public void onBackPressed() {
finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
/TestActivityInOutAnimation/res/anim/slide_in_right.xml 右侧滑入的动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="100.0%p"
android:toXDelta="0.0" />
</set>
/TestActivityInOutAnimation/res/anim/slide_out_left.xml 左侧滑出的动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0.0"
android:toXDelta="-100.0%p" />
</set>
/TestActivityInOutAnimation/res/anim/slide_in_left.xml 左侧滑入的动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="-100.0%p"
android:toXDelta="0.0" />
</set>
/TestActivityInOutAnimation/res/anim/slide_out_right.xml 右侧滑出的动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0.0"
android:toXDelta="100.0%p" />
</set>