Live Wallpaper and extended background

大兔子大兔子 提交于 2019-12-07 17:31:53

问题


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?


回答1:


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();
    }


来源:https://stackoverflow.com/questions/10816432/live-wallpaper-and-extended-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!