【问题标题】:How to implement fragment shared element for pre Lollipop?如何为 pre Lollipop 实现片段共享元素?
【发布时间】:2016-07-23 05:48:28
【问题描述】:

我在包含图像的片段中有一个回收器视图。我实现了 OnImageCLickListener 在单击图像后,将打开一个全屏对话框片段并显示图像。现在我想在回收器视图中的图像和对话框片段中图像的全屏对话框之间实现共享元素转换,我还希望在 Lollipop 之前支持共享元素。

我该怎么做?

【问题讨论】:

    标签: android android-fragments shared-element-transition


    【解决方案1】:

    首先你必须得到点击的 imageView 的确切位置,并将其传递给片段: 要获得位置,您可以使用:

     int[] location = {0,0};
     mImageView.getLocationOnScreen(location);
    

    并将其传递给您的片段,您可以使用:

    Bundle bundle = new Bundle();
    bundle.putInt("locationX",location[0]);
    bundle.putInt("locationY",location[1]);
    

    并通过以下方式将其放入您的片段中:

    locationX = getArguments().getInt("locationX");
    locationY = getArguments().getInt("locationY");
    

    注意:要获得该位置,永远不要使用 view.getTop()、view.getRight() 等方法。 现在你需要在片段 OnCreateView 中像这样的简单动画:

    float factor = ((float) (Utils.getWidth(getActivity())) / ((float) (pictureWidth)));
    
        viewPager.getLayoutParams().height = pictureHeight;
        viewPager.getLayoutParams().width = pictureWidth;
        viewPager.setTranslationY(locationY);
        viewPager.setTranslationX(locationX);
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
            viewPager.animate()
                    .translationY(Utils.getHeight(getActivity()) / 2 - pictureHeight / 2)
                    .scaleX(factor)
                    .scaleY(factor)
                    .translationX(Utils.getWidth(getActivity()) / 2 - pictureWidth / 2);}
    

    它也支持pre-Lollipop。

    【讨论】:

      猜你喜欢
      • 2015-08-26
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 2015-01-15
      • 2015-01-13
      相关资源
      最近更新 更多