在具有共享元素的片段上输入过渡以共享元素为目标 [英] Enter transition on a fragment with a shared element targets the shared element

查看:105
本文介绍了在具有共享元素的片段上输入过渡以共享元素为目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在片段上使用新的Lollipop api到setEnterTransition,然后在片段中为图像添加共享元素过渡.所需的行为是,首先,图像应移动到其位置,然后片段中的其余视图应淡入.

I am using the new Lollipop api to setEnterTransition on a fragment and then add a shared element transition for an image in the fragment. The desired behavior is first, the image should move to its position, after which the rest of the views in the fragment should fade in.

但是,enterTransition已应用于共享元素,因此它与其余视图一起逐渐消失.如果未设置enterTransition,则图像会正确移动,但是在移动其他内容时它已经可见.

However, the enterTransition is being applied to the shared element so it is fading in with the rest of the views. If I don't set the enterTransition, then the image moves properly but it while it is moving the other content is already visible.

我如何不将enterTransition应用于共享视图?

How do I get it to not apply the enterTransition to the shared view?

我发现此AOSP中的提交似乎可以解决此问题,但它似乎不起作用.

I found this commit in the AOSP that seems like it should address this issue, but it doesn't seem to be working.

这是示例代码:

public class Fragment1 extends Fragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.main_fragment, container, false);
    final ImageView imageView = (ImageView) rootView.findViewById(R.id.image);
    final Button button = (Button) rootView.findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        TransitionSet transitionSet = new TransitionSet();
        transitionSet.addTransition(new ChangeImageTransform());
        transitionSet.addTransition(new ChangeBounds());
        transitionSet.setDuration(300);

        Fragment fragment2 = new Fragment2();
        fragment2.setSharedElementEnterTransition(transitionSet);
        fragment2.setSharedElementReturnTransition(transitionSet);
        Fade fade = new Fade();
        fade.setStartDelay(300);
        fragment2.setEnterTransition(fade);

        getFragmentManager().beginTransaction()
            .replace(R.id.container, fragment2)
            .addSharedElement(imageView, "SharedImage")
            .commit();
      }
    });
    return rootView;
  }
}

推荐答案

Enter过渡不应应用共享元素视图.最可能的情况是,您的共享元素位于具有背景的另一个视图中,从而使该视图受enter转换的影响.这种情况是这样的:

The enter transition should not apply the the shared element views. The most likely scenario is that your shared element is within another view with a background, making that view affected by the enter transition. That's a situation like this:

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FFF"
    >
    <ImageView android:src="@drawable/pretty_picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:transitionName="picture"
        android:padding="20dp"/>
</FrameLayout>

在这里,ImageView是共享元素.

Here, the ImageView is the shared element.

如果发生这种情况,您将要添加一个神奇的过渡:ChangeTransform.如果检测到父项发生更改,它将从父项中拉出共享元素并分别进行过渡.

If that happens, you'll want to add a magic transition: ChangeTransform. If it detects that the parent changes, it will pull out the shared element from the parent and transition it separately.

这篇关于在具有共享元素的片段上输入过渡以共享元素为目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆