惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
B
Blog
博客园 - 三生石上(FineUI控件)
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
Vercel News
Vercel News
量子位
A
About on SuperTechFans
博客园 - 聂微东
WordPress大学
WordPress大学
D
DataBreaches.Net
The Cloudflare Blog
M
MIT News - Artificial intelligence
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
雷峰网
雷峰网
C
Check Point Blog
S
SegmentFault 最新的问题
U
Unit 42
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Stop Animating Modifiers Like This in Jetpack Compose
YADNYESH RANA · 2026-06-24 · via DEV Community

YADNYESH RANA

Animating offset or alpha properties using standard compose states can easily tank your UI frame rates. If you read animation states directly in composition, your composable runs all three phases—composition, layout, and drawing—at 120 FPS.

You can bypass this overhead by deferring state reads to the layout or draw phases.

The Three Phases of Compose
Jetpack Compose renders frames in three distinct steps:

  1. Composition: What to show (runs the composable functions and builds the UI tree).
  2. Layout: Where to show (measures children and places them in coordinates).
  3. Draw: How to render (draws pixels on the canvas).

When a state changes, Compose starts from the phase where that state was read. If you read state during composition, you force the entire pipeline to rerun. If you defer the state read to the layout or draw phase, you skip composition entirely.

  1. Animating Positions (Layout Phase) Here is a common animation implementation that degrades layout performance:
// BAD: Recomposes the Box and its parents on every single frame
@Composable
fun SlidingCard(targetOffset: Dp) {
    val translationX by animateDpAsState(targetValue = targetOffset)

    Box(
        modifier = Modifier
            .size(100.dp)
            .offset(x = translationX, y = 0.dp) // State read occurs in composition
            .background(Color.Blue)
    )
}

Since the state translationX is read in the composition phase (as an argument to Modifier.offset), the composable recomposes at 120 FPS as the animation updates.

We can fix this by wrapping the offset calculation in a lambda:

// GOOD: Zero recompositions. Updates coordinates directly in the Layout phase
@Composable
fun SlidingCard(targetOffset: Dp) {
    val translationX by animateDpAsState(targetValue = targetOffset)

    Box(
        modifier = Modifier
            .size(100.dp)
            .offset { IntOffset(x = translationX.roundToPx(), y = 0) } // Lambda defers state read
            .background(Color.Blue)
    )
}

By using the lambda version of offset, the value of translationX is not read until the layout phase. The composition step is skipped entirely.

2.Animating Alpha and Rotations (Draw Phase)
Similarly, animating visual transformations like opacity or rotations can trigger layout recalculations if done incorrectly:

// BAD: Triggers composition and layout passes on every frame change
@Composable
fun FadingCard(targetAlpha: Float) {
    val alphaState by animateFloatAsState(targetValue = targetAlpha)

    Box(
        modifier = Modifier
            .size(100.dp)
            .alpha(alphaState) // State read occurs in composition
            .background(Color.Blue)
    )
}

Since changing alpha does not affect the size or positions of elements, there is no reason to rerun layout passes.

We can bypass both composition and layout by reading the state directly inside a graphics layer block:

// GOOD: Zero recompositions. Modifies drawing properties directly on the GPU
@Composable
fun FadingCard(targetAlpha: Float) {
    val alphaState by animateFloatAsState(targetValue = targetAlpha)

    Box(
        modifier = Modifier
            .size(100.dp)
            .graphicsLayer { alpha = alphaState } // Direct draw-phase read
            .background(Color.Blue)
    )
}

Inside the graphicsLayer lambda, alphaState is read during the drawing phase. Compose skips composition and layout, rendering the visual changes directly.

How to Verify in Layout Inspector
Open the Layout Inspector in Android Studio:

  • Run the animation.
  • Watch the Recomposition Counts column.
  • Using the standard modifiers, the recomposition count climbs rapidly.
  • Switch to lambda modifiers (offset {} or graphicsLayer {}) and the recomposition count stays at exactly 0.

Open-Source Reference
This optimization is part of the open-source Compose Performance Cheat Sheet. You can clone the full repository of performance starter kits, stability configurations, and lazy list recycling templates here:

👉 GitHub: Compose Performance & Recomposition Cheat Sheet (A print-ready, high-resolution A4 PDF version is also pinned in the repository description).