TYPE_ROTATION_VECTOR不工作在某些设备上 [英] TYPE_ROTATION_VECTOR not working on certain devices

查看:240
本文介绍了TYPE_ROTATION_VECTOR不工作在某些设备上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用TYPE_ROTATION_VECTOR拿到手机的定位..程序工作在某些三星Galaxy S手机,但不是索尼Xperia新V A计划..所有手机都与API> = 9 Android版本..可能是什么问题?

i have made a program that uses TYPE_ROTATION_VECTOR to get orientation of the mobile phone.. the program works on certain samsung galaxy s phone but not on sony xperia neo v.. all phones have android version with api >= 9.. what could be the problem ?

package com.example.sensortest;

import android.annotation.TargetApi;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener{
TextView t1,t2,t3;
private SensorManager mSensorManager;
private Sensor mRotVectSensor;
private float[] orientationVals=new float[3];
private float[] mRotationMatrix=new float[16];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    t1=(TextView)findViewById(R.id.TextView1);
    t2=(TextView)findViewById(R.id.TextView2);
    t3=(TextView)findViewById(R.id.TextView3);
    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mRotVectSensor=mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
public void onSensorChanged(SensorEvent event)
{
    if(event.sensor.getType()==Sensor.TYPE_ROTATION_VECTOR)
    {
        SensorManager.getRotationMatrixFromVector(mRotationMatrix,event.values);
        SensorManager.remapCoordinateSystem(mRotationMatrix,SensorManager.AXIS_X, SensorManager.AXIS_Z, mRotationMatrix);
        SensorManager.getOrientation(mRotationMatrix, orientationVals);
        orientationVals[0]=(float)Math.toDegrees(orientationVals[0]);
        orientationVals[1]=(float)Math.toDegrees(orientationVals[1]);
        orientationVals[2]=(float)Math.toDegrees(orientationVals[2]);
        t1.setText(String.valueOf(orientationVals[0]));
        t2.setText(String.valueOf(orientationVals[1]));
        t3.setText(String.valueOf(orientationVals[2]));
    }
}
@Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {

  }
@Override
  protected void onResume() {
    super.onResume();
    // register this class as a listener for the orientation and
    // accelerometer sensors
    mSensorManager.registerListener(this,
        mRotVectSensor,
        10000);
  }

  @Override
  protected void onPause() {
    // unregister listener
    super.onPause();
    mSensorManager.unregisterListener(this);
  }

}

推荐答案

我可能不会解决你的问题,但是在SensorManager.remapCoordinateSystem确保你使用不同的矩阵(见<一href=\"http://file:///C:/Program%20Files/Android/sdk/docs/reference/android/hardware/SensorManager.html#remapCoordinateSystem(float[],%20int,%20int,%20float[])\"相对=nofollow>文档)。有这样说的:(参数)OUTR-变换旋转矩阵。 INR和OUTR不应该是相同的数组。

I may not solve your problem, but make sure you're using different matrices in SensorManager.remapCoordinateSystem (see Documentation). There it says: (Parameter) outR- the transformed rotation matrix. inR and outR should not be the same array.

这篇关于TYPE_ROTATION_VECTOR不工作在某些设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆