بنقرة واحدة
android-high-performance-custom-view
Best practices for 60fps custom View rendering with zero allocation in onDraw
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
Best practices for 60fps custom View rendering with zero allocation in onDraw
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
| name | Android High-Performance Custom View |
| description | Best practices for 60fps custom View rendering with zero allocation in onDraw |
Last Verified: 2026-01-23 Applicable SDK: Android 14+ (API 34+) Dependencies: None
This skill covers techniques for achieving smooth 60fps rendering in custom Android Views, focusing on memory efficiency and GPU optimization.
NEVER allocate objects inside onDraw():
// ❌ BAD - allocates every frame
override fun onDraw(canvas: Canvas) {
val paint = Paint()
val path = Path()
val rect = RectF()
// ...
}
// ✅ GOOD - pre-allocate at class level
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private val path = Path()
private val rect = RectF()
override fun onDraw(canvas: Canvas) {
path.reset() // Reuse, don't recreate
// ...
}
Calculate complex paths in onSizeChanged, not onDraw:
private val clipPath = Path()
private val cardPath = Path()
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
// Pre-compute rounded rectangle path
cardPath.reset()
cardPath.addRoundRect(0f, 0f, w.toFloat(), h.toFloat(), cornerRadius, cornerRadius, Path.Direction.CW)
// Pre-compute clip regions
clipPath.set(cardPath)
tempPath.addRect(0f, 0f, w.toFloat(), h / 2f, Path.Direction.CW)
clipPath.op(tempPath, Path.Op.INTERSECT)
}
override fun onDraw(canvas: Canvas) {
canvas.clipPath(clipPath) // Use pre-computed path
// ...
}
Only recreate expensive objects when dimensions actually change:
private var lastWidth = 0
private var lastHeight = 0
private fun refreshGradientsIfNeeded(w: Int, h: Int) {
if (w == lastWidth && h == lastHeight) return
lastWidth = w
lastHeight = h
// Expensive shader creation
paint.shader = LinearGradient(
0f, 0f, 0f, h.toFloat(),
topColor, bottomColor,
Shader.TileMode.CLAMP
)
}
Enable hardware acceleration for animation-heavy views:
init {
setLayerType(LAYER_TYPE_HARDWARE, null)
}
When to use:
When NOT to use:
For views displaying text, cache measurement results:
private val textBoundsCache = mutableMapOf<String, Rect>()
private fun getTextBounds(text: String): Rect {
return textBoundsCache.getOrPut(text) {
Rect().also { paint.getTextBounds(text, 0, text.length, it) }
}
}
Avoid redundant recalculations for minor changes:
private var lastDimWidth = 0f
fun setDimensions(width: Float, height: Float) {
// Only update if change exceeds threshold
if (abs(width - lastDimWidth) < 0.5f) return
lastDimWidth = width
// Expensive recalculation...
}
new keywords inside onDraw()onSizeChanged()onDrawGuidance for creating effective modular skills for Claude. Use when creating or refining skills to ensure they are concise, follow progressive disclosure patterns, and provide appropriate degrees of freedom.
Guidelines for ensuring smooth and race-condition-free theme transitions in Android, specifically regarding icon tinting and state management.
Best practices for effective human-AI pair programming and communication
Clarify touch-target vs visual size when users ask to resize buttons without changing icons.
Choose whether the outer container acts as the touch proxy or remains a layout placeholder to control hit targets.
Expert guidance for creating soulful, industrial-grade Android native interfaces with Jetpack Compose, focusing on tactility, typography, and motion.