2

I am writing a LiveWallpaper for Android and I want to have a Bitmap with a certain amount of opacity to show.

In the constructor of my LiveWallpaper Engine I set a Paint that I will use later on my Canvas:

MyEngine() {
    ...
    mForeGroundPaint = new Paint();
    mForeGroundPaint.setAlpha(5);
}

I draw the Bitmap in this function, using the mForeGroundPaint on the drawBitmap():

void drawFrame() {
    final SurfaceHolder holder = getSurfaceHolder();
    Canvas c = null;
    try {
        c = holder.lockCanvas();
        if (c != null) {
            c.save();
            /* allows the wallpaper to scroll through the homescreens */
            c.drawBitmap(wpBitmap, screenWidth * -mOffset, 0,
                    mForeGroundPaint);
            c.restore();
        }
    } finally {
        if (c != null)
                 holder.unlockCanvasAndPost©;
    }
}

What happens now is, that everything seems to work fine, what means that the Bitmap is painted with the opacity value of 5, like I set it.

The problem happens when I use that drawFrame() function several times, as it is called during onOffsetsChanged(): The opacity sums up, making it 10, 15, 20, 25, ... with every call of drawFrame().

How can I prevent that from happening, and thus keep the amount of opacity on a steady level?

Simon Sarris
  • 62,212
  • 13
  • 141
  • 171
msal
  • 947
  • 1
  • 9
  • 30

1 Answers1

4

The Bitmap is just being redrawn over old ones, so you have 2 Bitmaps at 5% opacity = 10% opacity. Try clearing the Canvas with c.drawColor(...); (with your background color) after c.save();.

Cat
  • 66,919
  • 24
  • 133
  • 141