2

I have a MainActivity holding 2 fragments - MainFragment and SlideshowFragment. Both fragments are using ViewPager with FragmentStatePagerAdapter to hold childFragments.

When an ImageView in ChildFragmentA of MainFragment is clicked, it is to expand and fill the PhotoView in ChildFragmentB of SlideshowFragment. The ImageView is held in a RecyclerView.

I've referred to the Android Official Guide here, looked that its GitHub code and had made the required change to make sure the transition names and callback are properly set.

However, ImageView is using centerCrop in xml, while PhotoView is using fitCenter. The result is that before the transition happen, the image will expand first, which is very unpleasant. Gif here (irrelevant, but appreciate if someone can let me know how to embed gif).

Below are some of the relevant codes.

Loading of image into view with 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)

Preparation of transition in 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>

Really appreciate any help as I have spent 2 days on this.

tingyik90
  • 1,641
  • 14
  • 23
  • @pskink, is there any resource that you can point me to or perhaps some code to show? Most of the sites that I found are just re-telling the story of what I found on Android Official Guide. – tingyik90 Apr 23 '18 at 15:07
  • Do we have an easier method using xml? – tingyik90 Apr 23 '18 at 15:21
  • @pskink, well... tried that and was not working. There's no issue with the viewtype, but the scale type I think. – tingyik90 Apr 24 '18 at 03:14
  • I am facing the same problem. Did you solve this issue ? – Paul Souteyrat Mar 09 '20 at 11:44
  • @PaulSouteyrat, unfortunately no. In the end, I used Activity to Activity transition: Fragment A in Activity A --> Fragment B in Activity B. That then worked properly. I have open-sourced my app at https://github.com/tingyik90/Pholder. You can get try my app at https://play.google.com/store/apps/details?id=com.dokidevs.pholder. – tingyik90 Mar 10 '20 at 12:23
  • There are a lot of comments within the code. You should look into `startSlideshow()` method of `GalleryBaseActivity` and follow the method calls. – tingyik90 Mar 10 '20 at 12:25

0 Answers0