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 6064055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:14:18+00:00 2026-05-23T09:14:18+00:00

i have done developed a sample application with the help of tutorials posted on

  • 0

i have done developed a sample application with the help of tutorials posted on web. my aim is to access the accelerometer and move a ball as per the phone orientation. i was successful to certail extent. But i have two isses

  1. the ball is going out of bound of the screen
  2. the ball movement is not smooth ( looks it disappears and re appears on the screen)

here is my code. is there any change i need to do to get the smooth and exact movement of the ball as we see in lot of games.

public class Accelerometer extends Activity implements SensorEventListener{
    /** Called when the activity is first created. */
    CustomDrawableView mCustomDrawableView = null;
    ShapeDrawable mDrawable = new ShapeDrawable();
    public static int x;
    public static int y;
       private Bitmap mBitmap;
       private Bitmap mWood;
    private SensorManager sensorManager = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        // Get a reference to a SensorManager
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mCustomDrawableView = new CustomDrawableView(this);
        setContentView(mCustomDrawableView);
        // setContentView(R.layout.main);

    }

    // This method will update the UI on new sensor events
    public void onSensorChanged(SensorEvent sensorEvent)
    {
        {
           /* if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                // the values you were calculating originally here were over 10000!
                x = (int) Math.pow(sensorEvent.values[0], 2); 
                y = (int) Math.pow(sensorEvent.values[1], 2);

            }*/

            if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
                  Display display = getWindowManager().getDefaultDisplay(); 
                  int xmax = display.getWidth();
                  int ymax = display.getHeight();
                  x = (int) Math.pow(sensorEvent.values[1], 2); 
                  y = (int) Math.pow(sensorEvent.values[2], 2);
                  if (x > xmax) {
                      x = xmax;
                  } else if (x < -xmax) {
                      x = -xmax;
                  }
                  if (y > ymax) { 
                      y = ymax;
                  } else if (y < -ymax) {
                      y = -ymax;
                  }

            }
        }
    }

    // I've chosen to not implement this method
    public void onAccuracyChanged(Sensor arg0, int arg1)
    {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onResume()
    {
        super.onResume();
        // Register this class as a listener for the accelerometer sensor
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_GAME);
        // ...and the orientation sensor
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_NORMAL); 
    }

    @Override
    protected void onStop()
    {
        // Unregister the listener
        sensorManager.unregisterListener(this);
        super.onStop();
    }

    public class CustomDrawableView extends View
    {


        public CustomDrawableView(Context context)
        {
            super(context);

          Bitmap ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
          final int dstWidth = 50; 
          final int dstHeight = 50; 
          mBitmap = Bitmap.createScaledBitmap(ball, dstWidth, dstHeight, true);
          mWood = BitmapFactory.decodeResource(getResources(), R.drawable.wood);

        }

        protected void onDraw(Canvas canvas)
        {

            final Bitmap bitmap = mBitmap;

            canvas.drawBitmap(mWood, 0, 0, null);
            canvas.drawBitmap(bitmap, x, y, null);

            invalidate();
        }
    }
}
  • 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-05-23T09:14:18+00:00Added an answer on May 23, 2026 at 9:14 am

    Is this not the same question as your last question here?! You should just edit/expand your original question rather that starting a new one!

    But basically to make it realistic you want to move the ball by using x/y speed rather than just changing the position. So you want to have a loop which draws the ball and you want to know the time difference between the current loop and the previous, then you can use simple kinematics equations to work out the position change.

    For example:

    newspeed = oldSpeed + (acceleration * time)
    distance = (original speed*time) + (0.5 * acceleration * time^2).
    

    Where you use sensor inputs to set the acceleration, then just add the calculated distance to the balls position.


    EDIT – Code as requested.

    Your lucky you found a bored games programmer! It’s not perfect by any means but its working for you. You should buy yourself a game development book and have a look at using open GL for this as it would be a lot better!

    public class test2 extends Activity implements SensorEventListener{
    
    CustomDrawableView mCustomDrawableView = null;
    ShapeDrawable mDrawable = new ShapeDrawable();
    public float xPosition, xAcceleration,xVelocity = 0.0f;
    public float yPosition, yAcceleration,yVelocity = 0.0f;
    public float xmax,ymax;
    private Bitmap mBitmap;
    private Bitmap mWood;
    private SensorManager sensorManager = null;
    public float frameTime = 0.666f;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    
        super.onCreate(savedInstanceState);
    
        //Set FullScreen & portrait
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
        // Get a reference to a SensorManager
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME);
    
        mCustomDrawableView = new CustomDrawableView(this);
        setContentView(mCustomDrawableView);
        // setContentView(R.layout.main);
    
        //Calculate Boundry
        Display display = getWindowManager().getDefaultDisplay();
        xmax = (float)display.getWidth() - 50;
        ymax = (float)display.getHeight() - 50;
    }
    
    // This method will update the UI on new sensor events
    public void onSensorChanged(SensorEvent sensorEvent)
    {
        {
            if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
                //Set sensor values as acceleration
                yAcceleration = sensorEvent.values[1]; 
                xAcceleration = sensorEvent.values[2];
                updateBall();
            }
        }
    }
    
    private void updateBall() {
    
    
        //Calculate new speed
        xVelocity += (xAcceleration * frameTime);
        yVelocity += (yAcceleration * frameTime);
    
        //Calc distance travelled in that time
        float xS = (xVelocity/2)*frameTime;
        float yS = (yVelocity/2)*frameTime;
    
        //Add to position negative due to sensor 
        //readings being opposite to what we want!
        xPosition -= xS; 
        yPosition -= yS;
    
        if (xPosition > xmax) {
            xPosition = xmax;
        } else if (xPosition < 0) {
            xPosition = 0;
        }
        if (yPosition > ymax) { 
            yPosition = ymax;
        } else if (yPosition < 0) {
            yPosition = 0;
        }
    }
    
    // I've chosen to not implement this method
    public void onAccuracyChanged(Sensor arg0, int arg1)
    {
        // TODO Auto-generated method stub
    }
    
    @Override
    protected void onResume()
    {
        super.onResume();
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_GAME); 
    }
    
    @Override
    protected void onStop()
    {
        // Unregister the listener
        sensorManager.unregisterListener(this);
        super.onStop();
    }
    
    public class CustomDrawableView extends View
    {
        public CustomDrawableView(Context context)
        {
            super(context);
            Bitmap ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
            final int dstWidth = 50; 
            final int dstHeight = 50; 
            mBitmap = Bitmap.createScaledBitmap(ball, dstWidth, dstHeight, true);
            mWood = BitmapFactory.decodeResource(getResources(), R.drawable.wood);
    
        }
    
        protected void onDraw(Canvas canvas)
        {
            final Bitmap bitmap = mBitmap;
            canvas.drawBitmap(mWood, 0, 0, null);
            canvas.drawBitmap(bitmap, xPosition, yPosition, null);
            invalidate();
        }
    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a sample application in which I am able to implement help
I work with a actively developed production web application that is written entirely in
I have developed an iPhone application that was rejected. With this application I offer
I am to develop an application on windows. I have never done that before
I have a large .NET web application. The system has projects for different intentions
So, being an early adopter, I have developed an application based on SQLCE4, ASP.Net
I have developed an application that runs in a Kiosk-mode by presenting itself as
I have a drawing application developed in winforms C# which uses many System.Drawing.Bitmap object
I have done Java and JSP programming in the past, but I am new
I have done a bit of testing on this myself (During the server side

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.