Android Glide library not working with shared element transitions

前端 未结 2 407
青春惊慌失措
青春惊慌失措 2020-12-06 17:38

I\'m looking to adopt the Glide library in place of Universal Image Loader but I\'m running into a problem with regards to shared element transitions.

In my simple S

相关标签:
2条回答
  • 2020-12-06 18:00

    For those of you that are struggling with the SharedElementTransition using Glide I think I got a workaround that works, or at least it worked for me. In your second activity or fragment use this code:

    postponeEnterTransition();
    GlideApp.with(this)
            .load(imageToLoad)
            .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    return false;
                }
    
                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    startPostponedEnterTransition();
                    return false;
                }
            })
            .into(imageView);
    

    If you are targeting API<21 use:

    supportPostponeEnterTransition();
    supportStartPostponedEnterTransition();
    

    Hope that helps!

    0 讨论(0)
  • 2020-12-06 18:01

    I tried your code and got the same result, then I tried another implementation following this: https://github.com/codepath/android_guides/wiki/Shared-Element-Activity-Transition

    And made some changes, which I think are the key here:

    The centerCrop should be set on the ImageView, if we set it with Glide it causes the same bug. activity_main.xml

        <ImageView
          android:id="@+id/ImageView"
          android:layout_width="match_parent"
          android:layout_height="150dp"
          android:layout_centerInParent="true"
          android:transitionName="detail_image"
          android:scaleType="centerCrop"
        />
    

    BaseActivity.java On the MainActivity dontTransform should be true and on DifferentActivity false.

        public void displayImageGlide(String url, ImageView imageView, Boolean dontTransform) {
        if (dontTransform) {
            Glide.with(this).load(url)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .dontTransform()
                    .into(imageView);
            return;
        }
    
            Glide.with(this).load(url)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(imageView);
       }
    

    Edit, see how it looks: https://www.dropbox.com/s/30s5l8awxogltls/device-2015-06-23-153153.mp4?dl=0

    0 讨论(0)
提交回复
热议问题