Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement scale up animation on shared element on activity transition

I wish to implement scale up animation on shared elements on activity transitions just like in this link .

But couldn't find any good reference for this specific effect and how to implement it. Is this a custom transition or a default ? Maybe anyone could help or post more detailed tutorial on this rather than official documentation .

like image 587
Datenshi Avatar asked Jun 13 '16 14:06

Datenshi


People also ask

Which of the following transitions is a shared elements transition?

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.

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 transition between fragments?

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.


1 Answers

Let me give you a short tutorial right here :)

Shared element transition

What you actually want is a Shared element transition between two activities. You'll not actually share any views, both activities will have independent view trees. But we'll pass the info about the shared element such as its view and its size to the new activity.

While launching, the new activity will make all its views transparent and locates the shared view. It alters its attributes to match those passed in from the launching activity and makes that single view visible. It then runs animations to transition the shared view from this state to its natural position in the layout. As the transition progresses, the window background and the rest of the non-shared elements slowly fade in until they're totally opaque. All of this is done automatically.

Now to mark a view as shared set this property:

<ImageView
...
android:transitionName="@string/transition_photo" />

in both the activity layouts.

Now while starting your new activity from old activity define a transition animation:

 Bundle bundle = ActivityOptions.makeSceneTransitionAnimation(
                                  this, 
                                  sharedView,
                                  sharedView.getTransitionName())
                                 .toBundle();
startActivity(intent,bundle);

You can also specify multiple views for transition. You can even transition shared views between different applications.

By default the animation used is move:

<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeBounds/>
<changeTransform/>
<changeClipBounds/>
<changeImageTransform/>
</transitionSet>

But you can also set your custom animations in styles.xml:

<style name="AppTheme.Details">
    <item name="android:windowSharedElementEnterTransition">@transition/shared_photo</item>
</style>

Here is a working example of shared element transition as shown above: https://github.com/anshchauhan/SharedElementTransition

like image 182
Heisenberg Avatar answered Sep 21 '22 20:09

Heisenberg