0
\$\begingroup\$

I'm developing an android app and want to make move the background to the right. The problem that I have is if it moves it does not repeat the background. I appreciate every suggestion. Thanks.

The code:

public class Background
{
    private Bitmap bmp = null;
    private int newWidth = 0;
    private int newHeight = 0;
    private Bitmap bmpScaled = null;
    private int move = 0;
    private float scale = 0;
    private GameView gameView = null;

    public Background(GameView gameView, Bitmap bmp, int x, int y)
    {
        this.gameView = gameView;
        this.bmp = bmp;
        scale = (float) bmp.getHeight() / (float) gameView.getHeight();
        newWidth = Math.round(bmp.getWidth() / scale);
        newHeight = Math.round(bmp.getHeight() / scale);
        bmpScaled = Bitmap.createScaledBitmap(bmp, newWidth, newHeight, true);
    }

    private void start()
    {
        move++;
    }

    private void update()
    {
        start();
    }

    public void onDraw(Canvas canvas)
    {
        update();
        canvas.drawBitmap(bmpScaled, move, 0, null);
    }
}
\$\endgroup\$
1
\$\begingroup\$

If you want to repeat the background you will actually need to call drawBitmap twice.

Consider what would happen if you wanted to repeat a very small background. You would need to tile it several times to cover the area. In this case, you need to tile it twice - once for each screen width.

So it should be something like

canvas.drawBitmap(bmpScaled, move % newWidth, 0, null);
canvas.drawBitmap(bmpScaled, (move % newWidth) - newWidth, 0, null);
\$\endgroup\$
  • 1
    \$\begingroup\$ Shouldn't it be "- newWidth" ? \$\endgroup\$ – Heckel Jun 24 '14 at 9:38
  • \$\begingroup\$ Yeah. I also wonder if void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) would be better, but I'm not too familiar with the deep workings of android and how it handles overdraw. \$\endgroup\$ – jzx Jun 24 '14 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.