Question

My problem is probably really simple. I recently picked up creating live wallpapers for Android (and Android coding in general), and I lack the basics behind Live Wallpaper coding, since it's really hard to find.

I would like to create a Live Wallpaper with 2 (or more) "layers". My two so called layers are:

  • background which reacts to touch events
  • a bitmap, which is on top

At the moment I'm drawing everything in a single Canvas, which looks like this:

Resources res = getResources();
Bitmap image = BitmapFactory.decodeResource(res, R.drawable.image);

        Canvas c = null;
        try {
            c = holder.lockCanvas();
            if (c != null) {
                c.drawARGB(aa, rr, gg, bb);
                c.drawBitmap(image, 0, 0, paint);
            }
        } finally {
            if (c != null) holder.unlockCanvasAndPost(c);
        }

It works, but the performance is really bad, since I'm drawing this relatively large PNG file on each frame redraw.

I would like to know what is the best way to display bitmaps in Live Wallpapers? As well as how to later animate them (basically just change the position for a start) without having to redraw them each time? Finally, how to determine the z-index (have multiple layers) in Live Wallpapers - can this also be done with XML like in "normal apps"?

Was it helpful?

Solution

Today I've posted a live wallpaper template on GitHub which uses canvas and a separate animation thread. You might want to check it out, if you're just starting with live wallpapers.

In your case, you should decode bitmap only once and then simply draw it. There is nothing wrong in redrawing bitmap every frame. If you want to have multiple layers simply draw them in order, so that the top layer will be drawn last.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top