title: How I can move an image from left to right and right to left in android?
body: <p>I'm new in android. I want to move an image from left to right and right to left.When user touch right side image of the image it will move right side.Same thing will happen when user will touch right side of the image.But image will move predefine point across x-axis.</p>
  
  <p>For example: Image will move p1(100,100), p2(150,100), p3(200,100), p4(250,100) across those points sequentially. If user touch left side of p1, it will remain current position.Same thing will occurs at p4. I can move image from p1 to p2 and p2 to p1. when I add p3 and p4 it's not working as I expected. </p>
  
  <p>Here is my GameView class.</p>
  
  <pre><code>public class GameView extends SurfaceView{
      private Bitmap BG_image;
      private SurfaceHolder holder;
      //private GameLoopThread gameLoopThread;    
      int x;
      int y;
      private int srcX=100;
      private int srcY=100;
      int replaceX=0,areaThreshold=50;
      int distance=50;
  
      public GameView(Context context) {
  
          super(context);
          //gameLoopThread = new GameLoopThread(this);
          holder = getHolder();
          holder.addCallback(new SurfaceHolder.Callback() {
  
              @Override
              public void surfaceDestroyed(SurfaceHolder holder) {
  
  /*              boolean retry = true;
                  gameLoopThread.setRunning(false);
  
                  while (retry) {
  
                      try {
  
                          gameLoopThread.join();
                          retry = false;
  
                      } catch (InterruptedException e) {
  
                      }
                  }*/
              }
  
              @Override
              public void surfaceCreated(SurfaceHolder holder) {
  
  /*              gameLoopThread.setRunning(true);
                  gameLoopThread.start();*/
                  Canvas c = holder.lockCanvas(null);
                  onDraw(c);
                  holder.unlockCanvasAndPost(c);
              }
  
              @Override
              public void surfaceChanged(SurfaceHolder holder, int format,
  
                      int width, int height) {
              }
          });
          BG_image = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
          Bitmap.createScaledBitmap(BG_image, BG_image.getWidth(), BG_image.getHeight(), false);
      }
      @Override
      public boolean onTouchEvent(MotionEvent ev) {
          // TODO Auto-generated method stub
                 // x = (int) ev.getX();
                 // y = (int) ev.getY();
                  updateX(srcX);
                  replaceX=(int)ev.getX();
                  int StartX=0, EndX=0;
                  StartX=replaceX-areaThreshold;
                  EndX=replaceX+areaThreshold;
                  if(StartX&lt;=100){
                      SurfaceHolder holder=getHolder();
                      Canvas myCanvas=holder.lockCanvas(null);
                      onDraw(myCanvas);
                      holder.unlockCanvasAndPost(myCanvas);
                      srcX=100;
                  }
                  else if(StartX&gt;100 &amp;&amp; StartX&lt;250){
                      SurfaceHolder holder=getHolder();
                      Canvas myCanvas=holder.lockCanvas(null);
                      onDraw(myCanvas);
                      holder.unlockCanvasAndPost(myCanvas);
                      srcX=srcX+distance;
                  }
                  if(EndX&gt;100 &amp;&amp; EndX&lt;250){
                      SurfaceHolder holder=getHolder();
                      Canvas myCanvas=holder.lockCanvas(null);
                      onDraw(myCanvas);
                      holder.unlockCanvasAndPost(myCanvas);
                      srcX=srcX-distance;
                  }
                  else if(EndX&gt;250){
                      SurfaceHolder holder=getHolder();
                      Canvas myCanvas=holder.lockCanvas(null);
                      onDraw(myCanvas);
                      holder.unlockCanvasAndPost(myCanvas);
                      srcX=250;
                  }
  
          return super.onTouchEvent(ev);
      }
      @Override
      protected void onDraw(Canvas canvas) {
          // TODO Auto-generated method stub  
          updateX(srcX);
          Paint paint = new Paint();
          canvas.drawColor(Color.BLACK);
          canvas.drawRect(new Rect(0,0,getWidth(),getHeight()),paint);
          canvas.drawBitmap(BG_image, srcX, srcY, null);
      }
  
      private int updateX(int srcX){
          this.srcX =srcX;
          return srcX ;
  
      }
  }
  </code></pre>
  
  <p>Please review my code and give your valuable advice to solve my problem.
  Hopefully, I will get a reply soon!</p>