Activity animation method
Add activity's animation attribute windowAnimationStyle in AndroidMenifest
<item name="android:windowAnimationStyle">@style/anim_fade</item>
Add overridePendingTransition in activity code
overridePendingTransition(int enterAnim,int exitAnim)
Summary of questions
- 1. There is something wrong with the animation writing
- 2. Set the animation to null in the activity theme, or set the animation to null in the parent theme
- 3. The timing of use of overridePendingTransition
- 4. OverridePendingTransition Written incorrectly
- 5. The overridePendingTransition in onPause and onResume will overwrite other locations
- 6. Transparency affects animation
- 7. The plug-in problem causes the animation to be found
1. There is something wrong with the animation writing
The ways in which problems arise in the animation itself cannot be listed one by one. Common ones include "duration is set to 0" and "from and to value settings are the same".
<alpha xmlns:andro android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="0.0" android:duration="300" />
<alpha xmlns:andro android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="0" />
2. Set the animation to null in the activity theme, or set the animation to null in the parent theme
as follows:
<style name="TestActivityTheme"> <item name="android:windowNoTitle">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@null</item> </style>
<style name="TestActivityTheme" parent="ParentActivityTheme"> </style> <style name="ParentActivityTheme"> <item name="android:windowNoTitle">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@null</item> </style>
3. The timing of use of overridePendingTransition
The source code comments for overridePendingTransition are as follows:
Call immediately after one of the flavors of startActivity(Intent) or finish to specify an explicit transition animation to perform next.
As of Build.VERSION_CODES.JELLY_BEAN an alternative to using this with starting activities is to supply the desired animation information through a ActivityOptions bundle to startActivity(Intent, Bundle) or a related function. This allows you to specify a custom animation even when starting an activity from outside the context of the current top activity.
Params:
enterAnim – A resource ID of the animation resource to use for the incoming activity. Use 0 for no animation.
exitAnim – A resource ID of the animation resource to use for the outgoing activity. Use 0 for no animation.
There are two usage timings for overridePendingTransition:
- After startActivity
- After finish
as follows:
startActivity(intent); overridePendingTransition(.fade_in, .fade_out);
finish(); overridePendingTransition(.fade_in, .fade_out);
4. OverridePendingTransition Written incorrectly
The wrong place is purely the developer's carelessness. Examples are as follows:
Rewrite the finish method, but the finishAndRemoveTask is called
@Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); initViews(); finishAndRemoveTask(); } @Override public void finish() { (); overridePendingTransition(.fade_in, .fade_out); }
5. The overridePendingTransition in onPause and onResume will overwrite other locations
According to my experience, if overridePendingTransition is written in onPause and onResume, the effect will overwrite the animation set in other places.
For example, when you finish, you set the overridePendingTransition and then also set the overridePendingTransition in onPause, the final effect will be in onPause.
For example, in the following example, after finish, animation is set, and the animation of activity is turned off in onPause, then there is no animation in the end.
@Override protected void onPause() { (); overridePendingTransition(0,0) } @Override public void finish() { (); overridePendingTransition(.fade_in, .fade_out); }
6. Transparency affects animation
For example, if the page itself is transparent and the transparency animation is set, it will look invalid.
7. The plug-in problem causes the animation to be found
If the animation resource cannot be found, it will cause the animation to fail.
In the plug-in scenario, the more special things are:
Some plug-in frameworks load animation resources and need to use the API corresponding to their framework to operate.
The reason is: plug-in frameworks generally change the id of the resource, and the corresponding resource can be found through a fixed API.
In some plug-in frameworks, if you call overridePendingTransition directly to load the animation, the animation resources will not be found, and Android Studio will not report an error.
For example, if the following code is called directly in the plug-in, the resource may not be found, and Android Studio will not report an error.
overridePendingTransition(.fade_in, .fade_out);
This is the article about the reasons and solutions for Android activity animation not taking effect. For more information about Android activity animation, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!