Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8179313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:58:29+00:00 2026-06-06T23:58:29+00:00

I had written a code to move a ball with the help of accelerometer

  • 0

I had written a code to move a ball with the help of accelerometer in android such that if it touches any of 4 edges it reappears on opposide edge my problems are

1.ball is not reappearing on opposite edge
2.orientation changes to lanscape

here is my code

 public class Accelerate extends Activity implements SensorEventListener {

float x, y, sensorX, sensorY, a, b, centerX, centerY;
Bitmap ball;
SensorManager sm;
NixSurface ourSurfaceView;

public class NixSurface extends SurfaceView implements Runnable {

    SurfaceHolder ourHolder;
    Thread ourThread = null;
    boolean isRunning = false;

    public NixSurface(Context context) {
        super(context);
        ourHolder = getHolder();

    }

    public void pause() {
        isRunning = false;
        while (true) {
            try {
                ourThread.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
        ourThread = null;
    }

    public void resume() {
        isRunning = true;
        ourThread = new Thread(this);
        ourThread.start();
    }

    public void run() {
        // TODO Auto-generated method stub
        while (isRunning) {
            if (!ourHolder.getSurface().isValid())
                continue;

            Canvas canvas = ourHolder.lockCanvas();

            canvas.drawRGB(255, 255, 255);
            centerX = canvas.getWidth() / 2;
            centerY = canvas.getHeight() / 2;
            x += sensorX;
            y += sensorY;
            a = centerX + x;
            b = centerY + y;

            if (a > canvas.getWidth())
                a = 0;
            if (b > canvas.getHeight())
                b = 0;
            if (a < 0)
                a = canvas.getWidth();
            if (b < 0)
                b = canvas.getHeight();

            canvas.drawBitmap(ball, a, b, null);
            ourHolder.unlockCanvasAndPost(canvas);
        }
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
        Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
        sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
    }
    x = y = sensorY = sensorX = 0;
    ball = BitmapFactory.decodeResource(getResources(), R.drawable.nix);
    ourSurfaceView = new NixSurface(this);
    ourSurfaceView.resume();
    setContentView(ourSurfaceView);
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    sm.unregisterListener(this);
    super.onStop();
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {
    // TODO Auto-generated method stub
    // nothing to do here
}

public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub

    try {
        Thread.sleep(16);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    sensorX = -event.values[0];
    sensorY = event.values[1];
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:58:29+00:00Added an answer on June 6, 2026 at 11:58 pm

    I guess your problem is that you assume that the sensor origin (x=0,y=0) is the screen center. The sensor origin is the top left corner of the screen (X pointing left and Y pointing down), so comparing a & b to canvas.getWidth() & canvas.getHeight() is not correct since you already added centerX & centerY. Also, negating sensorX will cause the condition a > canvas.getWidth() to never occur since X is never negative.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was overlooking some code that I had written to generate an A-Z navigation
I had written C++ code for a problem. After that i have tried to
i had written a code in t-sql that will create a table in sql
I recently inherited some code that someone else had written. I discovered that everywhere
I had written a jquery code to hide some labels, textbox contained in tables
My code base initially was written in ruby. It had a rakefile.rb file to
I'm having a problem with some code I've written. I've had to anonymize it,
I used FxCop to analyze some code I had written. I had exposed a
I originally had this code written as a series of if statements, but after
I had written the code given below: Its output datatype is integers, I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.