Last talked about the fragment animation loading of the exception, today, and then talk about its animation load loadanimation implementation of the source code:
Animation loadanimation (Fragment Fragment, int Transit, Boolean enter, int transitionstyle) {
Next specific look inside the source code section, I will part of the explanation, the first is:
Animation Animobj = fragment.oncreateanimation (Transit, enter, Fragment.mnextanim); if (animobj! = null) { return animobj; }
The beginning of this part, will call fragment Oncreateanimation this method, in the fragment class, this method is empty, and there is no reality:
/** * Called when a fragment loads a animation. * /public Animation oncreateanimation (int Transit, boolean enter, int nextanim) { return null; }
If you write a fragment subclass that overrides this method after inheriting fragment, you can get animations here.
If you do not rewrite this method, it will go down:
if (Fragment.mnextanim! = 0) { Animation anim = animationutils.loadanimation (mactivity, Fragment.mnextanim); if (anim! = null) { return anim; } }
Fragment.mnextanim This is the animation that was set with the fragmenttransaction when we added or replaced fragment, as in the example in the previous article:
Fragmenttransaction ft = Getfragmentmanager (). BeginTransaction (); Ft.setcustomanimations (R.animator.fragment_rotate_enter, r.animator.fragment_rotate_exit, R.animator.fragment_rotate_pop_enter, r.animator.fragment_rotate_pop_exit);
The specific is to enter the animation, or quit the animation, it depends on the specific situation at that time, this is not necessary for developers to worry about.
If we haven't defined a toggle animation before, well, let's see if we set the transit and Transitionstyle parameters:
if (transit = = 0) { return null; } int styleindex = Transittostyleindex (transit, enter); if (Styleindex < 0) { return null; }
Look again at the Transittostyleindex method:
public static int Transittostyleindex (int transit, Boolean enter) { int animattr =-1; Switch (transit) {case fragmenttransaction.transit_fragment_open: animattr = enter? Anim_style_open_enter:anim_style_open_exit; break; Case Fragmenttransaction.transit_fragment_close: animattr = enter? Anim_style_close_enter:anim_style_close_exit; break; Case Fragmenttransaction.transit_fragment_fade: animattr = enter? Anim_style_fade_enter:anim_style_fade_exit; break; } return animattr; }
Therefore, the value of TRANSIT can only be: Fragmenttransaction.transit_fragment_open,fragmenttransaction.transit_fragment_close, Fragmenttransaction.transit_fragment_fade. Then, based on this value, the system's own animation file is called:
Switch (styleindex) {case anim_style_open_enter: return makeopencloseanimation (mactivity, 1.125f, 1.0f, 0, 1) ; Case Anim_style_open_exit: return makeopencloseanimation (mactivity, 1.0f,. 975f, 1, 0); Case Anim_style_close_enter: return Makeopencloseanimation (mactivity,. 975f, 1.0f, 0, 1); Case Anim_style_close_exit: return makeopencloseanimation (mactivity, 1.0f, 1.075f, 1, 0); Case Anim_style_fade_enter: return makefadeanimation (mactivity, 0, 1); Case Anim_style_fade_exit: return Makefadeanimation (mactivity, 1, 0); }
According to the previous logic, the code runs here and it should be finished. But the method, followed by some code:
if (Transitionstyle = = 0 && mactivity.getwindow () = null) { Transitionstyle = Mactivity.getwindow (). GetAttributes (). windowanimations; } if (Transitionstyle = = 0) { return null; }
Comparing the same fragmentmanager in the original package, the following code is still somewhat different over there:
int styleindex = Transittostyleindex (transit, enter); if (Styleindex < 0) { return null; } if (Transitionstyle = = 0 && mactivity.getwindow () = null) { Transitionstyle = Mactivity.getwindow (). GetAttributes (). windowanimations; } if (Transitionstyle = = 0) { return null; } TypedArray attrs = mactivity.obtainstyledattributes (Transitionstyle, com.android.internal.r.styleable.fragmentanimation); int anim = Attrs.getresourceid (styleindex, 0); Attrs.recycle (); if (Anim = = 0) { return null; } Return Animatorinflater.loadanimator (mactivity, anim);
So, transitionstyle This parameter, the V4 package here is meaningless.
All right, fragment, this animation loads the content of the method.