Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transition from one screen to next with animation

I just looked at the twitter app and it seems to have a nice sliding transition while moving from one screen to the next. I'm trying to get the same behavior in my app.

Currently I move between screens with:

startActivityForResult(new Intent(getApplicationContext(), MyActivity.class), 1);

But this way there is no transition between the screen. The MyActivity just pops up on the screen.

like image 641
Anthony Avatar asked Feb 14 '23 00:02

Anthony


2 Answers

Animations with drawables:

This tutorial might help you to understand how it works. First, you should create a folder named anim in /res/ folder. Then, create and put into it the drawables which will be uses to make a transition animation as follows:

anim_left_to_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="-100%"
        android:toXDelta="0%"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>  

anim_right_to_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="0%"
        android:toXDelta="-100%"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>  

Then, use overridePendingTransition() method to call the drawables and apply them to the startActivity() (or startActivityForResult()) method:

startActivityForResult(new Intent(getApplicationContext(), MyActivity.class), 1);
overridePendingTransition(anim_left_to_right, anim_right_to_left);

Custom animations relative to lifecycle:

You can also make custom animations, "regardless startActivity method", but in using the lifecycle of activities: I mean like Vine when you call the enter animation into onCreate() and the out animation into onPause(). This is a great demo about this kind of feature.

like image 166
Blo Avatar answered Feb 15 '23 15:02

Blo


Call overridePendingTransition(entry_anim, exit_anim) after calling the startActivity().

You can specify the entry and exit animations through xml.

like image 40
Swayam Avatar answered Feb 15 '23 13:02

Swayam