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?
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.
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.
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.
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
Try changing the parent of the dialog animation
<style name="CustomDialogAnimation" parent="@style/Animation.AppCompat.Dialog"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With