3

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?

Simon Sarris
  • 62,212
  • 13
  • 141
  • 171
Santacrab
  • 3,165
  • 2
  • 25
  • 31

1 Answers1

3

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();
    }
Santacrab
  • 3,165
  • 2
  • 25
  • 31
  • Hi. Actually I am doing the same thing but I want the animation to appear in all the home screen pages. How to achieve that. Can you suggest anything. – android developer Dec 05 '12 at 10:28
  • @anuja simply add your mxOffset also to the animation and not only to the background – Santacrab Jan 18 '13 at 15:15
  • hi santacrab i have done what u say in my code and it works fine on GO LAUNCHER home screen but when i switch to my native android home screen from GO LAUNCHER home screen my live wallpaper stops parallax scrolling.Please help me to get rid of this problem – himanshu May 23 '13 at 11:09
  • @himanshu probably your native homescreen dowsn't support the "extended" background feature. Since I work on a lot of devices I often encountered those kind of homescreen. To know if they support it, usually is enough to try to select a background image with the native wallpaper selector and check if it allows to crop an image with an aspect ratio different from the display size. – Santacrab May 23 '13 at 14:42