Android Activity transition slide in/out animation
This is a very common effect, however the Android framework only provides the slide in animation. This is how I implement all the sliding transition animation. There’s in total 4 XMLs to define the animation, and with the use of overridePendingTransition() in appropriate location you can implement this easily.
pull_in_left.xml
1
2
3
4
5
6
7
|
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="-100%"
android:toXDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator"
/>
|
pull_in_right.xml
1
2
3
4
5
6
7
|
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="100%"
android:toXDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator"
/>
|
push_out_left.xml
1
2
3
4
5
6
|
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="-100%" />
|
push_out_right.xml
1
2
3
4
5
6
|
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="0%"
android:interpolator="@android:anim/decelerate_interpolator"
android:toXDelta="100%" />
|
Usage
When starting new Activity ( Method 1)
1
2
3
|
Intent intent = new Intent(mContext, NextActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
|
When starting new Activity (Method 2)
1
2
3
4
5
6
|
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
//...
}
|
When Back to previous Activity
1
2
3
4
5
|
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
}
|
Thumbs up
ReplyDelete