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?
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.
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).
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With