yinter 发表于 2013-1-15 02:44:45

对android重力测试的一个疑问

之前有看过IBM Eyes 关于SensorManager的代码,但因为没有手机,没法测试,今天把代码发布到手机上了(android版本为1.5),终于可以知道他的坐标(X,Y,Z)的重力位置,代码如下:

/* ** IBMEyes.java * sample code for IBM Developerworks Article * Author: W. Frank Ableson * fableson@msiservices.com **/package com.msi.ibm.eyes;import android.app.Activity;import android.hardware.SensorListener;import android.hardware.SensorManager;import android.os.Bundle;import android.util.Log;import android.widget.TextView;public class IBMEyes extends Activity implements SensorListener {final String tag = "IBMEyes";SensorManager sm = null;TextView xViewA = null;TextView yViewA = null;TextView zViewA = null;TextView xViewO = null;TextView yViewO = null;TextView zViewO = null;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      sm = (SensorManager) getSystemService(SENSOR_SERVICE);      setContentView(R.layout.main);      xViewA = (TextView) findViewById(R.id.xbox);      yViewA = (TextView) findViewById(R.id.ybox);      zViewA = (TextView) findViewById(R.id.zbox);      xViewO = (TextView) findViewById(R.id.xboxo);      yViewO = (TextView) findViewById(R.id.yboxo);      zViewO = (TextView) findViewById(R.id.zboxo);            }                public void onSensorChanged(int sensor, float[] values) {      synchronized (this) {            Log.d(tag, "onSensorChanged: " + sensor + ", x: " + values +             ", y: " + values + ", z: " + values);            if (sensor == SensorManager.SENSOR_ORIENTATION) {            xViewO.setText("Orientation X: " + values);            yViewO.setText("Orientation Y: " + values);            zViewO.setText("Orientation Z: " + values);            }            if (sensor == SensorManager.SENSOR_ACCELEROMETER) {            xViewA.setText("Accel X: " + values);            yViewA.setText("Accel Y: " + values);            zViewA.setText("Accel Z: " + values);            }                  }    }      public void onAccuracyChanged(int sensor, int accuracy) {    Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);            }   @Override    protected void onResume() {      super.onResume();      sm.registerListener(this,               SensorManager.SENSOR_ORIENTATION |      SensorManager.SENSOR_ACCELEROMETER,                SensorManager.SENSOR_DELAY_NORMAL);    }      @Override    protected void onStop() {      sm.unregisterListener(this);      super.onStop();    }    }

我把手机平放在桌面,屏幕朝上,(x,y,z)的值分别为(0,0,-10);
手机翻过来放(与平放相反,即屏幕朝下),(x,y,z)的值分别为(0,0,10);
手机竖着放,(x,y,z)的值分别为(0,-10,0);
手机倒过来放(与竖相反),(x,y,z)的值分别为(0,10,0);

说明一下,实际输出为float值,为了好确认方向,我取整了;

对这个值有个什么疑问呢,与raymondlueng在博客写的正好相反,这就奇怪了。
页: [1]
查看完整版本: 对android重力测试的一个疑问