Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Is this not the same question as your last question <a href="https://stackoverflow.com/questions/6457768/moving-an-image-using-accelerometer-of-anroid">here?!</a> You should just edit/expand your original question rather that starting a new one!</p> <p>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. </p> <p>For example: </p> <pre><code>newspeed = oldSpeed + (acceleration * time) distance = (original speed*time) + (0.5 * acceleration * time^2). </code></pre> <p>Where you use sensor inputs to set the acceleration, then just add the calculated distance to the balls position.</p> <hr> <p>EDIT - Code as requested.</p> <p>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!</p> <pre><code>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 &amp; 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 &gt; xmax) { xPosition = xmax; } else if (xPosition &lt; 0) { xPosition = 0; } if (yPosition &gt; ymax) { yPosition = ymax; } else if (yPosition &lt; 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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload