安卓机型杂乱,大部分的机子默认的activity在startActivity的时候都是默认从右往左进来的,但是如果有时候我们需要点击返回跳转到前面的activity,此时又是重新startActivity,为了使activity从左往右进来,就需要自己写两个简单的动画文件,然后利用overridePendingTransition方法设置。
第一步,先在res文件下新建一个anim文件夹
然后在anim文件夹下面写动画xml文件
代码示例:
R.anim.in_from_left.xml 从左边进
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="-100%p"
android:toXDelta="0%p" >
</translate>
R.anim.out_to_right.xml 从右边出
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="0%p"
android:toXDelta="100%p" >
</translate>
R.anim.in_from_right.xml 从右边进
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="100%p"
android:toXDelta="0%p" >
</translate>
R.anim.out_to_left.xml 从左边出
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="0%p"
android:toXDelta="-100%p" >
</translate>
android:duration="300" 动画持续0.3秒
android:fromXDelta="0" android:toXDelta="-100%p" 往左边消失
android:fromXDelta="-100%p" android:toXDelta="0" 从左边进
android:fromXDelta="0" android:toXDelta="100%p" 往右边消失
android:fromXDelta="100%p" android:toXDelta="0" 從右边进
android:fromXDelta="0" android:toXDelta="-100%p" 往左边消失
android:fromXDelta="-100%p" android:toXDelta="0" 从左边进
android:fromXDelta="0" android:toXDelta="100%p" 往右边消失
android:fromXDelta="100%p" android:toXDelta="0" 從右边进
ActivityOne代码
package com.Julia.activity;
import com.Julia.testactivity.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
/**
* @Title: ActivityOne.java
* @Package
* @Description: TODO(第一个activity)
* @author XieHuiZhu xiehuizhu_gx@sina.com
* @date 2016-1-29 下午4:10:26
* @version V1.0
*/
public class ActivityOne extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
findViewById(R.id.jump_out_right).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
startActivity(intent);
overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
finish();
}
});
findViewById(R.id.jump_out_left).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
startActivity(intent);
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
finish();
}
});
}
}
ActivityOne的layout代码:activity_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:gravity="center"
android:text="ActivityOne" />
<Button
android:id="@+id/jump_out_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左进右出" />
<Button
android:id="@+id/jump_out_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右进左出" />
</LinearLayout>
ActivityTwo代码
/**
* @Title: ActivityTwo.java
* @Package com.Julia.activity
* @Description: TODO(第二个activity)
* @author XieHuiZhu xiehuizhu_gx@sina.com
* @date 2016-1-29 下午4:20:25
* @version V1.0
*/
package com.Julia.activity;
import com.Julia.testactivity.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class ActivityTwo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
findViewById(R.id.jump_in_left).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(ActivityTwo.this, ActivityOne.class));
overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
finish();
}
});
findViewById(R.id.jump_in_right).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(ActivityTwo.this, ActivityOne.class));
overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
finish();
}
});
}
}
ActivityTwo的layout代码:activity_two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ActivityTwo"
android:layout_marginBottom="100dp" />
<Button
android:id="@+id/jump_in_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左进右出" />
<Button
android:id="@+id/jump_in_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右进左出" />
</LinearLayout>
overridePendingTransition(进入的动画, 退出的动画);
如果前面的动画写了,后面的动画里面是空的文件,则跳转的时候背景是黑色的。