ImageView SharedElement在转换之前调整大小

时间:2018-04-23 15:01:47

标签: android kotlin imageview shared-element-transition

我有MainActivity个2个片段 - MainFragmentSlideshowFragment。这两个片段都使用ViewPager FragmentStatePagerAdapter来保留childFragments

如果点击ImageView ChildFragmentA MainFragment,则展开并填充PhotoView ChildFragmentB SlideshowFragmentImageView保存在RecyclerView

我已经参考了Android官方指南here,查看了它的GitHub代码,并进行了必要的更改,以确保正确设置转换名称和回调。

但是,ImageView在xml中使用centerCrop,而PhotoView正在使用fitCenter。结果是在转换发生之前,图像将首先扩展,这是非常不愉快的。 Gif here(无关紧要,但如果有人能让我知道如何嵌入gif,我会很感激。)

以下是一些相关的代码。

使用Glide

将图像加载到视图中
GlideApp.with(getActivityContext())
    .load(filePath)
    .dontTransform()    // I found some posts that say this should be set
    .listener(object : RequestListener<Drawable> {
        override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?,
                                  isFirstResource: Boolean): Boolean {
            startPostponedEnterTransition()
            return false
        }

        override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?,
                                     dataSource: DataSource?, isFirstResource: Boolean): Boolean {
            startPostponedEnterTransition()
            return false
        }
    })
    .into(imageView)

在SlideshowFragment中准备转换

private fun prepareSharedElementTransition() {
    val transition = TransitionInflater.from(getActivityContext())
            .inflateTransition(R.transition.image_shared_element_transition)
    transition.duration = 1000
    sharedElementEnterTransition = transition
    sharedElementReturnTransition = transition    // it wasn't working, so I tried adding this line too
    setEnterSharedElementCallback(
            object : SharedElementCallback() {
                override fun onMapSharedElements(names: List<String>, sharedElements: MutableMap<String, View>) {
                    // mapping stuff here done correctly
                }
            })
}

XML

<!-- In ChildFragmentA -->
<ImageView
    android:id="@+id/thumbnailLayout_thumbnail"
    android:layout_width="match_parent"
    android:adjustViewBounds="true"
    android:layout_height="match_parent"
    android:scaleType="centerCrop" />

<!-- In ChildFragmentB -->
<com.github.chrisbanes.photoview.PhotoView
    android:id="@+id/childFragmentB_photoView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
    android:layout_centerInParent="true"
    android:scaleType="fitCenter" />

<!-- In image_shared_element_transition.xml -->
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:interpolator/fast_out_slow_in"
    android:transitionOrdering="together">
    <changeBounds/>
    <changeClipBounds/>
    <changeTransform/>
</transitionSet>

非常感谢任何帮助,因为我花了2天时间。

0 个答案:

没有答案