For my live wallpaper I use the following code (called by a Runnable)
to draw each frame. Each time it is called, I fill the current canvas
with a solid color and draw a background bitmap (bg_image has been
resized to perfectly fit the screen). I then call drawParticles(c),
which simply uses c.drawCircle(...) a bunch of times drawing particles
all over the canvas.

In the live wallpaper preview mode, this code works great. However,
when I actually set this as my live wallpaper it flickers and seems to
not clear the canvas before drawing. Let me 'splain:

Frame 1: The bitmap is drawn and circles are overlaid.

Frame 2: The bitmap is drawn and circles are overlaid (based on my
rough understanding, there are two canvases that are drawn on and
posted alternately for efficiency).

Frame 3: The canvas is not being cleared! This frame includes the new
positions of each drawn circle as well as the circles from Frame 1!.

Frame 4: Includes the new positions of each drawn circle as well as
the circles from Frame 2!

The end effect is that the circles leave "trails" all over the screen
that flicker between (I believe) the two alternating canvases. Why,
based on my code below, isn't my canvas being cleared each frame?
Again, this works fine during preview mode but not when it is actually
set as my live wallpaper. It's also worth noting that this flickering
problem only occurs if I am drawing a bitmap; if the background is
just a solid color, this problem never arises.

---

final SurfaceHolder holder = getSurfaceHolder();

Canvas c = null;
try {
    c = holder.lockCanvas();
    if (c != null) {
        c.drawColor(Color.BLACK);
        c.drawBitmap(bg_image, 0, 0, null);
        fluid.drawParticles(c);
    }
} finally {
    if (c != null) holder.unlockCanvasAndPost(c);
}

mHandler.removeCallbacks(mDrawRunnable);
mHandler.postDelayed(mDrawRunnable, 1000/targetFramerate -
(System.currentTimeMillis() - mLastTime));
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to