首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Fragment.onCreateAnimator()的文档在哪里?

Fragment.onCreateAnimator()的文档在哪里?
EN

Stack Overflow用户
提问于 2013-06-04 13:38:06
回答 2查看 6.2K关注 0票数 20

Fragment.onCreateAnimator(int, boolean, int)方法的完整文档由以下文本组成:

“在片段加载动画时调用。”

就这样。没有关于参数的解释。

这些参数意味着什么?Even the source code doesn't reveal much.

EN

回答 2

Stack Overflow用户

发布于 2013-12-10 05:00:48

onCreateAnimator方法很奇怪。我看到的原型是这样的:

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

int transit -如sandrstar上面所说的转换类型

boolean enter -如果为'entering‘,则为true,否则为false

int nextAnim -即将播放的动画的资源ID。

举个例子,如果你试着翻牌,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();

注意:上例中的R.id.container_view是包含您试图替换的现有片段的ViewGroup的ID。

当执行上述代码时,将调用onCreateAnimator方法,并且nextAnim参数将是传递给setCustomAnimations()函数的四个动画is之一,即R.animator.card_flip_right_in、R.animator.card_flip_right_out...等。

乍一看,这似乎不是很有用,因为它不会为您提供对可以附加侦听器的实际Animator对象的引用。但奇怪的是,您可以直接从nextAnim资源中膨胀另一个Animator,然后将侦听器附加到该动画中,奇怪的是,它将在正确的时间触发所有被覆盖的回调:

@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;
}

通过这种方式,您应该能够将侦听器添加到片段转换动画器中,就像您自己创建它们一样。

票数 16
EN

Stack Overflow用户

发布于 2013-06-04 13:59:09

基于FragmentManager代码和FragmentManagerImpl.loadAnimator(android.app.Fragment,int,boolean,int)的用法,Fragment.onCreateAnimator(int, boolean, int)似乎可以让你定义自己的动画来隐藏,显示,改变状态。然而,我从来没有见过它在真正的应用程序中的使用。

关于参数:

  • int transit -转换类型(常量FragmentTransaction,例如,如果其状态为enter,则在otherwise;
  • int transitionStyle - true中使用,false -来自资源的样式的false- style - id (该样式可能包含从onCreateAnimator);

中遗漏的动画

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16910510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档