Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared element transition among fragments that belong to different activities in Android lollipop

I have a shared element in a fragment that belongs to one Activity.

I want to make a shared element transition in Android Lollipop with an element that is part of a fragment that belongs to another activity.

Is it possible?

How can I achieve that?

like image 727
Matroska Avatar asked Dec 17 '14 11:12

Matroska


People also ask

Which of the following transitions is a shared elements transition in Android?

Android also supports these shared elements transitions: changeBounds - Animates the changes in layout bounds of target views. changeClipBounds - Animates the changes in clip bounds of target views. changeTransform - Animates the changes in scale and rotation of target views.

How to animate fragments in android?

To animate the transition between fragments, or to animate the process of showing or hiding a fragment you use the Fragment Manager to create a Fragment Transaction . Within each Fragment Transaction you can specify in and out animations that will be used for show and hide respectively (or both when replace is used).

What is transition animation in Android?

Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.

How do you animate fragment transitions?

At a high level, here's how to make a fragment transition with shared elements: Assign a unique transition name to each shared element view. Add shared element views and transition names to the FragmentTransaction . Set a shared element transition animation.


2 Answers

UPDATE: As of v25.1.1 of the support library, these same methods are in the support Fragments. Links to the docs: Fragment.postponeEnterTransition() and Fragment.startPostponedEnterTransition()

ORIGINAL ANSWER:

It's possible, even with a dynamically added Fragment in the second Activity.

You just need to tell the second Activity not to run its Transition animations until the shared elements have been laid out and measured.

In the onCreate of the second Activity call postponeEnterTransition() (or supportPostponeEnterTransition() if you're using the Support Library). Dynamically add your Fragment to this Activity. At the end of the onCreateView method in the Fragment that you're dynamically adding, call getActivity().startPostponedEnterTransition().

This of course assumes you've done everything else required for a shared element transition, but I believe these methods are what you're searching for with your question.

Credit to @alex-lockwood's blog for showing me the light.

like image 124
pumpkinpie65 Avatar answered Oct 29 '22 23:10

pumpkinpie65


It's possible.

First, when you detect in your fragment that transition is about to happen, build a array of Pair<View, String> which you populate with view and transition name.

For example, if you want to animate from thumbnail image to full width image:

  Pair[] pairs = new Pair[1];
  pairs[0] = new Pair(thumbnailImage, "THUMBNAIL_IMAGE");

Second, pass that array to fragment's activity so it can initiate the actual transition. (I'm using Otto to pass that event up, you can use usual callbacks if you like).

Then, in your activity, start the second activity. (I created a simple method for doing that)

public static void transitionExpand(Activity activity, Intent intent, Pair<View, String>[] sharedElements) {
        ActivityOptionsCompat options =
            ActivityOptionsCompat.makeSceneTransitionAnimation(activity, sharedElements);

        ActivityCompat.startActivity(activity, intent, options.toBundle());
}

In your second activity, you can add the fragment in the usual way. Then, in second fragment's onViewCreated() method, you can call:

ViewCompat.setTransitionName(fullWidthImage, "THUMBNAIL_IMAGE");

hope it helps

like image 34
Tomislav Novoselec Avatar answered Oct 29 '22 23:10

Tomislav Novoselec