Take screen rotation into account
Change-Id: I354c58eb1d28552d46b8fa269e12c00eaf6cbb99
This commit is contained in:
@@ -30,7 +30,10 @@ import android.os.Bundle;
|
|||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.os.PowerManager.WakeLock;
|
import android.os.PowerManager.WakeLock;
|
||||||
import android.util.DisplayMetrics;
|
import android.util.DisplayMetrics;
|
||||||
|
import android.view.Display;
|
||||||
|
import android.view.Surface;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.view.WindowManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is an example of using the accelerometer to integrate the device's
|
* This is an example of using the accelerometer to integrate the device's
|
||||||
@@ -49,6 +52,8 @@ public class AccelerometerPlayActivity extends Activity {
|
|||||||
private SimulationView mSimulationView;
|
private SimulationView mSimulationView;
|
||||||
private SensorManager mSensorManager;
|
private SensorManager mSensorManager;
|
||||||
private PowerManager mPowerManager;
|
private PowerManager mPowerManager;
|
||||||
|
private WindowManager mWindowManager;
|
||||||
|
private Display mDisplay;
|
||||||
private WakeLock mWakeLock;
|
private WakeLock mWakeLock;
|
||||||
|
|
||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
@@ -62,6 +67,10 @@ public class AccelerometerPlayActivity extends Activity {
|
|||||||
// Get an instance of the PowerManager
|
// Get an instance of the PowerManager
|
||||||
mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
|
mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
|
||||||
|
|
||||||
|
// Get an instance of the WindowManager
|
||||||
|
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
|
||||||
|
mDisplay = mWindowManager.getDefaultDisplay();
|
||||||
|
|
||||||
// Create a bright wake lock
|
// Create a bright wake lock
|
||||||
mWakeLock = mPowerManager.newWakeLock(
|
mWakeLock = mPowerManager.newWakeLock(
|
||||||
PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName());
|
PowerManager.SCREEN_BRIGHT_WAKE_LOCK, getClass().getName());
|
||||||
@@ -392,9 +401,33 @@ public class AccelerometerPlayActivity extends Activity {
|
|||||||
* record the accelerometer data, the event's timestamp as well as
|
* record the accelerometer data, the event's timestamp as well as
|
||||||
* the current time. The latter is needed so we can calculate the
|
* the current time. The latter is needed so we can calculate the
|
||||||
* "present" time during rendering.
|
* "present" time during rendering.
|
||||||
|
*
|
||||||
|
* In this application, we need to take into account how the
|
||||||
|
* screen is rotated with respect to the sensors (which always
|
||||||
|
* return data in a coordinate space aligned to with the screen
|
||||||
|
* in its native orientation).
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
switch (mDisplay.getRotation()) {
|
||||||
|
case Surface.ROTATION_0:
|
||||||
mSensorX = event.values[0];
|
mSensorX = event.values[0];
|
||||||
mSensorY = event.values[1];
|
mSensorY = event.values[1];
|
||||||
|
break;
|
||||||
|
case Surface.ROTATION_90:
|
||||||
|
mSensorX = -event.values[1];
|
||||||
|
mSensorY = event.values[0];
|
||||||
|
break;
|
||||||
|
case Surface.ROTATION_180:
|
||||||
|
mSensorX = -event.values[0];
|
||||||
|
mSensorY = -event.values[1];
|
||||||
|
break;
|
||||||
|
case Surface.ROTATION_270:
|
||||||
|
mSensorX = event.values[1];
|
||||||
|
mSensorY = -event.values[0];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
mSensorTimeStamp = event.timestamp;
|
mSensorTimeStamp = event.timestamp;
|
||||||
mCpuTimeStamp = System.nanoTime();
|
mCpuTimeStamp = System.nanoTime();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user