How can I define the transition between two activities for Android 1.5 and later? I would like an activity to fade in.
You can do this with Activity.overridePendingTransition()
. You can define simple transition animations in an XML resource file.
onCreate
function. –
Pennywise Here's the code to do a nice smooth fade between two Activities..
Create a file called fadein.xml
in res/anim
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />
Create a file called fadeout.xml
in res/anim
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="2000" />
If you want to fade from Activity A to Activity B, put the following in the onCreate()
method for Activity B. Before setContentView()
works for me.
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
If the fades are too slow for you, change android:duration
in the xml files above to something smaller.
overridePendingTransition(android.R.anim.fadein, android.R.anim.fadeout);
Viewing those files may also give you hints on how to improve your custom animations (e.g. by making the fade in last longer than the fade out). –
Jutland fadein
and fadeout
to fade_in
and fade_out
. From Dan J's post –
Cushing overridePendingTransition()
right after calling finish()
and/or startActivity()
. I was able to get a nice fade this way by calling it right after launching the new Activity. –
Seidel You can do this with Activity.overridePendingTransition()
. You can define simple transition animations in an XML resource file.
onCreate
function. –
Pennywise An even easy way to do it is:
- Create an animation style into your styles.xml file
<style name="WindowAnimationTransition"> <item name="android:windowEnterAnimation">@android:anim/fade_in</item> <item name="android:windowExitAnimation">@android:anim/fade_out</item> </style>
- Add this style to your app theme
<style name="AppBaseTheme" parent="Theme.Material.Light.DarkActionBar"> <item name="android:windowAnimationStyle">@style/WindowAnimationTransition</item> </style>
That's it :)
Yes. You can tell the OS what kind of transition you want to have for your activity.
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setWindowAnimations(ANIMATION);
...
}
Where ANIMATION is an integer referring to a built in animation in the OS.
For a list of default animations see: http://developer.android.com/reference/android/R.anim.html
There is in fact fade_in
and fade_out
for API level 1 and up.
create res>anim>fadein.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" />
create res>anim>fadeout.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />
In res>values>styles.xml
<style name="Fade">
<item name="android:windowEnterAnimation">@anim/fadein</item>
<item name="android:windowExitAnimation">@anim/fadeout</item>
</style>
In activities onCreate()
getWindow().getAttributes().windowAnimations = R.style.Fade;
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
–
Triple Here's the code to do a nice smooth between two activity.
smooth effect from left to right
Create a file called slide_in_right.xml and slide_out_right.xml in res/anim
slide_in_right.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" > <translate android:duration="5000" android:fromXDelta="100%" android:toXDelta="0%" /> <alpha android:duration="5000" android:fromAlpha="0.0" android:toAlpha="1.0" /> </set>
slide_out_right.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" > <translate android:duration="5000" android:fromXDelta="0%" android:toXDelta="-100%"/> <alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" /> </set>
smooth effect from right to left
Create a file called animation_enter.xml and animation_leave.xml in res/anim
animation_enter.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="-100%" android:toXDelta="0%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="700"/> </set>
animation_leave.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="0%" android:toXDelta="100%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="700" /> </set>
Navigate from one activity to second Activity
Intent intent_next=new Intent(One_Activity.this,Second_Activity.class); overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right); startActivity(intent_next); finish();
4.On back press event or Navigate from second activity to one Activity
Intent home_intent = new Intent(Second_Activity.this, One_Activity.class); overridePendingTransition(R.anim.animation_enter, R.anim.animation_leave); startActivity(home_intent); finish();
overridePendingTransition
immediately after startActivity: developer.android.com/reference/android/app/… –
Tetzel You cannot use overridePendingTransition in Android 1.5. overridePendingTransistion came to Android 2.0.
If you're gonna go through this without any error you have to compile for the target (1.5 or higher) using the ordinary animations (or you own) or you have to compile for the target (2.0 or higher) using overridePendingTransistion.
Summary: You cannot use overridePendingTransistion in Android 1.5.
You can though use the built-in animations in the OS.
Before Starting your Intent:
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(AlbumListActivity.this);
startActivity(intent, options.toBundle());
This gives Default Animation to your Activity Transition.
IN GALAXY Devices :
You need to make sure that you havn't turned it off in the device using the Settings > Developer Options:
Use ActivityCompat.startActivity() works API > 21.
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, transitionImage, EXTRA_IMAGE);
ActivityCompat.startActivity(activity, intent, options.toBundle());
zoom in out animation
Intent i = new Intent(getApplicationContext(), LoginActivity.class);
overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
startActivity(i);
finish();
zoom_enter
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />
zoom_exit
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:fillAfter="true"
android:duration="500" />
Some versions of Android support custom Activity
transitions and some don't (older devices). If you want to use custom transitions it's good practice to check whether the Activity
has the overridePendingTransition()
method, as on older versions it does not.
To know whether the method exists or not, reflection API can be used. Here is the simple code which will check and return the method if it exists:
Method mOverridePendingTransition;
try {
mOverridePendingTransition = Activity.class.getMethod(
"overridePendingTransition", new Class[] { Integer.TYPE, Integer.TYPE } );
/* success */
} catch (NoSuchMethodException nsme) {
/* failure, this version of Android doesn't have this method */
}
And then, we can apply our own transition, i.e. use this method if it exists:
if (UIConstants.mOverridePendingTransition != null) {
try {
UIConstants.mOverridePendingTransition.invoke(MainActivity.this, R.anim.activity_fade_in, R.anim.activity_fade_out);
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
Here, as an example, simple fade-in and fade-out animations were used for transition demonstration..
Kotlin Extension Function
fun Context.transition(enterTransition: Int, exitTransition: Int) {
(this as Activity).overridePendingTransition(enterTransition, exitTransition)
}
© 2022 - 2024 — McMap. All rights reserved.
overridePendingTransition
-related answers below: You can pass(0, 0)
if you want no animation at all. – Sawfish