Search code examples
javaandroidandroid-activityandroid-recyclerviewshared-element-transition

android shared element transition start transition has delay


I'm using the shared element transition with a recyclerview, as you can see in this gif:

enter image description here

there is a delay starting the shared element transition, It's not about the animation's speed, it's like a loading delay at the start, if you notice, there is a moment where the whole background disappear and just the image remains there, that moment is what I want to remove, that is the delay I talked about. How can I remove it and make the whole thing more smoother? it's really ugly right now, it's my first time with this animation so If you have advice I'd be thankful.

This is the code that handle the shared element transition inside my adapter:

holder.image.setTransitionName("example_transition");
            Intent intent = new Intent(context, ShowElementActivity.class);
            intent.putExtra("Element", elemento);
            ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
                    (AnimeActivity)context, holder.image, ViewCompat.getTransitionName(holder.image));
            context.startActivity(intent, options.toBundle());

This is the code that handle it inside the destination:

postponeEnterTransition();
    Glide.with(this)
            .load(elemento.image_url)
            .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    supportStartPostponedEnterTransition();
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    startPostponedEnterTransition();
                    return false;
                }
            })
            .into(showelement_image);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().getSharedElementEnterTransition().setDuration(200);
        getWindow().getSharedElementReturnTransition().setDuration(200)
                .setInterpolator(new DecelerateInterpolator());
    }
    Fade fade = new Fade();
    fade.excludeTarget(android.R.id.statusBarBackground, true);
    fade.excludeTarget(android.R.id.navigationBarBackground, true);
    getWindow().setEnterTransition(fade);
    getWindow().setExitTransition(fade);

Solution

  • This delay is cost of new Activity not cost of Transition, if you don't use Animation you will see same performance. So use Fragment rather than Activity.

    Because of new Activity performance, I am developing applications via Single Activity with Multiple Fragments for best performance.