动态壁纸和扩展的背景 [英] Live Wallpaper and extended background

查看:170
本文介绍了动态壁纸和扩展的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个总是在主屏幕当前页面为中心的动画动态壁纸,没有松动的背景延伸。
我在做什么,现在是吸引我的自定义背景位图,然后绘制一些文本。

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

它的工作,但obviousely当我改变主屏幕上页我喜欢当我使用一个大的壁纸一个固定的背景图片,而不是一个不同的部分。

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?

推荐答案

我已经找到了如何解决它!

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是我想提请为墙纸的位图,它的宽度是屏幕宽度的两倍。

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

该mxOffsets是采取在overrided onOffsetsChanged:

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

这篇关于动态壁纸和扩展的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆