我正在尝试创建一个动态壁纸,该动画始终位于当前主屏幕页面的中心,而不丢失扩展的背景.
我现在正在做的是绘制自定义背景位图,然后在其上绘制一些文本.
这是我的并条机方法:
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);
}
它确实可以工作,但是很明显,当我在主屏幕上更改页面时,得到的背景图像是固定的,而不是像使用大墙纸时一样的“部分”.
我也尝试过在锁定画布之前设置墙纸,但是它无法正常工作:
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);
}
更改主屏幕页面时,我该怎么做才能保留大背景“移动设备”,但是在当前页面中添加一些动画?
解决方法:
我已经找到了解决方法!
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是我要绘制为墙纸的位图,它的宽度是屏幕宽度的两倍.
mxOffsets在重写的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();
}