Search code examples
androidbitmapandroid-canvaslive-wallpaperwallpaper

Live Wallpaper and extended background


I'm trying to create a live wallpaper with an animation always centered in the current homescreen page, without loosing the extended background. What I'm doing right now is to draw my custom background bitmap, then draw some text on it.

This is my drawframe method:

        final SurfaceHolder holder = getSurfaceHolder();
        Canvas canvas = null;
        try {
            canvas = holder.lockCanvas();
            if (canvas != null) {

                if(mBackgroundBitmap != null) {
                    canvas.drawBitmap(mBackgroundBitmap, 0, 0, null);
                } else {
                    canvas.drawColor(Color.BLACK);
                }
                drawText(canvas);
            }
        }
        finally {
            if (canvas != null ) holder.unlockCanvasAndPost(canvas);
        }

It does work but obviousely when I change page on the homescreen I got a fixed background image and not a different "portion" like when I use a big Wallpaper.

I've tried also to set the wallpaper before locking the canvas but it doesn't work like expected:

if(mBackgroundBitmap != null) {                 
                try {
                    setWallpaper(mBackgroundBitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
final SurfaceHolder holder = getSurfaceHolder();
    Canvas canvas = null;
    try {
        canvas = holder.lockCanvas();
        if (canvas != null) {
            drawText(canvas);
        }
    }
    finally {
        if (canvas != null ) holder.unlockCanvasAndPost(canvas);
    }

What can I do to mantain the big background "mobile" when changing homescreen pages, but add some animation centered in the current page?


Solution

  • I've found out how to resolve it!

        void drawFrame() {
            final SurfaceHolder holder = getSurfaceHolder();
    
            Canvas canvas = null;
            try {
                canvas = holder.lockCanvas();
                if (canvas != null) {
                    canvas.save();
                    canvas.translate((float) mxOffset, 0f);
    
                    if(mBackgroundBitmap != null) {
                        canvas.drawBitmap(mBackgroundBitmap, 0, 0, null);
                    }
    
                    canvas.restore();
                }
            }
            finally {
                if (canvas != null ) holder.unlockCanvasAndPost(canvas);
            }
        }
    

    mBackgroundBitmap is the Bitmap I want to draw as Wallpaper and its width is the double of the screen width.

    The mxOffsets is taken in the overrided onOffsetsChanged:

        @Override
        public void onOffsetsChanged(float xOffset, float yOffset,
                float xOffsetStep, float yOffsetStep, int xPixelOffset,
                int yPixelOffset) {
            super.onOffsetsChanged(mxOffset, yOffset, xOffsetStep, yOffsetStep,
                    xPixelOffset, yPixelOffset);
    
            mxOffset = xPixelOffset;
            drawFrame();
        }