本文介绍了什么是Android定向传感器的替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试在我的游戏中添加一个传感器,允许播放器倾斜Android作为其控件。



理想情况下,我想使用ORIENTATION传感器,但我注意到它已被弃用。有没有人知道如何在Android中检测到倾斜并且不使用此传感器?

解决方案

没有实际的方向传感器 - 实际上是一个复合传感器,由加速度计和压力计的组合产生。





传感器类型存在原因,请使用与来计算这些值。

  public class OrientationTestActivity extends Activity实现了SensorEventListener 
{
private SensorManager mSensorManager;
私人传感器mAccelerometer;
私人传感器mMagnetometer;

private float [] mLastAccelerometer = new float [3];
private float [] mLastMagnetometer = new float [3];
private boolean mLastAccelerometerSet = false;
private boolean mLastMagnetometerSet = false;

private float [] mR = new float [9];
private float [] mOrientation = new float [3];

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mSensorManager =(SensorManager)getSystemService(SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
}

protected void onResume(){
super.onResume();
mLastAccelerometerSet = false;
mLastMagnetometerSet = false;
mSensorManager.registerListener(this,mAccelerometer,SensorManager.SENSOR_DELAY_NORMAL);
mSensorManager.registerListener(this,mMagnetometer,SensorManager.SENSOR_DELAY_NORMAL);
}

protected void onPause(){
super.onPause();
mSensorManager.unregisterListener(this);
}

public void onAccuracyChanged(传感器传感器,int精度){
}

@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor == mAccelerometer){
System.arraycopy(event.values,0,mLastAccelerometer,0,event.values.length);
mLastAccelerometerSet = true;
} else if(event.sensor == mMagnetometer){
System.arraycopy(event.values,0,mLastMagnetometer,0,event.values.length);
mLastMagnetometerSet = true;
}
if(mLastAccelerometerSet&& mLastMagnetometerSet){
SensorManager.getRotationMatrix(mR,null,mLastAccelerometer,mLastMagnetometer);
SensorManager.getOrientation(mR,mOrientation);
Log.i(OrientationTestActivity,String.format(Orientation:%f,%f,%f,
mOrientation [0],mOrientation [1],mOrientation [2]));
}
}
}


Hi I am building a 3D game for Android.

I am currently trying to add a sensor to my game that allows the player to tilt the android as its controls.

Ideally I would like to use the ORIENTATION sensor, but I noticed it has been deprecated. Does anyone know how to detect tilt in the android and doesn't use this sensor?

解决方案

There is no actual "orientation" sensor - this is (was) actually a composite sensor, generated from a combination of the accelerometer and the magnometer.

From http://developer.android.com/reference/android/hardware/SensorEvent.html

"This sensor type exists for legacy reasons, please use getRotationMatrix() in conjunction with remapCoordinateSystem() and getOrientation() to compute these values instead."

public class OrientationTestActivity extends Activity implements SensorEventListener
{
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private Sensor mMagnetometer;

    private float[] mLastAccelerometer = new float[3];
    private float[] mLastMagnetometer = new float[3];
    private boolean mLastAccelerometerSet = false;
    private boolean mLastMagnetometerSet = false;

    private float[] mR = new float[9];
    private float[] mOrientation = new float[3];

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    }

    protected void onResume() {
        super.onResume();
        mLastAccelerometerSet = false;
        mLastMagnetometerSet = false;
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        mSensorManager.registerListener(this, mMagnetometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor == mAccelerometer) {
            System.arraycopy(event.values, 0, mLastAccelerometer, 0, event.values.length);
            mLastAccelerometerSet = true;
        } else if (event.sensor == mMagnetometer) {
            System.arraycopy(event.values, 0, mLastMagnetometer, 0, event.values.length);
            mLastMagnetometerSet = true;
        }
        if (mLastAccelerometerSet && mLastMagnetometerSet) {
            SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
            SensorManager.getOrientation(mR, mOrientation);
            Log.i("OrientationTestActivity", String.format("Orientation: %f, %f, %f",
                                                           mOrientation[0], mOrientation[1], mOrientation[2]));
        }
    }
}

这篇关于什么是Android定向传感器的替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:28