title: Bounce ball back android
body: <p>I have take Example of Accelerator with SensorManager, in which canvas(ball) are get update its position as per device Accelerator are rotated. Here is image :</p>
  
  <p><img src="https://i.stack.imgur.com/eF2Zs.png" alt="enter image description here"></p>
  
  <p>As shown in the image there is a ball and one line. The ball's position is frequently updated, while the line's position is static.</p>
  
  <p>I would like to have the ball bounce back when it touches the line. I have tried since from 3 day, but don't understand how I can do this.</p>
  
  <p><strong>here is my code:</strong>   </p>
  
  <pre><code>public class ballsensor extends Activity implements SensorEventListener {
  
      // sensor-related
      private SensorManager mSensorManager;
      private Sensor mAccelerometer;
  
      // animated view
      private ShapeView mShapeView;
  
      // screen size
      private int mWidthScreen;
      private int mHeightScreen;
  
      // motion parameters
      private final float FACTOR_FRICTION = 0.5f; // imaginary friction on the
                                                  // screen
      private final float GRAVITY = 9.8f; // acceleration of gravity
      private float mAx; // acceleration along x axis
      private float mAy; // acceleration along y axis
      private final float mDeltaT = 0.5f; // imaginary time interval between each
                                          // acceleration updates
  
      // timer
      private Timer mTimer;
      private Handler mHandler;
      private boolean isTimerStarted = false;
      private long mStart;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          // set the screen always portait
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  
          // initializing sensors
          mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
          mAccelerometer = mSensorManager
                  .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  
          // obtain screen width and height
          Display display = ((WindowManager) this
                  .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
          mWidthScreen = display.getWidth();
          mHeightScreen = display.getHeight() - 35;
  
          // initializing the view that renders the ball
          mShapeView = new ShapeView(this);
          mShapeView.setOvalCenter((int) (mWidthScreen * 0.6),
                  (int) (mHeightScreen * 0.6));
  
          setContentView(mShapeView);
  
      }
  
      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {
  
      }
  
      @Override
      public void onSensorChanged(SensorEvent event) {
          // obtain the three accelerations from sensors
          mAx = event.values[0];
          mAy = event.values[1];
  
          float mAz = event.values[2];
  
          // taking into account the frictions
          mAx = Math.signum(mAx) * Math.abs(mAx)
                  * (1 - FACTOR_FRICTION * Math.abs(mAz) / GRAVITY);
          mAy = Math.signum(mAy) * Math.abs(mAy)
                  * (1 - FACTOR_FRICTION * Math.abs(mAz) / GRAVITY);
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          // start sensor sensing
          mSensorManager.registerListener(this, mAccelerometer,
                  SensorManager.SENSOR_DELAY_NORMAL);
  
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          // stop senser sensing
          mSensorManager.unregisterListener(this);
      }
  
      // the view that renders the ball
      private class ShapeView extends SurfaceView implements
              SurfaceHolder.Callback {
  
          private final int RADIUS = 30;
          private final float FACTOR_BOUNCEBACK = 0.50f;
  
          private int mXCenter;
          private int mYCenter;
          private RectF mRectF;
          private final Paint mPaint;
          private ShapeThread mThread;
  
          private float mVx;
          private float mVy;
  
          public ShapeView(Context context) {
              super(context);
  
              getHolder().addCallback(this);
              mThread = new ShapeThread(getHolder(), this);
              setFocusable(true);
  
              mPaint = new Paint();
              mPaint.setColor(0xFFFFFFFF);
              mPaint.setAlpha(192);
              mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
              mPaint.setAntiAlias(true);
  
              mRectF = new RectF();
          }
  
          // set the position of the ball
          public boolean setOvalCenter(int x, int y) {
              mXCenter = x;
              mYCenter = y;
              return true;
          }
  
          // calculate and update the ball's position
          public boolean updateOvalCenter() {
              mVx -= mAx * mDeltaT;
              mVy += mAy * mDeltaT;
  
              System.out.println("mVx is ::" + mVx);
              System.out.println("mVy is ::" + mVy);
  
              mXCenter += (int) (mDeltaT * (mVx + 0.6 * mAx * mDeltaT));
              mYCenter += (int) (mDeltaT * (mVy + 0.6 * mAy * mDeltaT));
  
              if (mXCenter &lt; RADIUS) {
                  mXCenter = RADIUS;
                  mVx = -mVx * FACTOR_BOUNCEBACK;
              }
  
              if (mYCenter &lt; RADIUS) {
                  mYCenter = RADIUS;
                  mVy = -mVy * FACTOR_BOUNCEBACK;
              }
              if (mXCenter &gt; mWidthScreen - RADIUS) {
                  mXCenter = mWidthScreen - RADIUS;
                  mVx = -mVx * FACTOR_BOUNCEBACK;
              }
  
              if (mYCenter &gt; mHeightScreen - 2 * RADIUS) {
                  mYCenter = mHeightScreen - 2 * RADIUS;
                  mVy = -mVy * FACTOR_BOUNCEBACK;
              }
  
              return true;
          }
  
          // update the canvas.
          @Override
          protected void onDraw(Canvas canvas) {
              if (mRectF != null) {
                  mRectF.set(mXCenter - RADIUS, mYCenter - RADIUS, mXCenter
                          + RADIUS, mYCenter + RADIUS);
                  canvas.drawColor(0XFF000000);
                  // canvas.drawOval(mRectF, mPaint);
  
                  Bitmap kangoo = BitmapFactory.decodeResource(getResources(),
                          R.drawable.stripe1);
  
                  Bitmap ball = BitmapFactory.decodeResource(getResources(),
                          R.drawable.blackwhiteball);
  
                  canvas.drawBitmap(ball, mXCenter - RADIUS, mYCenter - RADIUS,
                          mPaint);
                  canvas.drawBitmap(kangoo, 130, 10, null);
  
              }
          }
  
          @Override
          public void surfaceChanged(SurfaceHolder holder, int format, int width,
                  int height) {
          }
  
          @Override
          public void surfaceCreated(SurfaceHolder holder) {
              mThread.setRunning(true);
              mThread.start();
          }
  
          @Override
          public void surfaceDestroyed(SurfaceHolder holder) {
              boolean retry = true;
              mThread.setRunning(false);
              while (retry) {
                  try {
                      mThread.join();
                      retry = false;
                  } catch (InterruptedException e) {
  
                  }
              }
          }
      }
  
      class ShapeThread extends Thread {
          private SurfaceHolder mSurfaceHolder;
          private ShapeView mShapeView;
          private boolean mRun = false;
  
          public ShapeThread(SurfaceHolder surfaceHolder, ShapeView shapeView) {
              mSurfaceHolder = surfaceHolder;
              mShapeView = shapeView;
          }
  
          public void setRunning(boolean run) {
              mRun = run;
          }
  
          public SurfaceHolder getSurfaceHolder() {
              return mSurfaceHolder;
          }
  
          @Override
          public void run() {
              Canvas c;
              while (mRun) {
                  mShapeView.updateOvalCenter();
                  c = null;
                  try {
                      c = mSurfaceHolder.lockCanvas(null);
                      synchronized (mSurfaceHolder) {
                          mShapeView.onDraw(c);
                      }
                  } finally {
                      if (c != null) {
                          mSurfaceHolder.unlockCanvasAndPost(c);
                      }
                  }
              }
          }
      }
  }
  </code></pre>