Search code examples
javaandroidsensorsgravitysensormanager

Moving an ImageView according to gravity?


I am trying to make an ImageView move according to however the user tilts the device. I'm still learning in Android development so i don't know much about Sensors. I have tried using this but i'm not sure what to do afterwards but I'm guessing that I'm on the right track.

ImageView image = (ImageView) findViewById(R.id.image);
SensorManager mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor mSensor =  mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);

Any tips would help Thank you!


Solution

  • Have a look at this SO question for more understanding about gravity.

    Try to learn how compass works bye this example

    I guess your looking for solution

    ImageView move according to however the user tilts the device

    which is exactly look like this accelerometer example

    enter image description here