Moving an image using Accelerometer of android?

Use this code. You were never setting the location of the drawable after you intialized that class. You'll have to do some calculations to set the balls location properly.

The way you were doing it was getting values over 10000 which was drawing the oval off screen import android.app. Activity; import android.content. Context; import android.graphics.

Canvas; import android.graphics. Color; import android.graphics. Paint; import android.graphics.

RectF; import android.graphics.drawable. ShapeDrawable; import android.graphics.drawable.shapes. OvalShape; import android.hardware.

Sensor; import android.hardware. SensorEvent; import android.hardware. SensorEventListener; import android.hardware.

SensorManager; import android.os. Bundle; import android.view. View; 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 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. Values1, 2); y = (int) Math. Pow(sensorEvent.

Values2, 2); } if (sensorEvent.sensor.getType() == Sensor. TYPE_ORIENTATION) { } } } // 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_NORMAL); // ...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 { static final int width = 50; static final int height = 50; public CustomDrawableView(Context context) { super(context); mDrawable = new ShapeDrawable(new OvalShape()); mDrawable.getPaint(). SetColor(0xff74AC23); mDrawable.

SetBounds(x, y, x + width, y + height); } protected void onDraw(Canvas canvas) { RectF oval = new RectF(Accelerometer. X, Accelerometer. Y, Accelerometer.

X + width, Accelerometer. Y + height); // set bounds of rectangle Paint p = new Paint(); // set some paint options p. SetColor(Color.

BLUE); canvas. DrawOval(oval, p); invalidate(); } } }.

Use this code. You were never setting the location of the drawable after you intialized that class. You'll have to do some calculations to set the balls location properly.

The way you were doing it was getting values over 10000 which was drawing the oval off screen. Import android.app. Activity; import android.content.

Context; import android.graphics. Canvas; import android.graphics. Color; import android.graphics.

Paint; import android.graphics. RectF; import android.graphics.drawable. ShapeDrawable; import android.graphics.drawable.shapes.

OvalShape; import android.hardware. Sensor; import android.hardware. SensorEvent; import android.hardware.

SensorEventListener; import android.hardware. SensorManager; import android.os. Bundle; import android.view.

View; 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 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. Values1, 2); y = (int) Math.

Pow(sensorEvent. Values2, 2); } if (sensorEvent.sensor.getType() == Sensor. TYPE_ORIENTATION) { } } } // 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_NORMAL); // ...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 { static final int width = 50; static final int height = 50; public CustomDrawableView(Context context) { super(context); mDrawable = new ShapeDrawable(new OvalShape()); mDrawable.getPaint().

SetColor(0xff74AC23); mDrawable. SetBounds(x, y, x + width, y + height); } protected void onDraw(Canvas canvas) { RectF oval = new RectF(Accelerometer. X, Accelerometer.

Y, Accelerometer. X + width, Accelerometer. Y + height); // set bounds of rectangle Paint p = new Paint(); // set some paint options p.

SetColor(Color. BLUE); canvas. DrawOval(oval, p); invalidate(); } } }.

Thank you..my image now moves..have not gone through the code yet..just copy pasted and deployed onto my mobile. Thanks again – sankara rao bhatta Jun 24 at 18:26 if this is what worked for you make sure to mark it as the answer. – dymmeh Jun 24 at 18:46.

I think you need to invalidate your view in the onSensorChanged() method or at a specific fps rate that you have to implement.

Try using sensorEvent. Values0 for your xChange and sensorEvents. Values1 for your yChange if you want to use the accleleration sensor, if not use the same values and move it into the (sensorEvent.sensor.getType() == Sensor.

TYPE_ORIENTATION) if statement, this will give you the tilt of the handset rather than how quicly its moving along an axis. You also need to call invalidate(); on the View when you set or change a sensor. The Sensor.

TYPE_ACCELEROMETER returns: values0: Acceleration minus Gx on the x-axis values1: Acceleration minus Gy on the y-axis values2: Acceleration minus Gz on the z-axis The Sensor. TYPE_ORIENTATION returns: values0: Azimuth, angle between the magnetic north direction and the y-axis, around the z-axis (0 to 359). 0=North, 90=East, 180=South, 270=West values1: Pitch, rotation around x-axis (-180 to 180), with positive values when the z-axis moves toward the y-axis.

Values2: Roll, rotation around y-axis (-90 to 90), with positive values when the x-axis moves toward the z-axis.

Can you give me the code what I need to insert and where – sankara rao bhatta Jun 24 at 5:25 use @dymmeh answer. – Kenny Jun 24 at 17:34.

Null && mAccelerometerValues! RectF oval = new RectF(Actividad_Principal. X + width, Actividad_Principal.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions