The first article put one button in four chapters. Each chapter used one API, and you could read it alone. That method helps you learn an API. It does not show you how an API behaves together with other APIs. This article puts five APIs on one screen. All five work at the same time.
The screen shows a speak button. Hold the pill and the button shows a listening state. Release it and the button sends. The gesture must give five results at the same time. A level value must become light. A clock must continue to make level values while no animation runs. The pill must change width when its label changes, with no jump. The press must end in one of four states. The whole scene must go dark when the work stops. A sixth part has no pixels. It gives a tick to the thumb, and it makes the button feel complete.
The ring counts down. Then the dot shows the level. Then "Sent" shows. Then the button returns to idle.
What each API owns
| piece | API | its job |
|---|---|---|
| the halo |
Canvas + Brush.radialGradient + BlendMode
|
turn a value from 0 to 1 into light |
| the driver |
withFrameNanos + a pure function |
make a value with no target |
| the morph |
Animatable + AnimatedContent + SizeTransform
|
keep four states and change the width with no jump |
| the gesture |
detectTapGestures + tryAwaitRelease
|
press, hold, cancel, release |
| the tick |
LocalHapticFeedback + HapticFeedbackType
|
mark the threshold and the release with no pixel |
| the gate | animateFloatAsState |
turn the glow on and off |
All six parts meet at one object: the four-state machine. The other parts do two jobs. They read the state, or they make a value that the state uses.
1. The halo uses three circles and one blend mode
The amplitude and the code are the same. On the right the halo adds light. On the left it must subtract light.
The usual solution is one large blurred circle. The result looks like a smear. To get a light effect, stack three radial gradients. Each gradient is wider and dimmer than the one before it:
val haloRadius = baseRadius * (minRadius + (maxRadius - minRadius) * amp)
val peakAlpha = (minAlpha + (maxAlpha - minAlpha) * amp) * gate
drawHalo(center, haloRadius, haloColor, peakAlpha, blendMode)
drawHalo(center, haloRadius * 0.6f, midColor, peakAlpha * 0.7f, blendMode)
drawHalo(center, haloRadius * 0.28f, coreColor, peakAlpha * 0.55f, blendMode)
Each halo is a drawCircle with a Brush.radialGradient. The brush has three stops: full alpha at the centre, 45% at 0.62, transparent at the edge. The stop at 0.62 stops the falloff from looking linear. A gradient with two stops goes to transparent too evenly, and the centre looks flat. The radii come from size.minDimension. Thus the same code works on a 260dp stage and on a 40dp chip.
The colour is lerp(primary, tertiary, 0.65). It is not the theme primary. Two hues at high alpha look like light with a temperature. One hue looks like a fill with a soft edge.
The blend mode decides if this light is visible at all:
val isLight = MaterialTheme.colorScheme.background.luminance() > 0.5f
val blendMode = if (isLight) BlendMode.SrcOver else BlendMode.Plus
BlendMode.Plus adds each colour channel. On a dark surface the result is additive light, and it looks like a lamp. On a white surface there is nothing to add. Each channel is near 1.0, the result is clamped, and the glow disappears. This is the usual cause of a glow that works in the dark theme. In the light theme it does nothing.
On a light surface, use SrcOver and paint a halo that subtracts light. Take the theme primary and move it 25% toward black. Make the core a little darker. The calculation is the same, but the direction is opposite. The constants are separate. They are not one set multiplied by a flag. The light halo is wider and stronger than the dark halo (0.62 to 1.28 against 0.55 to 1.15). A bright field needs more light, and darkness needs less.
2. A value with no target
The two ends of the range, with the clock stopped. The radius is fifteen times larger at 1.00.
All other numbers on this screen have a target. The halo value has no target. It goes up and down continuously. It never arrives. It has no meaning when the button is idle. Animatable is not correct for this type of value. Animatable interpolates from the current value to a target value, then it stops.
This value needs its own clock:
if (enabled && frozenValue == null) {
LaunchedEffect(seed, periodMillis) {
while (true) {
withFrameNanos { now ->
state.floatValue = generator.valueAt(now / 1_000_000L)
}
}
}
}
withFrameNanos gives you the frame clock. The value updates one time on each frame, and never two times on one frame. Do not use LaunchedEffect { while (true) { delay(16) ... } } here. That loop drifts against the display. It can also fire two times between two frames, and the user sees a stutter in a shape this soft.
The function valueAt is a pure function of time. One sine wave makes the slow rise. Three octaves of value noise make the texture. The weights are 0.55 and 0.45, so the texture does not control the shape. A pure function gives the same value for the same millisecond. For this reason, the halo in the clip above is the same object on each run. The two screenshots above show one curve, not two lucky frames.
Freezing does not change the composition. It changes the source. The function returns one State<Float> in all cases. That state uses the frozen value when it is available, and the live value when it is not. When you stop the clock, one number changes. The effect that makes the value stays alive.
Move the slider from 0.04 to 0.95 while the clock is stopped. The radius and the alpha follow the value.
3. The pill changes width while it morphs
Left: the countdown ring while the hold is too short. Right: the dot after the hold counts. The dot uses the same value as the halo.
A Button does not report a press. There is no onPress callback and no onRelease callback. There is no duration, and no "the finger is still down". The ripple follows the pointer and then disappears. It gives no information. combinedClickable with onLongClick is nearer, but it fires one time at a threshold. It also gives no cancel signal when the finger moves away. The gesture layer is the only API that shows all four moments:
Modifier.pointerInput(Unit) {
detectTapGestures(
onPress = {
pressStartedAt = System.nanoTime()
state = ActionState.Loading
val released = tryAwaitRelease()
val held = (System.nanoTime() - pressStartedAt) / 1_000_000
val outcome = if (released) releaseOutcome(held) else ActionState.Idle
state = outcome
},
)
}
tryAwaitRelease() is the important call. It suspends until the finger lifts or the gesture cancels, and it reports which of the two occurred. A cancelled hold returns to idle. It does not send a one-second note.
From there, the pill is a state machine, and the width is the difficult part. "Hold to speak", "Listening" with a ring, and "Sent" have three different widths. If a label replaces another label immediately, the button looks like it reloaded. Use one AnimatedContent:
AnimatedContent(
targetState = state,
transitionSpec = {
(fadeIn(spec) + scaleIn(spec, 0.92f)) togetherWith
(fadeOut(spec) + scaleOut(spec, 0.92f)) using SizeTransform(clip = false)
},
)
With clip = true, the new label is cut at the old width while the container grows. Set clip = false and the content can draw past the bounds during the 160ms of the swap.
The container is not new. SpeakCtaPill calls rememberMorphVisuals(state) and actionSemantics(state) from the first article. It does not change them:
val visuals = rememberMorphVisuals(state)
Box(
modifier = modifier
.clip(RoundedCornerShape(visuals.corner))
.background(visuals.container)
.actionSemantics(state),
)
The corner radius, the container colour and the TalkBack state line come from the mapping in the first article. A hold sets the state instead of a tap. A state machine that works only for taps is not a state machine.
Five seconds is the minimum hold. During these five seconds the button shows how much of the rule remains. After the hold counts, the dot replaces the ring. If both indicators show at the same time, the user must decide which one is important.
LaunchedEffect(state) {
while (state == ActionState.Loading) {
withFrameNanos { heldMillis = (System.nanoTime() - pressStartedAt) / 1_000_000 }
}
}
This is the second clock on the screen. It is separate for a reason. The halo continues to breathe when no finger is down. This loop exists only while the state is Loading. The loop also stops on release, so it does not leak a coroutine for the life of the screen.
At the same moment, the thumb gets a tick. The tick uses the same threshold that the ring shows:
// one tick at the threshold, not one on each frame after it
if (!registered && heldMillis >= CtaMotion.MinHoldMillis) {
registered = true
haptics.performHapticFeedback(HapticFeedbackType.Confirm)
}
// and one on release, which can disagree
if (released) haptics.performHapticFeedback(
if (outcome == ActionState.Failed) HapticFeedbackType.Reject
else HapticFeedbackType.Confirm,
)
The registered flag separates feedback from noise. Without it, each frame after five seconds queues one more tick. The release tick answers a different question, and it can disagree with the first tick. Use Reject when the hold was too short. Use Confirm when the button sends.
HapticFeedbackType is part of Compose, not a platform constant. The same two lines work on each device that runs the app. On hardware with no vibrator, the call does nothing. This is also the only part of the screen that a GIF cannot show. The proof is a dumpsys vibrator_manager dump. It shows a CLICK at the threshold, a CLICK on a good release, and a DOUBLE_CLICK on a rejected one. All three come from the uid of the app.
Release after five seconds, and release after one. The failure text gives the correct action.
The verdict is a pure function. The dwell times are different on purpose. A failure stays 2.4 seconds. A send stays 0.9 seconds. The user needs one look at "Sent". The user must read "Too short, hold 5s" and then act.
4. One float turns the glow on and off
The stage is a Box with a height of 300dp and the full width. A 300dp halo is centred in the Box. The pill is on top. No other part connects them. The halo is on while the button works. It is off in all other states, except when the clock is frozen, because then the user must see it:
val glowIntensity by animateFloatAsState(
targetValue = if (frozen || state == ActionState.Loading) 1f else 0f,
animationSpec = tween(durationMillis = 240),
label = "glowGate",
)
intensity multiplies the alpha only. The value controls the radius. The gate controls the brightness. Thus a halo that fades in does not also look smaller. If you put the gate into the radius, the fade becomes a zoom.
One line in the canvas is important:
val peakAlpha = (minAlpha + (maxAlpha - minAlpha) * amp) * gate
if (peakAlpha <= 0f) return@Canvas
At alpha 0, three gradient circles still cost three draw calls. They also blend a few hundred thousand pixels on each frame. They show nothing. The early return stops all of that.
The gate also makes the five parts look like one object. It does not animate a property of the halo. It reads the state machine and multiplies the answer into the picture. The last second of the clip above shows this result. The halo goes off on release because state leaves Loading. The position of the finger is not the cause.
Conclusion
Five animation APIs, and only two of them know about each other. The gesture writes the state machine. The state machine controls the morph, the gate, the TalkBack line, and the threshold for the tick. The halo reads a value that has no relation to these parts. One float decides if the halo is visible.
This separation is the important part. You can replace the halo with a waveform. You can remove the waveform. The button stays the same: four states, and a verdict after release.
In the first article, each chapter showed one API. Here, the list of APIs stopped to be important when the screen started to work as one object.
If I build this again, I set the gate before I tune the halo. I built the halo first at full brightness. Then I found that half of the frames in the app showed it at zero. I had tuned it for a state that almost never occurred.
Code, previews and both themes: github.com/hyuwah/composelab






Top comments (0)