Search code examples
androidcanvaslive-wallpaper

Android Canvas Multiple Layers for WallpaperService.Engine


I'm working on a live wallpaper. The wallpaper requires small movable images to be behind a large fixed image with transparent areas. The smaller images will only be visible when they are in the transparent parts of the large image.

Here's how I've been doing it so far:

Canvas c = holder.lockCanvas();
c.save()
drawSmallImages(c); //draw the movable images
drawLargeImage(c); //draw the fixed large image
c.restore();

I ran this through traceview and it looks like android is spending a good chunk of processing power to draw the large image, and ideally I'd only want it to be drawn once when the wallpaper starts. I don't know how to get the smaller images to be drawn behind the larger image without re-drawing the large image after the smaller images on each frame.


Solution

  • The content of the Canvas won't be cleared so you could easily optimize your drawing by clearing small portions and using the clip rect to redraw only part of the large image.