How to create custom transitions in Android Studio?

Asked

Viewed 3,326 times

1

I would like to know how to make custom transitions on Android. Transitions between activities are always the same. Have some tutorial that teaches you how to improve transitions?

  • The answer helped you or needs some more example?

1 answer

6

To create custom animations between screen transitions in the application, you can use the method overridePendingTransition() of its public class Activity. Its syntax is:

public void overridePendingTransition(int animacaoEntrada, int animacaoSaida)

Have some tutorial that teaches you how to improve transitions?

Yes, there are several tutorials, but I’ll give you a short summary. As for the improvement of transitions, it goes from the creativity of each.

You see, there are some standard transitions within the directory android.R.anim as an example fade_in and fade_out. So applying this animation would be this way:

Intent i = new Intent(this, EnterActivity.class);
startActivity(i);
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);

That’ll get you out of layout gently using the tag alpha, such as the fade_in. Behold:

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:interpolator="@interpolator/decelerate_quad"
        android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="@android:integer/config_longAnimTime" />

To create a custom animation, simply create directory with the name anim in his Resource, and insert the animation file as for example botton_in and top_out that will cause the layout called will come from bottom to top, different from the default transition. See how it would be:

botton_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <translate
        android:fromYDelta="100%p"
        android:toYDelta="0"
        android:duration="150"/>
</set>

top_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <translate
        android:fromYDelta="0"
        android:toYDelta="-100%p"
        android:duration="150"/>
</set> 

Class

Intent i = new Intent(this, EnterActivity.class);
startActivity(i);
overridePendingTransition(R.anim.botton_in, R.anim.top_out);

The Material theme provides some standard animations for buttons and activity transitions, and Android 5.0 (Level 21 API) and later lets you customize these animations and create new ones. You can see in How to define custom animations.

  • Thanks Ack Lay...but there’s a porem.. how I implement this in Mainactivity?

  • @Legolas when you switch from one activity to another. To change you use Intent. The code is there in the answer.

  • Thanks...I’ve seen here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.