如何知道共享元素转换何时结束

时间:2015-11-10 23:39:10

标签: android shared-element-transition

我在活动之间与Shared Element Transitions合作。转换工作正常,但我想知道共享元素转换何时结束,以便我可以显示其他内容。

我尝试在onSharedElementEnd中使用SharedElementCallback在我要转换的活动中,但在转换开始之前会调用它。

我可以收听另一个回调吗?

5 个答案:

答案 0 :(得分:31)

您是否尝试将动画侦听器绑定到onMapSharedElements内的共享元素视图? ViewCompat.animate(view)将为您提供新的或缓存的ViewPropertyAnimator(Compat),然后绑定动画侦听器应该是微不足道的。不过,我还没试过。

setEnterSharedElementCallback(new SharedElementCallback() {
            @Override
            public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
                super.onMapSharedElements(names, sharedElements);
                View keySharedElementView = sharedElements.get("keySharedElement");
                if(keySharedElementView != null){
                    ViewCompat.animate(keySharedElementView).setListener(new ViewPropertyAnimatorListenerAdapter(){
                        @Override
                        public void onAnimationEnd(View view) {
                            super.onAnimationEnd(view);
                        }
                    });
                }
            }
        });

如何将Transition.Listener添加到共享元素转换?

 Transition sharedElementEnterTransition = getWindow().getSharedElementEnterTransition();
 sharedElementEnterTransition.addListener(new TransitionListenerAdapter() {
         @Override
          public void onTransitionEnd(android.support.transition.Transition transition) {
                    super.onTransitionEnd(transition);
           }
      });

答案 1 :(得分:18)

请尝试onEnterAnimationComplete()回调您的活动。

我敢打赌,这正是你所寻找的。

 @Override
    public void onEnterAnimationComplete() {
        super.onEnterAnimationComplete();

    //your code 
    }

答案 2 :(得分:5)

以下是我在片段中所做的事情:

Transition sharedElementEnterTransition = getActivity().getWindow().getSharedElementEnterTransition();
    sharedElementEnterTransition.addListener(new Transition.TransitionListener() {
        @Override
        public void onTransitionStart(Transition transition) {

        }

        @Override
        public void onTransitionEnd(Transition transition) {

        }

        @Override
        public void onTransitionCancel(Transition transition) {

        }

        @Override
        public void onTransitionPause(Transition transition) {

        }

        @Override
        public void onTransitionResume(Transition transition) {

        }
    });

答案 3 :(得分:0)

尝试在Fragment A上添加TransitionListener,而不是B.并使用android.support.transition.Transition

片段A

            val anim = DetailsTransition()
            anim.addListener(object : android.support.transition.Transition.TransitionListener
            {
                override fun onTransitionEnd(transition: android.support.transition.Transition) {
                }

                override fun onTransitionResume(transition: android.support.transition.Transition) {
                }

                override fun onTransitionPause(transition: android.support.transition.Transition) {
                }

                override fun onTransitionCancel(transition: android.support.transition.Transition) {
                }

                override fun onTransitionStart(transition: android.support.transition.Transition) {
                }

            })

答案 4 :(得分:0)

我在Kotlin中使用导航组件努力做到这一点,但设法使其与之配合工作

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    setAndPostponeEnterAnimation()
    _binding = PokemonDetailFragmentBinding.inflate(inflater, container, false)
    setInsets()
    handleNavigationArgs()
    return binding.root
}

private fun setAndPostponeEnterAnimation() {
    postponeEnterTransition()
    sharedElementEnterTransition = TransitionInflater.from(context)
        .inflateTransition(R.transition.shared_element_transition)
    addSharedElementListener()
}

private fun addSharedElementListener() {
    (sharedElementEnterTransition as TransitionSet).addListener((object :
        TransitionListenerAdapter() {
        override fun onTransitionEnd(transition: Transition) {
            super.onTransitionEnd(transition)
            createRevealAnimation()
        }
    }))
}

所以我可以等到共享过渡完成后再开始制作新动画