Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different exit/enter animations

I want to define two window animation styles for different activities.

This is what I have so far:

Manifest:

<application
    ...>
    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:theme="@style/A" />
    <activity
        android:name=".SecondActivity"
        android:screenOrientation="portrait"
        android:theme="@style/B" />
</application>

Styles.xml:

<style name="A" parent="AppTheme">
    <item name="android:windowAnimationStyle">@style/CustomActivityAnimation</item>
</style>

<style name="CustomActivityAnimation" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/bottom_in</item>
    <item name="android:activityOpenExitAnimation">@anim/scale_out</item>
    <item name="android:activityCloseEnterAnimation">@anim/scale_in</item>
    <item name="android:activityCloseExitAnimation">@anim/bottom_out</item>
</style>


<style name="B" parent="@style/Theme.AppCompat.Light.Dialog">
    <item name="android:windowAnimationStyle">@style/CustomDialogAnimation</item>
</style>

<style name="CustomDialogAnimation" parent="@android:style/Animation.Dialog">
   <item name="android:windowEnterAnimation">@anim/dialog_in</item>
   <item name="android:windowExitAnimation">@anim/dialog_out</item>
</style>

What I want to achieve is:

Transitions between activities with style A should use the animations defined in CustomActivityAnimation.

Activities with style B are styled as a dialog and should have other transition animations defined in CustomDialogAnimation.

My problem:

The android:windowExitAnimation from style CustomDialogAnimation is never used when I close an activity from style B. Instead the android:activityCloseExitAnimation from style CustomActivityAnimation is played.

Any hints?

like image 788
Prexx Avatar asked Aug 21 '18 13:08

Prexx


People also ask

What is the difference between exit and entrance animation?

Entrance animations are used to introduce a slide object within a slide. ... Exit animations are used to animate slide objects off a slide. So, if you were a slide object in a room (slide), then you walking out of the room would be akin to an Exit animation.

Can you have an entrance and exit animation in PowerPoint?

You can combine entrance and exit animations in PowerPoint to make an object appear and disappear on a slide during a slide show. This can occur automatically or on click.

Can you have two animations in PowerPoint at the same time?

In the PowerPoint desktop apps, you can apply multiple animation effects to a single string of text or an object, such as a picture, shape, or SmartArt graphic.


2 Answers

For your purpose you can give it at runtime time as below:

we’ll need four animations in total, and we’ll be defining them through XML. Of the four animations, there are really two groups. The first is to move a view from its current position to a position out of view, and the second is to bring a view in from out of view.

Defining animation XML Files :

slide_to_left.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-100%p"
               android:interpolator="@android:anim/accelerate_decelerate_interpolator"
               android:duration="300"/>
</set>

slide_to_right.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%p"
               android:interpolator="@android:anim/accelerate_decelerate_interpolator"
               android:duration="300"/>
</set>

slide_from_left.xml:

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

slide_from_right.xml:

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

Just as easy is animating the transition between Activities. The Activity class provides us with a method called overridePendingTransition that we can use to set the animation of the exiting and entering Activities, like so:

Intent intent = new Intent(this, B.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_from_right, R.anim.slide_to_left);

Using the animations we defined earlier, we can have the new Activity slide in from the right of the view, and the current Activity slides out of view to the left.

Similarly, when the new Activity is finished, we can perform the reverse animation to have the finished Activity slide out of view to the right, and the previous Activity slide back into view from the left:

finish();
overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right);

Handling on back button:

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_right);
}

credit

like image 67
Gaurang Goda Avatar answered Oct 06 '22 00:10

Gaurang Goda


Try changing the parent of the dialog animation <style name="CustomDialogAnimation" parent="@style/Animation.AppCompat.Dialog"

like image 28
Zachary Sweigart Avatar answered Oct 06 '22 00:10

Zachary Sweigart