共享元素转换:将活动转换为嵌套在另一个活动中的片段 [英] Shared element transition : activity into fragment nested in another activity

查看:132
本文介绍了共享元素转换:将活动转换为嵌套在另一个活动中的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将共享元素过渡添加到我的应用中.

i am trying to add shared element transition into my app.

方案是用户单击图像缩略图,然后打开另一个具有全屏图像视图的活动.

如果共享视图直接托管在目标活动的布局内,则此方法很好用.流畅地进行输入/退出动画. 但是,当我尝试在目标活动的嵌套内的片段中实现类似效果时,该方法不起作用.有趣的是,没有显示进入动画,但是退出动画可以正常工作.

This works fine if shared view is hosted directly within layout of target activity. Works smoothy for enter/exit animation. But when i'am trying to achieve similar effect within fragment which is nested in target activity this approach doesn't work. Funny thing is that enter animation is not showed, but exit animation is working fine .

另一个更为复杂的视图层次结构是,如果目标视图(ImageView)托管在视图分页器中,而该视图分页器则托管在目标活动的框架布局中.

Another even more complicated view hierarchy is that if target view (ImageView) is hosted within view pager which is hosted in frame layout of target activity.

有人遇到同样的问题吗?

Does someone had same issue ?

我的点击监听器代码

public class OnClickPicture extends OnClickBase {
  private ObjectPicture object;

  public OnClickPicture(Activity_Parent activity, ObjectPicture object) {
    super(activity);
    this.object = object;
  }

  public void onClick(View v) {

    picasso.load(object.getFullUrl()).fetch();
    Intent intent = new Intent(activity, ActivityPicture.class);
    intent.putExtra("picture_object", helper.gson.toJson(object));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && v != null) {
      Pair<View, String> p1 = Pair.create(v, "image");
      ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, p1);
      activity.startActivity(intent, options.toBundle());
    } else {
      activity.startActivity(intent);
    }

  }

}

推荐答案

过渡工作的方式要求在发生任何动画之前先创建,测量和布置新的Activity.这样一来,它就可以找到要设置动画的视图并创建适当的动画.

The way that transitions work require the new Activity to be created, measured and laid out before any animations can happen. That's so that it can find the view that you want to animate and create the appropriate animation.

在您的情况下,这不会发生,因为文档中所述的 a>,所有FragmentTransaction.commit()所做的只是安排要完成的工作.它不会立即发生.因此,当框架创建您的Activity时,它找不到您要设置动画的视图.这就是为什么您看不到入口动画,但是却看到出口动画的原因.离开活动时,视图就在那里.

In your case this isn't happening because, as stated in the docs, all FragmentTransaction.commit() does is schedule work to be done. It doesn't happen immediately. Therefore when the framework creates your Activity it cant find the view that you want to animate. That's why you don't see an entry animation but you do see an exit animation. The View is there when you leave the activity.

解决方案非常简单.首先,您可以尝试 FragmentManager.executePendingTransactions().那可能还不够.过渡框架还有另一个解决方案:

The solution is simple enough. First of all you can try FragmentManager.executePendingTransactions(). That still might not be enough. The transitions framework has another solution:

在活动的onCreate中 postponeEnterTransition().这告诉框架等待,直到您告诉它可以安全地创建动画.那确实意味着您需要告诉它它的安全性(通过调用 startPostponedEnterTransition()).在您的情况下,可能在Fragments onCreateView中.

In the onCreate of Activity postponeEnterTransition(). This tells the framework to wait until you tell it that its safe to create the animation. That does mean that you need to tell it that its safe (via calling startPostponedEnterTransition()) at some point. In your case that would probably be in the Fragments onCreateView.

这是一个看起来像这样的例子:

Here's an example of how that might look like:

活动B

@Override
protected void onCreate(Bundle savedInstanceState) {
    // etc
    postponeEnterTransition();
}

片段B

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View sharedView = root.findViewById(R.id.shared_view);
    sharedview.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            sharedview.getViewTreeObserver().removeOnPreDrawListener(this);
            getActivity().startPostponedEnterTransition();
            return true;
        }
    });
}

感谢Alex Lockwood的有关该主题的详细博客文章过渡框架.

Thanks to Alex Lockwood for his detailed blog posts about the Transitions framework.

这篇关于共享元素转换:将活动转换为嵌套在另一个活动中的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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