本文整理汇总了Java中android.hardware.SensorManager.getOrientation方法的典型用法代码示例。如果您正苦于以下问题:Java SensorManager.getOrientation方法的具体用法?Java SensorManager.getOrientation怎么用?Java SensorManager.getOrientation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.hardware.SensorManager
的用法示例。
在下文中一共展示了SensorManager.getOrientation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onSensorChanged
import android.hardware.SensorManager; //导入方法依赖的package包/类
public void onSensorChanged(SensorEvent event) {
if (event.accuracy == SensorManager.SENSOR_STATUS_ACCURACY_LOW) return;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
arrayCopy(event.values, geomagnetic);
}
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
arrayCopy(event.values, gravity);
}
if (SensorManager.getRotationMatrix(R, I, gravity, geomagnetic)) {
SensorManager.getOrientation(R, orientation);
azimuth += easing * (orientation[0] - azimuth);
pitch += easing * (orientation[1] - pitch);
roll += easing * (orientation[2] - roll);
}
}
示例2: update
import android.hardware.SensorManager; //导入方法依赖的package包/类
protected void update(float[] vectors) {
int worldAxisX = SensorManager.AXIS_X;
int worldAxisZ = SensorManager.AXIS_Z;
float[] rotationMatrix = new float[9];
float[] adjustedRotationMatrix = new float[9];
float[] orientation = new float[3];
SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisZ, adjustedRotationMatrix);
SensorManager.getOrientation(adjustedRotationMatrix, orientation);
float roll = orientation[2] * FROM_RADS_TO_DEGS;
if (roll > -45 && roll < 45) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Log.d(LOG_TAG, "Requesting undefined");
}
Log.d(LOG_TAG, "Roll: " + roll);
}
示例3: onSensorChanged
import android.hardware.SensorManager; //导入方法依赖的package包/类
/**
* SensorEventListener method called when sensor values are updated. Reads gravitational and
* magnetic field information, and when both are available computes the orientation values
* and calls the delegate with them.
*/
@Override public void onSensorChanged(SensorEvent event) {
switch(event.sensor.getType()) {
case Sensor.TYPE_MAGNETIC_FIELD:
mags = event.values.clone();
break;
case Sensor.TYPE_ACCELEROMETER:
accels = event.values.clone();
break;
}
if (mags!=null && accels!=null) {
SensorManager.getRotationMatrix(R, I, accels, mags);
SensorManager.getOrientation(R, orientationValues);
delegate.receivedOrientationValues(
orientationValues[0], orientationValues[1], orientationValues[2]);
}
}
示例4: getBaseOrientation
import android.hardware.SensorManager; //导入方法依赖的package包/类
/**
* Calculates orientation angles from accelerometer and magnetometer output.
*/
protected float[] getBaseOrientation(float[] acceleration, float[] magnetic) {
// To get the orientation vector from the acceleration and magnetic
// sensors, we let Android do the heavy lifting. This call will
// automatically compensate for the tilt of the compass and fail if the
// magnitude of the acceleration is not close to 9.82m/sec^2. You could
// perform these steps yourself, but in my opinion, this is the best way
// to do it.
float[] rotationMatrix = new float[9];
if (SensorManager.getRotationMatrix(rotationMatrix, null, acceleration, magnetic)) {
float[] baseOrientation = new float[3];
SensorManager.getOrientation(rotationMatrix, baseOrientation);
return baseOrientation;
}
return null;
}
示例5: onSensorChanged
import android.hardware.SensorManager; //导入方法依赖的package包/类
@Override
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
lowPass(event.values, mGravity);
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
lowPass(event.values, mGeomagnetic);
}
float R[] = new float[9];
float I[] = new float[9];
if (SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic)) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
mOrientation[0] = (float)Math.toDegrees(orientation[0]);
adjustArrow();
}
}
}
示例6: onSensorChanged
import android.hardware.SensorManager; //导入方法依赖的package包/类
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor == compas){
System.arraycopy(event.values,0,comp,0,event.values.length);
c=true;
}
else if(event.sensor == accelerometer){
System.arraycopy(event.values,0,acc,0,event.values.length);
a=true;
}
if(c && a) {
float []r = new float[9],orien=new float[3];
SensorManager.getRotationMatrix(r, null, acc, comp);
SensorManager.getOrientation(r, orien);
float grad = (float)((Math.toDegrees(orien[0])+360)%360);
MyLocation.getInstance().setCompas(grad);
}
}
示例7: onSensorChanged
import android.hardware.SensorManager; //导入方法依赖的package包/类
public void onSensorChanged(SensorEvent event) {
int type = event.sensor.getType();
float[] data;
if (type == Sensor.TYPE_ACCELEROMETER) {
data = mGData;
} else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
data = mMData;
} else {
// we should not be here.
return;
}
for (int i=0 ; i<3 ; i++)
data[i] = event.values[i];
SensorManager.getRotationMatrix(mR, mI, mGData, mMData);
// some test code which will be used/cleaned up before we ship this.
// SensorManager.remapCoordinateSystem(mR,
// SensorManager.AXIS_X, SensorManager.AXIS_Z, mR);
// SensorManager.remapCoordinateSystem(mR,
// SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, mR);
SensorManager.getOrientation(mR, mOrientation);
float incl = SensorManager.getInclination(mI);
if (mCount++ > 50) {
final float rad2deg = (float)(180.0f/Math.PI);
mCount = 0;
Log.d("Compass", "yaw: " + (int)(mOrientation[0]*rad2deg) +
" pitch: " + (int)(mOrientation[1]*rad2deg) +
" roll: " + (int)(mOrientation[2]*rad2deg) +
" incl: " + (int)(incl*rad2deg)
);
}
}
示例8: calculateOrientation
import android.hardware.SensorManager; //导入方法依赖的package包/类
private void calculateOrientation() {
// If phone doesn't have Rotation Vector sensor, calculate orientation based on Accelerometer and Magnetometer
if (SensorManager.getRotationMatrix(mAccMagMatrix, null, mAccel, mMagnet) && !hasRotationSensor) {
SensorManager.getOrientation(mAccMagMatrix, mOrientation);
} else {
SensorManager.getRotationMatrixFromVector(mRotationMatrixFromVector, mRotation);
SensorManager.getOrientation(mRotationMatrixFromVector, mOrientation);
}
// Calculate azimuth to detect direction
currentAzimuth = Math.toDegrees(mOrientation[0]);
// Only notify other receivers if there is a change in orientation greater than 2.0 degrees
if(Math.abs(currentAzimuth - preAzimuth) >= 2.0) {
announceChange(ANGLE_UPDATE);
preAzimuth = currentAzimuth;
}
}
示例9: calculateOrientation
import android.hardware.SensorManager; //导入方法依赖的package包/类
/**
* 计算方向
*
* @return
*/
private void calculateOrientation(float[] accValues, float[] magValues) {
float[] R = new float[9];
float[] values = new float[3];
SensorManager.getRotationMatrix(R, null, accValues, magValues);
SensorManager.getOrientation(R, values);
// 相反方向所以为负
newRotationDegree = (float) Math.toDegrees(values[0]);
}
示例10: onSensorChanged
import android.hardware.SensorManager; //导入方法依赖的package包/类
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED)
mGeomagnetic = event.values;
if ((mGravity == null) || (mGeomagnetic == null))
return;
float[] R = new float[9];
float[] I = new float[9];
if (!SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic))
return;
float[] orientation = new float[3];
SensorManager.getOrientation(R, orientation);
if (orientation == null)
return;
double rollAngle = orientation[1] * 180 / Math.PI;
double pitchAngle = orientation[2] * 180 / Math.PI;
if(notWithinAngleTolerance((int)rollAngle, mLastRoll))
this.mRollTextView.setText(String.format("%.0f", (rollAngle)));
if(notWithinAngleTolerance((int)pitchAngle,mLastPitch))
this.mPitchTextView.setText(String.format("%.0f", (pitchAngle)));
mLastPitch = (int)pitchAngle;
mLastRoll = (int) rollAngle;
calculateAndSetSpeed(rollAngle,pitchAngle);
}
示例11: getSensorsData
import android.hardware.SensorManager; //导入方法依赖的package包/类
private void getSensorsData(){
SensorManager.getRotationMatrix(sensorInitializer.getRotationMatrix(), null,
sensorInitializer.getAccelerationValues(), sensorInitializer.getMagneticValues());
SensorManager.getOrientation(sensorInitializer.getRotationMatrix(), sensorInitializer.getOrientationValues());
xSensorAxis = sensorInitializer.getOrientationValues()[1] * 57.3f * (-1);
ySensorAxis = sensorInitializer.getOrientationValues()[2] * 57.3f + 90.0f;
}
示例12: onSensorChanged
import android.hardware.SensorManager; //导入方法依赖的package包/类
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
mGravity = event.values;
}
if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
{
mGeomagnetic = event.values;
}
if(mGravity != null && mGeomagnetic != null)
{
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R,I,mGravity,mGeomagnetic);
if(success)
{
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
Log.e("NEW ORIENTATION---","---------------");
Log.e("orientation azimut", String.valueOf(orientation[0]*60));
Log.e("orientation pitch", String.valueOf(orientation[1]*60));
Log.e("orientation roll", String.valueOf(orientation[2]*60));
txt.setText("orientation azimut " + String.valueOf(orientation[0]*60) + "\n" + "orientation pitch" + String.valueOf(orientation[1]*60) + "\n" + "orientation roll"+ String.valueOf(orientation[2]*60));
}
}
}
示例13: updateOrientationAngles
import android.hardware.SensorManager; //导入方法依赖的package包/类
public void updateOrientationAngles() {
// Update rotation matrix, which is needed to update orientation angles.
SensorManager.getRotationMatrix(mRotationMatrix, null,
mAccelerometerReading, mMagnetometerReading);
// "mRotationMatrix" now has up-to-date information.
SensorManager.getOrientation(mRotationMatrix, mOrientationAngles);
// "mOrientationAngles" now has up-to-date information.
}
示例14: onSensorChanged
import android.hardware.SensorManager; //导入方法依赖的package包/类
@Override
public void onSensorChanged(SensorEvent event) {
// get the angle around the z-axis rotated
if (event.sensor == mMagneticSensor) {
System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
mLastMagnetometerSet = true;
} else if (event.sensor == mAccelerometer) {
System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
mLastAccelerometerSet = true;
}
if (mLastAccelerometerSet && mLastMagnetometerSet) {
SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
SensorManager.getOrientation(mR, mOrientation);
float azimuthInRadians = mOrientation[0];
float azimuthInDegress = (float) (Math.toDegrees(azimuthInRadians) + 360) % 360;
mTargetDirection = azimuthInDegress;
}
float azimuth = -mTargetDirection;
GeomagneticField geoField = new GeomagneticField( Double
.valueOf( mLastLocation.getLatitude() ).floatValue(), Double
.valueOf( mLastLocation.getLongitude() ).floatValue(),
Double.valueOf( mLastLocation.getAltitude() ).floatValue(),
System.currentTimeMillis() );
if(geoField.getDeclination()<0) {
azimuth += geoField.getDeclination(); // converts magnetic north into true north
}else{
azimuth -= geoField.getDeclination();
}
//Correct the azimuth
azimuth = (azimuth+360) % 360;
//get the bearing
float y = (float)Math.sin(Math.toRadians(target.getLongitude()-mLastLocation.getLongitude())) * (float)Math.cos(Math.toRadians(target.getLatitude()));
float x = (float)Math.cos(Math.toRadians(mLastLocation.getLatitude()))*(float)Math.sin(Math.toRadians(target.getLatitude())) -
(float)Math.sin(Math.toRadians(mLastLocation.getLatitude()))*(float)Math.cos(Math.toRadians(target.getLatitude()))*(float)Math.cos(Math.toRadians(target.getLongitude()-mLastLocation.getLongitude()));
float bearing = (float)Math.toDegrees(Math.atan2(y, x));
image.setRotation(azimuth+bearing);
}
示例15: onSensorChanged
import android.hardware.SensorManager; //导入方法依赖的package包/类
/**
* Responds to changes in the accelerometer or magnetic field sensors to
* recompute orientation. This only updates azimuth, pitch, and roll and
* raises the OrientationChanged event if both sensors have reported in
* at least once.
*
* @param sensorEvent an event from the accelerometer or magnetic field sensor
*/
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (enabled) {
int eventType = sensorEvent.sensor.getType();
// Save the new sensor information about acceleration or the magnetic field.
switch (eventType) {
case Sensor.TYPE_ACCELEROMETER:
// Update acceleration array.
System.arraycopy(sensorEvent.values, 0, accels, 0, DIMENSIONS);
accelsFilled = true;
// Only update the accuracy property for the accelerometer.
accuracy = sensorEvent.accuracy;
break;
case Sensor.TYPE_MAGNETIC_FIELD:
// Update magnetic field array.
System.arraycopy(sensorEvent.values, 0, mags, 0, DIMENSIONS);
magsFilled = true;
break;
default:
Log.e(LOG_TAG, "Unexpected sensor type: " + eventType);
return;
}
// If we have both acceleration and magnetic information, recompute values.
if (accelsFilled && magsFilled) {
SensorManager.getRotationMatrix(rotationMatrix, // output
inclinationMatrix, // output
accels,
mags);
SensorManager.getOrientation(rotationMatrix, values);
// Make sure values are in expected range.
azimuth = OrientationSensorUtil.normalizeAzimuth(
(float) Math.toDegrees(values[AZIMUTH]));
pitch = OrientationSensorUtil.normalizePitch(
(float) Math.toDegrees(values[PITCH]));
// Sign change for roll is for compatibility with earlier versions
// of App Inventor that got orientation sensor information differently.
roll = OrientationSensorUtil.normalizeRoll(
(float) -Math.toDegrees(values[ROLL]));
// Adjust pitch and roll for phone rotation (e.g., landscape)
int rotation = getScreenRotation();
switch(rotation) {
case Surface.ROTATION_0: // normal rotation
break;
case Surface.ROTATION_90: // phone is turned 90 degrees counter-clockwise
float temp = -pitch;
pitch = -roll;
roll = temp;
break;
case Surface.ROTATION_180: // phone is rotated 180 degrees
roll = -roll;
break;
case Surface.ROTATION_270: // phone is turned 90 degrees clockwise
temp = pitch;
pitch = roll;
roll = temp;
break;
default:
Log.e(LOG_TAG, "Illegal value for getScreenRotation(): " +
rotation);
break;
}
// Raise event.
OrientationChanged(azimuth, pitch, roll);
}
}
}