API Reference: Picture & PictureRecorder
When you need to draw the same complex scene multiple times—or if you have a static background that doesn't change—you should use Picture. It records your drawing commands into a highly optimized format that Skia can "play back" much faster than executing individual Java calls every frame.
The Workflow
Recording a picture involves using a PictureRecorder to get a temporary Canvas.
java
PictureRecorder recorder = new PictureRecorder();
// 1. Define the "cull rect" (the area you intend to draw in)
Canvas recordingCanvas = recorder.beginRecording(Rect.makeWH(500, 500));
// 2. Perform your drawing commands as usual
Paint p = new Paint().setColor(0xFF4285F4);
recordingCanvas.drawCircle(250, 250, 100, p);
// ... more drawing ...
// 3. Stop recording and get the Picture object
Picture picture = recorder.finishRecordingAsPicture();PictureRecorder API
The PictureRecorder is the stateful object used to capture commands.
beginRecording(bounds): Starts recording. Returns aCanvasyou can draw into. All drawing commands sent to this canvas will be stored.getRecordingCanvas(): Returns the active recording canvas, ornullif not recording.finishRecordingAsPicture(): Ends recording and returns the immutablePictureobject. Invalidates the recording canvas.finishRecordingAsPicture(cullRect): Ends recording, but overrides the cull rect stored in the picture.
Creating Pictures (Serialization)
makePlaceholder(cullRect): Creates a placeholder picture that draws nothing but has specific bounds.makeFromData(data): Deserializes a picture from aDataobject (created viaserializeToData).
Drawing the Picture
Once you have a Picture object, you can draw it onto any other Canvas.
java
canvas.drawPicture(picture);Why use Picture?
- Performance: If you have 1,000 drawing calls, Java has to call into native code 1,000 times per frame. If you record them into a
Picture, it's just one native call per frame. - Thread Safety: While a
Canvasis tied to a thread, aPictureis immutable and can be drawn from any thread (though usually you draw it on your main rendering thread). - Tessellation Caching: Skia can cache the complex geometry (like paths) inside a
Picturebetter than it can for individual calls.
Best Practices & Pitfalls
- Don't record everything: If your content changes every single frame (like a moving character), recording a new
Pictureevery time might actually be slower due to the overhead of the recorder. - Canvas Lifetime: The
Canvasyou get frombeginRecording()is only valid until you callfinishRecordingAsPicture(). Don't try to keep a reference to it! - Memory: Pictures take up native memory. If you are creating many small pictures, remember to
close()them when they are no longer needed.