Android Canvas & Animation Interview Questions

Custom drawing, animation, and performance.

50 questions in this topic · 8 sample questions below

Practice Canvas & Animation in the quiz engine

Sample questions

  1. Why is allocating a new Paint object inside a custom View's onDraw considered an anti-pattern?

    • onDraw can run many times per second, so allocating there causes GC churn and jank — correct
    • Paint objects cannot be created outside onDraw
    • The Canvas resets all Paint references after each frame
    • Paint allocation forces a full layout pass

    Why: onDraw may be called on every frame, so allocations create garbage that triggers GC pauses and dropped frames; allocate Paint once as a field. Paint can absolutely be created elsewhere, which is exactly the recommended fix.

  2. What is the key difference between invalidate() and postInvalidate() on a View?

    • postInvalidate() also re-runs measure and layout
    • invalidate() must be called from the UI thread while postInvalidate() can be called from a background thread — correct
    • invalidate() is synchronous and blocks until the frame is drawn
    • postInvalidate() skips the draw pass entirely

    Why: postInvalidate() posts the invalidation to the UI thread's message queue so it is safe from other threads, whereas invalidate() must be called on the UI thread. invalidate() is not synchronous; it just schedules a redraw for the next frame.

  3. Calling invalidate() on a View schedules which pass(es)?

    • measure, layout, and draw
    • only the layout pass
    • only the draw pass — correct
    • a recomposition of the whole hierarchy

    Why: invalidate() marks the view dirty and schedules only a redraw (draw pass). To re-trigger measure and layout you must call requestLayout(); a common misconception is that invalidate() lays out again.

  4. In the Compose UI phases (composition, layout, drawing), animating a value that is only read inside a graphicsLayer block is efficient because it skips which phases?

    • it skips drawing only
    • it skips nothing; graphicsLayer always recomposes
    • it skips layout only but recomposes
    • it skips both composition and layout, only re-running the draw and graphicsLayer phase — correct

    Why: Reading state inside graphicsLayer's lambda defers the read to the draw phase, so changes skip composition and layout entirely. The misconception that graphicsLayer changes cause recomposition is exactly what this technique avoids.

  5. What does the antiAlias flag on a Paint object control?

    • Smoothing of edges to reduce jaggedness on non-axis-aligned lines and curves — correct
    • Whether shapes are hardware accelerated
    • The transparency of the fill color
    • Whether the Paint can be reused across frames

    Why: Anti-aliasing smooths the pixelated edges of diagonal lines, curves, and text. It has nothing to do with hardware acceleration or reuse; transparency is controlled by alpha, not the anti-alias flag.

  6. You call setLayerType(LAYER_TYPE_HARDWARE) on a View to speed up an alpha animation. What is the main trade-off?

    • It disables anti-aliasing for that view
    • It renders the view into an offscreen GPU texture, consuming video memory and costing an upload when content changes — correct
    • It forces all child views onto the software renderer
    • It permanently caches the view so invalidate() no longer works

    Why: A hardware layer allocates an offscreen texture; it is fast for transforms and alpha but costs GPU memory and re-uploads whenever the layer's own content is invalidated. Hardware layers are not universally faster, which is why they should be applied only for the duration of an animation.

  7. Which Paint.Style causes only the outline of a shape to be drawn using the Paint's stroke width?

    • Paint.Style.FILL
    • Paint.Style.FILL_AND_STROKE
    • Paint.Style.STROKE — correct
    • Paint.Style.OUTLINE

    Why: STROKE draws only the outline using strokeWidth. FILL_AND_STROKE draws both the interior and the outline, so it is not outline-only, and OUTLINE is not a valid Paint.Style constant.

  8. To draw a bitmap source only inside a previously drawn circular region, which PorterDuff mode produces a circular crop?

    • PorterDuff.Mode.SRC_OVER
    • PorterDuff.Mode.DST_OUT
    • PorterDuff.Mode.XOR
    • PorterDuff.Mode.SRC_IN — correct

    Why: With the circle as destination and the bitmap as source, SRC_IN keeps the source only where the destination is opaque, yielding a circular crop. SRC_OVER simply draws the source on top without masking, so no clipping occurs.

Practice all 50 Canvas & Animation questions

These 8 are a sample. The full Canvas & Animation bank is scored, tracks your progress, and explains every answer.

Open the quiz

More Android interview topics