使用视差滚动制作Android动态壁纸

Nol*_*esh 6 android

我正在创建一个带有视差滚动的动态壁纸.我读过这篇文章:动态壁纸背景的视差效果滚动.但是当我更换桌面时,背景移动方式错误(如果我从左到右更换桌面,图片从右向左移动).如何改变方向?代码段:

public void Init(Bitmap bitmap){
  bg = new BitmapFactory().decodeResource(context.getResources(), R.drawable.thunder);
  bg = Bitmap.createScaledBitmap(bg, (int)(width*1.4), height, true);
}

float dx = 0.0f; 
@Override
    public void onOffsetsChanged(float xOffset, float yOffset,
            float xStep, float yStep, int xPixels, int yPixels) {
        dx = (width - bg.getWidth()) * (1 - xOffset);
    } 

private void doDraw(Canvas canvas) {
    canvas.save();
    canvas.translate(dx, 0);
    canvas.drawBitmap(bg, 0, 0, null);

    canvas.restore();
}   
Run Code Online (Sandbox Code Playgroud)

Mus*_*key 1

这里的算法需要改变:

dx = (width - bg.getWidth()) * (1 - xOffset);
Run Code Online (Sandbox Code Playgroud)

应改为类似以下内容:

dx = (width) * (xOffset);
Run Code Online (Sandbox Code Playgroud)

另一种方法是直接通过如下方式访问 canvas.drawBitmap() 方法:

canvas.drawBitmap(bg, xPixels, 0, null);
Run Code Online (Sandbox Code Playgroud)