B1 中级 English 7:25 1,272 单词 Science & Tech

Advanced shared elements with shape morphing | Jetpack Compose Tips

English with Lucy · 10,913 次观看 · Added 1 小时前

Learning Stats

B1

CEFR 等级

1,272

Total Words

360

Unique Words

4/10

Difficulty

Vocabulary Diversity 28%

字幕 (174 segments)

Download
00:00

REBECCA FRANKS: Users love screens

00:01

that feel fluid and show continuity between transitions.

00:05

In a previous video, we covered how

00:07

to get started with the basics of shared elements in Jetpack

00:09

Compose.

00:10

In this tip, we will cover a more advanced example

00:13

application of shared elements and how you can also add

00:17

moments of delight to your app.

00:18

[MUSIC PLAYING]

00:24

In Androidify, we are using the new material

00:27

shapes that come as part of the Material 3 Expressive updates.

00:30

We have two screens, the Creation screen

00:33

that shows the camera button and the Camera

00:35

screen that shows the camera viewfinder and the related

00:38

buttons.

00:39

Currently, we've transitioned between the two screens,

00:42

but we don't have a nice animation between the two.

00:44

So what if we wanted to transform between the Take Photo

00:48

button, which is this delicious cookie

00:50

shape, to the full-screen square that the camera screen takes on

00:54

as a shared element, slowly having the cookie reveal

00:57

the camera screen as it grows in size

00:59

and transforms to the screen's rectangle shape.

01:03

We need to do three things to achieve this animation--

01:05

use shared elements to grow from the Photo button

01:08

to the camera screen; reveal the camera

01:11

screen as the button grows in size;

01:13

and clip the shared element to the morphing shape.

01:16

So for step 1, we use the same concept

01:19

shown in the shared element video that's linked below.

01:22

Firstly, we need to make sure that the top level of our

01:25

app we are using a SharedTransitionLayout.

01:28

We've placed our SharedTransitionLayout

01:30

at our top level of the theme in order

01:32

to use it all throughout the Androidify app.

01:35

We also set a composition local for the SharedTransitionScope

01:38

so that we don't need to pass the scope around.

01:41

The next step is to ensure that we have an animated visibility

01:45

scope, which drives the transition between the two

01:47

screens.

01:48

We are using Navigation 3 in this sample, which

01:51

provides a built-in composition local where

01:53

you can access when needed.

01:55

Perfect.

01:56

Now, we have the correct scope set up.

01:59

We can add the sharedBounds modifier onto our content.

02:02

We first add it onto the Take Photo button,

02:05

passing in the key and the animated visibility

02:08

scope from Nav3.

02:09

We then add the same modifier onto the full-screen camera

02:13

screen composable.

02:15

We should have a shared element at this point.

02:17

However, we'd like this effect to be a little bit more

02:19

sophisticated.

02:21

The camera contents to grow from the size

02:23

of the originating button to the size of the screen.

02:26

It's not the effect that we are going for.

02:28

We prefer it to stay in its final end state

02:30

and be slowly revealed to the user instead.

02:32

We call this pattern the reveal pattern.

02:35

The other issue is that the shape isn't morphing

02:37

between the two just yet.

02:39

So for step 2, let's create a wrapper

02:42

to hide away the implementation details

02:44

and make the usage neater.

02:46

We will call this function sharedBoundsReve

02:48

alWithShapeMorph.

02:49

We provide in default scopes from the composition locals,

02:53

the shared transition scope from the SharedTransitionLayout

02:56

and the LocalNavAnimatedContentScope

02:58

from Nav3 and switch our usages to use this modifier that

03:02

wraps shared bounds instead.

03:05

The other values are the same defaults

03:07

that the original sharedBounds modifier uses,

03:09

but we need to expose them to be able to use them in our usages

03:12

too.

03:13

To tackle the issue of the camera not revealing,

03:16

we need to tell the underlying layout

03:17

that it should stay in place and skip the lookahead measurements

03:20

and placement.

03:22

To do this, we add the skipToLookaheadSize

03:25

and skipToLookaheadPosition modifiers.

03:28

These two modifiers will ensure that the content placed

03:31

after the modifiers will not be animated or moved

03:34

when the shared element animation is happening.

03:37

Running this, we can see that the animation looks

03:40

like a reveal animation instead of growing to the camera

03:42

viewfinder.

03:43

Right, for the grand finale, the actual shape morphing.

03:47

This can be achieved using the new material shapes.

03:51

The great thing about these preset shapes

03:53

is that they are backed by the AndroidX graphics shape

03:55

library, which has built-in morphing capabilities.

03:58

This means that we can provide two shapes,

04:01

create a morph between the two, and pass in the progress value

04:04

and have the shapes transform between the two.

04:07

To incorporate the shape transformation,

04:09

we need the two shapes passed in,

04:11

the resting shape and the target shape.

04:13

Each instance of shared bounds needs

04:15

both, as the transition shows both elements at the same time

04:19

on screen as it transitions.

04:20

So we need to ensure that we are matching up

04:22

the incoming and outgoing content

04:24

with the exact same shape as they transform.

04:27

So we update our modifier to take in a resting shape

04:30

and target shape as parameters.

04:32

We then need to also create the morph object.

04:35

We provide the resting shape and the target shape as input

04:38

into the morph.

04:39

We also need to create a progress value

04:41

that animates between 0 and 1 to be able to perform the morphing.

04:45

To do this, we tie into the animated visibility scope

04:48

transition object and use animateFloat

04:50

to animate based on the enter and exit

04:52

state of the transition.

04:54

For example, when the shared element is fully visible,

04:57

we'd like the resting shape to be the shape we use.

05:00

So the visible state has the progress set to 0.

05:03

OK, we now have the shapes passed in,

05:06

the morph object set up, and our progress between the two

05:09

shapes in place.

05:11

Now we need to figure out how to get

05:12

the changing path that will be used

05:14

as the clip for the animation.

05:16

To do this, we need to create an overlay clip.

05:19

We will call ours MorphOverlayClip,

05:21

where we pass in the morph and the animated progress value.

05:24

We will get to the implementation

05:26

of this in just a second.

05:28

This clip is provided to the sharedBounds modifier

05:31

as the clipInOverlayDuringTransition

05:33

variable.

05:34

The shape clip will apply during the transition between the two

05:37

states when the animation is active.

05:40

The MorphOverlayClip implementation

05:42

takes in the morph object and the animated progress.

05:45

We override the getClipPath function.

05:48

This is what the sharedBounds modifier

05:50

will call as it's transitioning between the two elements.

05:53

First, we need to scale up the morph object

05:56

to the size of the area that the animation is performed in.

05:59

In this example, we use a matrix to perform the scaling

06:01

operations.

06:03

We get the max between the width and the height

06:05

to scale it uniformly, not to stretch the object.

06:09

We then call two path on the morph object

06:11

with the animated progress to get

06:13

us the morphed path at that particular point

06:15

in the animation.

06:17

To scale the path up to the size of the area,

06:20

we call transform with the matrix

06:22

that we created previously, and we translate its offset

06:25

to the center of the drawing area.

06:27

We then return that path to be used with the shared elements.

06:30

Now we just need to make sure that both our call sites

06:33

pass in the correct shapes into the content.

06:35

On the camera screen, the resting shape

06:37

will be the rectangle, and the target shape

06:40

will be the cookie nine-sided shape.

06:43

The cookie button will have the same, but in reverse.

06:46

The resting shape is the cookie nine-sided,

06:48

and the target shape is the rectangle.

06:50

And that gives us this ultra-fabulous, expressive,

06:53

delightful shared element transition

06:55

between the two screens.

06:57

Oh, and did I mention it's also compatible with predictive back?

07:01

You can see, as we slowly pull back,

07:03

it animates between the two screens.

07:05

And that's it for this tip.

07:07

To check out the full code sample,

07:09

head to the GitHub repository.

07:10

Bye for now.

07:12

[MUSIC PLAYING]

Key Vocabulary (50)

to A1 preposition

toward

"Go to school."

of A1 preposition

belonging

"Cup of tea."

and A1 conjunction

also

"You and me."

in A1 preposition

inside

"In the house."

that A1 determiner

specific

"That book."

it A1 pronoun

A third-person singular pronoun used to refer to an object, animal, or situation that has already been mentioned or is clear from context. It is also frequently used as a dummy subject to talk about time, weather, or distance.

for A1 preposition

Used to show who is intended to have or use something, or to explain the purpose or reason for an action. It is also frequently used to indicate a specific duration of time.

with A1 preposition

A preposition used to indicate that people or things are together, in the same place, or performing an action together. It can also describe the instrument used to perform an action or a characteristic that someone or something has.

as A1 conjunction

A conjunction used to compare two things that are equal in some way. It is most commonly used in the pattern 'as + adjective/adverb + as' to show similarity.

this A1 pronoun

Used to identify a specific person, thing, or idea that is physically close to the speaker or has just been mentioned. It can also refer to the present time or a situation that is currently happening.

from A1 preposition

Used to indicate the starting point, source, or origin of something. It can describe a physical location, a point in time, or the person who sent or gave an item.

we A1 pronoun

The word 'we' is a first-person plural pronoun used to refer to the speaker and one or more other people collectively. It is used as the subject of a sentence or clause.

will A1 verb

A modal verb used to talk about future actions, predictions, or promises. It is placed before the base form of a verb to show that something is going to happen later.

so A1 adverb

An adverb used to emphasize the quality, intensity, or extent of an adjective or another adverb. It indicates that something is very or extremely high in degree.

can A1 verb

Used to express physical or mental ability to do something or to show that something is possible. It is also frequently used to ask for or give permission in informal situations.

Sign up to unlock full features

Track progress, save vocabulary, and practice exercises

Description

Rebecca Franks, a Developer Relations Engineer, covers a more advanced example application of shared elements in Jetpack Compose, combining the basics with shape morphing and the new Material...

分类

Technology

English with Lucy