我对Parallax的(非常基本和粗略的)实现有点问题。基本上我有一个图像应该以与背景略有不同的速率在屏幕上移动。它这样做 - 直到它到达图像的末尾。它似乎循环回到图像的开头 - 但是当它到达图像的末尾时,似乎将位图拉伸到整个屏幕的长度,直到它完全展开 - 然后它自己重置。 我无法附上截图(代码不够),但这里是代码。
Init方法:
private void init(Context context) {
screenSize = new Point();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getSize(screenSize);
backGroundViewPort = new Rect();
cityscapeRect = new Rect();
backGroundViewPort.set(0, 0, 400, 300);
GGRAssetManager ggrAssetManager = new GGRAssetManager(context);
background = ggrAssetManager.loadImage("background.jpg");
cityscape = ggrAssetManager.loadImage("cityscape3.png");
clouds = ggrAssetManager.loadImage("clouds.png");
backGroundViewPort.set(0, 0, 400, 300);
}
DoUpdate方法:
public void doUpdate() {
Game.getInstance().viewport.set(Math.max(0, (int) (game.getPlayer().getxPos() - screenSize.x / 2)),
0,
(int) (game.getPlayer().getxPos() + (screenSize.x / 2)),
screenSize.y);
if (backGroundViewPort.right + 1 >= background.getWidth()) {
backGroundViewPort.left = 0;
backGroundViewPort.right = 400;
} else {
backGroundViewPort.right += 1;
backGroundViewPort.left += 1;
}
if (cityscapeRect.right + 1 >= cityscape.getWidth()) {
cityscapeRect.left = cityscape.getWidth() - screenSize.x;
cityscapeRect.right = 0;
} else {
cityscapeRect.right += 1;
cityscapeRect.left += 1;
}
}
DoDraw方法:
public void doDraw(Canvas canvas) {
//PARALLAX
backGroundRect.set(0, 0, screenSize.x, screenSize.y);
canvas.drawBitmap(background, backGroundViewPort, backGroundRect, null);
cityscapeRect.set(0, screenSize.y/2, (screenSize.x), screenSize.y);
Paint paint = new Paint();
paint.setAlpha(150);
canvas.drawBitmap(cityscape, backGroundViewPort, cityscapeRect, paint);
}
我确定它与矩形有关,但我不知道如何解决它。非常感谢帮助。