All publications
Jetpack Compose

TransformableBrush: Efficient Brush Animations in Compose

Smooth shimmer and gradient effects in Jetpack Compose — without the allocation tax.

Nov 7, 2024 · 8 min read · Preview
TransformableBrush: Efficient Brush Animations in Compose

Summary

Brush animations in Compose are easy to write and easy to make slow: recreating a brush every frame quietly allocates on the hot path. This article builds a shimmer from scratch, finds the allocation problem, then fixes it with a transformable brush backed by LocalMatrix — keeping the effect smooth and cheap.

The fastest way to jank a Compose animation is to allocate inside draw. This article builds a shimmer, diagnoses exactly that mistake, and replaces it with a transformable brush — same effect, a fraction of the cost.

Key ideas

  • 01 A naive shimmer recreates its Brush every frame — allocations on the draw path.
  • 02 Brushes can be transformed instead of rebuilt, reusing the same shader.
  • 03 LocalMatrix lets you translate/scale the brush per frame for free.
  • 04 The result: identical visuals, far less garbage and smoother frames.