Where is the documentation for Fragment.onCreateAnimator()? [closed]

自作多情 提交于 2019-12-03 23:25:33

问题


The entire documentation for the Fragment.onCreateAnimator(int, boolean, int) method consists of the following text:

"Called when a fragment loads an animation."

That's it. No explanation about the parameters.

What do the parameters mean? Even the source code doesn't reveal much.


回答1:


The onCreateAnimator method is odd. The prototype I've seen is this:

public Animator onCreateAnimator(int transit, boolean enter, int nextAnim)

int transit - transition type, as sandrstar said above

boolean enter - true if 'entering,' false otherwise

int nextAnim - The resource ID of the animation that is about to play.

So, for example, if you tried doing a card flip, from the documentation:

// Create and commit a new fragment transaction that adds the fragment for the back of
// the card, uses custom animations, and is part of the fragment manager's back stack.
BackOfCardFragment backFragment = new BackOfCardFragment();

getFragmentManager()
    .beginTransaction()

    // Replace the default fragment animations with animator resources representing
    // rotations when switching to the back of the card, as well as animator
    // resources representing rotations when flipping back to the front (e.g. when
    // the system Back button is pressed).
    .setCustomAnimations(
         R.animator.card_flip_right_in, R.animator.card_flip_right_out,
         R.animator.card_flip_left_in, R.animator.card_flip_left_out)

    // Replace any fragments currently in the container view with a fragment
    // representing the next page (indicated by the just-incremented currentPage
    // variable).
    .replace(R.id.container_view, backFragment)

    // Add this transaction to the back stack, allowing users to press Back
    // to get to the front of the card.
    .addToBackStack(null)

    // Commit the transaction.
    .commit();

NOTE: R.id.container_view in the above example is the ID of a ViewGroup that contains the existing fragment you are trying to replace.

When the above code is executed, the onCreateAnimator method will get called, and the nextAnim parameter will be one of the four animation IDs passed into the setCustomAnimations() function, i.e. R.animator.card_flip_right_in, R.animator.card_flip_right_out... etc.

This doesn't seem immediately useful at first, since it doesn't give you a reference to the actual Animator object that you could attach a listener to. But oddly, you can just inflate another Animator directly from the nextAnim resource, and then attach listeners to that, which will, oddly, fire all the overridden callbacks at the right times:

@Override
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
    Animator animator = null;
    // In this example, i want to add a listener when the card_flip_right_in animation
    // is about to happen.
    if (nextAnim == R.animator.card_flip_right_in) {
        animator = AnimatorInflater.loadAnimator(getActivity(), nextAnim);
        // * Sometimes onCreateAnimator will be called and nextAnim will be 0, 
        //   causing animator to be null.
        // * I wanted to add a listener when the fragment was entering - 
        //   your use case may be different.
        if (animator != null && enter) {

            animator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                   // Do something when the card flip animation begins
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                   // Do something as soon as the card flip animation is over
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                }

                @Override
                public void onAnimationRepeat(Animator animation) {
                }
            });
        }
    }
    return animator;
}

In this way, you should be able to add listeners to the fragment transition animators as if you had created them yourself.




回答2:


Based on FragmentManager code and usages of FragmentManagerImpl.loadAnimator(android.app.Fragment,int,boolean,int) it seems that Fragment.onCreateAnimator(int, boolean, int) lets You define own animations for fragment hiding, showing, changing state. However, I've never seen usage of it in real apps.

Regarding parameters:

  • int transit - transition type (constants FragmentTransaction, e.g. used in here);
  • boolean enter - true if it's state enter, false - otherwise;
  • int transitionStyle - id of style from resources (that style might contain animations missed from onCreateAnimator);


来源:https://stackoverflow.com/questions/16910510/where-is-the-documentation-for-fragment-oncreateanimator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!