I am able to get TextViews to transition perfectly between two activities using ActivityOptions.makeSceneTransitionAnimation
. However I want to make the text scale up as it transitions. I can see the material design example scaling up the text "Alphonso Engelking" in the contact card transition.
I've tried setting the scale attributes on the destination TextView and using the changeTransform shared element transitions, but it doesn't scale and the text ends up being truncated as it transitions.
How can I scale TextViews using shared element transition?
This was covered in one of the Google I/O 2016 talks. The source for the transition which you can copy into your code is found here. If your IDE complains the
addTarget(TextView.class);
requires API 21, just remove the constructor and add the target either dynamically or in your xml.i.e. (note this is in Kotlin)
If you look at how
ChangeBounds
works, it operates on the left/right/top/bottom properties of the view.What I expect is that you'll need to use the same text size across the two activities, and use the
scaleX
andscaleY
properties in your launched activity to modify the text size as needed. Then, use a combination ofChangeBounds
andChangeTransform
in yourTransitionSet
.Edit:
As pointed out by Kiryl Tkach in the comments below, there is a better solution described in this Google I/O talk.
You can create a custom transition that animates a
TextView
's text size as follows:Since changing the
TextView
's text size will cause its layout bounds to change during the course of the animation, getting the transition to work properly will take a little more effort than simply throwing aChangeBounds
transition into the sameTransitionSet
. What you will need to do instead is manually measure/layout the view in its end state in aSharedElementCallback
.I've published an example project on GitHub that illustrates the concept (note that the project defines two Gradle product flavors... one uses Activity Transitions and the other uses Fragment Transitions).
I used solution from Alex Lockwood and simplified the use (it's only for TextSize of a TextView), I hope this will help:
and the class EnterSharedElementTextSizeHandler: