Shared element transition between replaced views

Rya*_*n C 3 android android-animation android-layout android-view android-transitions

I have a view that I'm removing with removeView(), and then a view that is replacing it with an addView(). Both of these views are within a FrameLayout. I would like to be able to do a Shared Element Transition between two shared images in these layouts, but don't really know how to go about doing that as the android tools seems to be built with the (perhaps reasonable) idea that you will only have shared element transitions between activities or fragments.

我目前最好的想法是对动画进行自己的尝试。我会通过将图像的副本绘制到 viewOverlay 上,删除第一个视图,将第二个视图添加到可见的动画到第二个视图中的位置,然后使第二个视图可见来做到这一点。如果可行,我将尝试这条路线并回答我自己的问题,但我希望找到一种更原生的方式来做到这一点。

Rya*_*n C 5

我尝试自己制作动画的想法绝对是错误的路线(尽管它确实有效)。我发现在某些方面,如果您的视图被替换,共享内容实际上会更容易。大多数文档都围绕着使用活动和片段的转换,但它们的核心实际上是围绕更改视图构建的。

这篇文章http://blog.stylingandroid.com/transition-animation-part-1/对我帮助很大。

基本上我正在做的是:

            // above I've gotten the sharedElements (Views)...
            newSharedElement.setTransitionName("sharedProperty");
            oldSharedElement.setTransitionName("sharedProperty");
            // having android:transitionName in the xml can be easier

            // This transition handles the shared element move based on the shared tansition name
            Transition shared = TransitionInflater.from(context).inflateTransition(android.R.transition.move);
            shared.addTarget(newSharedElement);

            // create a scene container is a parentView (FrameLayout) of both children
            Scene scene = new Scene(container, newChild);

            // everything else is set to Fade, shared element is excluded
            Transition fade = TransitionInflater.from(context).inflateTransition(android.R.transition.fade);
            fade.excludeTarget(newSharedElement, true);

            // combine the translations
            TransitionSet set = new TransitionSet();
            set.addTransition(shared).addTransition(fade);

            // translationize
            TransitionManager.go(scene, set);
Run Code Online (Sandbox Code Playgroud)

Android 允许以多种方式执行转换的每个步骤,因此如果这对您的用例不太合适,则有很多选择。

归功于视图之间的共享元素转换(不是活动或片段),也寻求帮助。