drawWithCacheとパフォーマンス
フレームごとの描画オブジェクトの再構築を避けます。
「drawWithCacheとパフォーマンス」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Drawing Runs Often
Your Canvas lambda can run on every frame. Rebuilding heavy objects there, frame after frame, quietly drags down performance.
The Costly Objects
Things like Path, Brush, and Paint are not free to create. Allocating them on each draw wastes work the GPU never needed.
Enter drawWithCache
The drawWithCache modifier lets you build expensive objects once and reuse them. It splits setup from the actual drawing step.
Modifier.drawWithCache {
// build once here
onDrawBehind { /* draw each frame */ }
}Build Block Runs Rarely
Code in the cache block only re-runs when the size or your read state changes. Put your Path and Brush creation right here.
Modifier.drawWithCache {
val brush = Brush.linearGradient(colors)
onDrawBehind { drawRect(brush) }
}onDrawBehind
Use onDrawBehind to paint underneath the Composable's content. It gives you a DrawScope that runs on each frame, cheaply.
onDrawBehind {
drawCircle(color = Color.Blue)
}onDrawWithContent
Need to mix your art with the actual UI? onDrawWithContent lets you draw, call drawContent, and draw again around it.
onDrawWithContent {
drawContent()
drawRect(color = Color.Black, alpha = 0.2f)
}Cache the Path
A chart Path is a classic win. Build it once in the cache block, then just drawPath it every frame without rebuilding.
val path = Path().apply { /* shape */ }
onDrawBehind { drawPath(path, Color.Green) }React to Size
Inside the cache block you can read size safely. When the canvas resizes, your block re-runs and rebuilds objects to fit.
Avoid State Reads in Draw
Reading state during drawing can trigger extra work. Keep your draw block lean and move setup into the cache block instead.
Measure, Do Not Guess
Before optimizing, profile. The Layout Inspector and frame timings tell you what really stutters, so you fix the right thing.
Reuse Wherever You Can
The golden rule: build heavy objects once and reuse them. drawWithCache makes that pattern easy and your animations buttery smooth.
Quick Check
Where should you create an expensive Brush so it is not rebuilt every frame?
Recap
You learned to keep drawing fast: build heavy objects in the drawWithCache block, draw cheaply in onDrawBehind, and always measure first. Smooth frames! ⚡
よくある質問
「drawWithCacheとパフォーマンス」レッスンは無料ですか?
はい。「drawWithCacheとパフォーマンス」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。
「drawWithCacheとパフォーマンス」で何を学びますか?
フレームごとの描画オブジェクトの再構築を避けます。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Jetpack Compose Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「drawWithCacheとパフォーマンス」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJetpack Compose Academyレッスンでコードを書いて実行できますか?
はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- DrawScope:線、矩形、円
- パス、グラデーション、ブレンドモード
- drawWithCacheとパフォーマンス
- ライブ進捗リングを作る