Android-傳感器-方向

使用方向傳感器,定位手機y軸方向(y軸與北方夾角0-360度)

y軸: 手機長邊方向
x軸:手機短邊方向
z軸:與手機平面垂直方向

本文源碼:https://github.com/lioilwin/StepOrient

一.使用

public class MainActivity extends AppCompatActivity implements OrientSensor.OrientCallBack{
    .........
       @Override
    public void Orient(float orient) {
        // 方向回調,手機長邊y軸與北方夾角0-360度
        orientText.setText("方向:" + (int) orient);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.activity_main);
        orientText = (TextView) findViewById(R.id.orient_text);
        
        // 開啟方向監聽
        orientSensor = new OrientSensor(this, this);
        if (!orientSensor.registerOrient()) {
            Toast.makeText(this, "方向傳感器不可用!", Toast.LENGTH_SHORT).show();
        }
    }
    .......
 }
 

二.方向傳感器類


/**
 * 方向傳感器
 */

public class OrientSensor implements SensorEventListener {
    private static final String TAG = "OrientSensor";
    private SensorManager sensorManager;
    private OrientCallBack orientCallBack;
    private Context context;
    float[] accelerometerValues = new float[3];
    float[] magneticValues = new float[3];

    public OrientSensor(Context context, OrientCallBack orientCallBack) {
        this.context = context;
        this.orientCallBack = orientCallBack;
    }

    public interface OrientCallBack {
        /**
         * 方向回調
         */
        void Orient(int orient);
    }

    /**
     * 注冊加速度傳感器和地磁場傳感器 
     * @return 是否支持方向功能
     */
    public Boolean registerOrient() {
        Boolean isAvailable = true;
        sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);

        // 注冊加速度傳感器
        if (sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_GAME)) {
            Log.i(TAG, "加速度傳感器可用!");
        } else {
            Log.i(TAG, "加速度傳感器不可用!");
            isAvailable = false;
        }

        // 注冊地磁場傳感器
        if (sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_GAME)) {
            Log.i(TAG, "地磁傳感器可用!");
        } else {
            Log.i(TAG, "地磁傳感器不可用!");
            isAvailable = false;
        }
        return isAvailable;
    }

    /**
     * 注銷方向監聽器
     */
    public void unregisterOrient() {
        sensorManager.unregisterListener(this);
    }



    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            accelerometerValues = event.values.clone();
        } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            magneticValues = event.values.clone();
        }

        float[] R = new float[9];
        float[] values = new float[3];
        SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticValues);
        SensorManager.getOrientation(R, values);
        int degree = (int) Math.toDegrees(values[0]);//旋轉角度
        if (degree < 0) {
            degree += 360;
        }
        orientCallBack.Orient(degree);
    }

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

簡書: http://www.lxweimin.com/p/066f667c99c3
CSDN博客: http://blog.csdn.net/qq_32115439/article/details/61620059
GitHub博客:http://lioil.win/2017/03/13/Android-Orient.html
Coding博客:http://c.lioil.win/2017/03/13/Android-Orient.htmlv

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容