Android Activity切换动画案例
Activity的切换动画指的是从一个activity跳转到另外一个activity时的动画。它包括两个部分:
一部分是第一个activity退出时的动画;另外一部分时第二个activity进入时的动画。
在Android的2.0版本之后,有了一个overridePendingTransition函数来帮我们实现这个动画
测试类:
ActivityAnimationTest.java
package com.example.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ActivityAnimationTest extends Activity {
private int mAnimResoureIdEnter;
private int mAnimResoureIdExit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_anim_main);
((Button) findViewById(R.id.btn1)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAnimResoureIdEnter = R.anim.zoomin;
mAnimResoureIdExit = R.anim.zoomout;
finish();
}
});
((Button) findViewById(R.id.btn2)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAnimResoureIdEnter = R.anim.slide_in_right;
mAnimResoureIdExit = R.anim.slide_out_right;
finish();
}
});
((Button) findViewById(R.id.btn3)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAnimResoureIdEnter = R.anim.slide_in_left;
mAnimResoureIdExit = R.anim.slide_out_right;
finish();
}
});
((Button) findViewById(R.id.btn4)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAnimResoureIdEnter = R.anim.fade_in;
mAnimResoureIdExit = R.anim.fade_out;
finish();
}
});
}
@Override
public void finish() {
super.finish();
overridePendingTransition(mAnimResoureIdEnter, mAnimResoureIdExit);
}
}
布局文件:
<?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:orientation="vertical" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="zoomin zoomout" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="slide_in_right slide_out_right" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="slide_in_left slide_out_right" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fade_in fade_out" />
</LinearLayout>
上述用到的动画案例:
一 从大到小
1. zoomin.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:Android="http://schemas.android.com/apk/res/android"
Android:interpolator="@android:anim/decelerate_interpolator" >
<scale
Android:duration="@android:integer/config_mediumAnimTime"
Android:fromXScale="2.0"
Android:fromYScale="2.0"
Android:pivotX="50%p"
Android:pivotY="50%p"
Android:toXScale="1.0"
Android:toYScale="1.0" />
</set>
2.zoomout.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:Android="http://schemas.android.com/apk/res/android"
Android:interpolator="@android:anim/decelerate_interpolator"
Android:zAdjustment="top" >
<scale
Android:duration="@android:integer/config_mediumAnimTime"
Android:fromXScale="1.0"
Android:fromYScale="1.0"
Android:pivotX="50%p"
Android:pivotY="50%p"
Android:toXScale=".5"
Android:toYScale=".5" />
<alpha
Android:duration="@android:integer/config_mediumAnimTime"
Android:fromAlpha="1.0"
Android:toAlpha="0" />
</set>
二 右进右出
1.slide_in_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="50%p"
android:toXDelta="0" />
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
2.slide_out_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="50%p" />
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
三 左进右出
1.slide_in_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="-50%p"
android:toXDelta="0" />
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
2.slide_out_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="@android:integer/config_mediumAnimTime"
android:fromXDelta="0"
android:toXDelta="50%p" />
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
四 淡入淡出
1.fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_longAnimTime"
android:fromAlpha="0.0"
android:interpolator="@interpolator/decelerate_quad"
android:toAlpha="1.0" />
2.fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="1.0"
android:interpolator="@interpolator/accelerate_quad"
android:toAlpha="0.0" />
3.
res/interpolator/accelerate_quad.xml
<?xml version="1.0" encoding="utf-8"?>
<accelerateInterpolator />
res/interpolator/decelerate_quad.xml
<?xml version="1.0" encoding="utf-8"?>
<decelerateInterpolator />