'scrolling background in game android studio.Background moves towards right with screen blinking how to solve it

I want to implement a scrolling background in my game in android studio.

My idea was to have two Bitmaps, who are drawn next to each other and to reset the position of a bitmap if it is not the screen anymore

  1. The background moves right
  2. the screen is blinking

How can I solve my problems?

my SurfaceView class

public class GameView extends SurfaceView implements Runnable {

  private Thread thread; // draw + update thread

  int screenx, screeny; // screen size
  Background background1 , background2;

  float screenratiox, screenratioy;

  private boolean isplaying;

  Paint paint;

  public GameView(Context context, int screenx, int screeny) {
    super(context);

    this.screenx = screenx;
    this.screeny = screeny;

    this.screenratiox = 1920f / screenx;
    this.screenratioy = 1080f / screeny;

    background1 = new Background(screenx, screeny, getResources());
    background2 = new Background(screenx, screeny, getResources());

    background2.x = screenx;

    paint = new Paint();
  }

  @Override
  public void run() {
    while (isplaying) {
      update();
      sleep();
      draw();
    }
  }

  public void sleep() {
    try {
      Thread.sleep(17);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  public void resume() {
    isplaying = true ;
    thread = new Thread(this) ;
    thread.start();
  }

  public void pause() {
    try {
      isplaying = false ;
      thread.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  private void update() { //should move the background and reset it, if beyond the screen
    background1.x += 10 * screenratiox; // move
    background2.x += 10 * screenratioy; // move

    if (background1.x + background1.background.getWidth() < 0) {
      background1.x = screenx; //reset x
    } 

    if (background2.x + background2.background.getWidth() < 0) {
      background2.x = screenx; //reset x
    } 
  }

  private void draw () {
    if (getHolder().getSurface().isValid()) {

      Canvas canvas = getHolder().lockCanvas();

      canvas.drawBitmap(background1.background, background1.x, background1.y, paint);
      canvas.drawBitmap(background2.background, background2.x, background2.y, paint);

      getHolder().unlockCanvasAndPost(canvas);
    }
  }
}

my Background class

public class Background {
  int x = 0, y = 0; // position
  Bitmap background; // image

  Background(int screenx , int screeny , Resources res) {
    background = BitmapFactory.decodeResource(res, R.drawable.background);
    background = Bitmap.createScaledBitmap(background, screenx, screeny, false);
  }
}


Solution 1:[1]

background1.x += 10 * screenratiox; // move

background2.x += 10 * screenratioy; // move

literally means: move the x to the right.

So

background1.x -= 10 * screenratiox; // move 
background2.x -= 10 * screenratioy; // move

Solution 2:[2]

background1.x -= (int)(10 * screenratiox);

background2.x -= (int)(10 * screenratiox);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Lp Pung