20

I have a shared element transition between two activities that works in the following way:

Intent someintent = new Intent(this, someclass.class);

        if (Build.VERSION.SDK_INT >= 21) {

            ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this
                    , new Pair<>(viewClicked.findViewById(R.id.someimage), "someimage")
                    , new Pair<>(viewClicked.findViewById(R.id.someicon), "someicon")
            );
            startActivity(someintent, options.toBundle());
        }
        else {
            startActivity(someintent);
        }

this works fine, but the transition is agonisingly slow. When the image is first clicked on it seems to stall for a second or two before the transition takes place. Is this due to the "weight" of the activity being loaded or is the delay configurable?

Jon
  • 7,941
  • 9
  • 53
  • 105
  • What view are you transitioning to? I have seen delays when I am using an image that needs to be grabbed from the web - and in this case, I delay the enter transition (part of the API) until the image has been fully downloaded/displayed. – Booger Jul 14 '15 at 12:53
  • @Booger I'm transitioning from an activity that holds a recyclerview of images to another activity containing a fragment that has the same image inside it. The view itself is a regular ImageView. – Jon Jul 14 '15 at 12:55
  • http://www.androiddesignpatterns.com/2015/03/activity-postponed-shared-element-transitions-part3b.html. – Sakiboy Feb 28 '17 at 02:19

2 Answers2

23

Did you try change duration of enterTransition and returntransition:

    private Transition enterTransition() {
        ChangeBounds bounds = new ChangeBounds();
        bounds.setDuration(2000);

        return bounds;
    }

    private Transition returnTransition() {
        ChangeBounds bounds = new ChangeBounds();
        bounds.setInterpolator(new DecelerateInterpolator());
        bounds.setDuration(2000);

        return bounds;
    }

And in onCreate:

getWindow().setSharedElementEnterTransition(enterTransition());
getWindow().setSharedElementReturnTransition(returnTransition());
Aleksandr
  • 4,906
  • 4
  • 32
  • 47
  • 1
    thanks - I tried using this both in the fromActivity and in the toActivity in the transition and it doesn't affect the speed. So, if this is the only way to change the transition times then I'm guessing that the weight of the fragment is causing the delay. Many thanks for your help – Jon Jul 14 '15 at 13:07
17

Another way that may help some that already have the Transitions setup in their styles:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    getWindow().getSharedElementEnterTransition().setDuration(2000);
    getWindow().getSharedElementReturnTransition().setDuration(2000)
        .setInterpolator(new DecelerateInterpolator());
}
Codeversed
  • 9,287
  • 3
  • 43
  • 42
  • i got crash if set ur code: window.sharedElementEnterTransition must not be null – famfamfam Mar 15 '21 at 15:50
  • @famfamfam You need to make sure you have a proper setup for your transition. Hard to say without seeing the code. You could add a null check or write this in Kotlin to avoid this error but it will not resolve the issue. – Codeversed Mar 17 '21 at 00:40