问题描述
我在属于一个Activity的片段中有一个共享元素。
I have a shared element in a fragment that belongs to one Activity.
我想在Android Lollipop中使用元素的一部分进行共享元素转换
I want to make a shared element transition in Android Lollipop with an element that is part of a fragment that belongs to another activity.
有可能吗?
如何实现?
推荐答案
有可能。
首先,当您在片段中检测到过渡即将发生,构建一个 Pair< View,String>
数组,并在其中填充视图和过渡名称。
First, when you detect in your fragment that transition is about to happen, build a array of Pair<View, String>
which you populate with view and transition name.
例如,如果要将动画从缩略图图像转换为全角图像:
For example, if you want to animate from thumbnail image to full width image:
Pair[] pairs = new Pair[1];
pairs[0] = new Pair(thumbnailImage, "THUMBNAIL_IMAGE");
第二步,将该数组传递给fragment的活动,以便可以启动实际的过渡。 (我正在使用Otto传递该事件,您可以根据需要使用常规回调。)
Second, pass that array to fragment's activity so it can initiate the actual transition. (I'm using Otto to pass that event up, you can use usual callbacks if you like).
然后在您的活动中,开始第二个活动。 (我为此创建了一个简单的方法)
Then, in your activity, start the second activity. (I created a simple method for doing that)
public static void transitionExpand(Activity activity, Intent intent, Pair<View, String>[] sharedElements) {
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(activity, sharedElements);
ActivityCompat.startActivity(activity, intent, options.toBundle());
}
在第二个活动中,可以按常规方式添加片段。然后,在第二个片段的 onViewCreated()
方法中,您可以调用:
In your second activity, you can add the fragment in the usual way. Then, in second fragment's onViewCreated()
method, you can call:
ViewCompat.setTransitionName(fullWidthImage, "THUMBNAIL_IMAGE");
希望有帮助
这篇关于属于Android棒棒糖中不同活动的片段之间的共享元素过渡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!