android原始数据转欧拉角,Android获取欧拉角

不说话,贴代码:

activity_main.xml

xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:weightSum="1">

android:layout_width="315dp"

android:layout_height="wrap_content"

android:text="New Text"

android:id="@+id/textView"

android:layout_gravity="center_horizontal"

android:textSize="20dp"/>

android:layout_width="315dp"

android:layout_height="wrap_content"

android:text="New Text"

android:id="@+id/textView2"

android:layout_gravity="center_horizontal"

android:textSize="20dp"/>

android:layout_width="315dp"

android:layout_height="wrap_content"

android:text="New Text"

android:id="@+id/textView3"

android:layout_gravity="center_horizontal"

android:textSize="20dp"/>

MainActivity.java

package com.example.hailang.getorientation;

import android.app.Activity;

import android.content.Context;

import android.hardware.Sensor;

import android.hardware.SensorEvent;

import android.hardware.SensorEventListener;

import android.hardware.SensorManager;

import android.os.Bundle;

import android.util.Log;

import android.widget.TextView;

public class MainActivity extends Activity {

private SensorManager sm;

//需要两个Sensor

private Sensor aSensor;

private Sensor mSensor;

private TextView textView1, textView2, textView3;

float[] accelerometerValues = new float[3];

float[] magneticFieldValues = new float[3];

private static final String TAG = "sensor";

@Override

public void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textView1 = (TextView)findViewById(R.id.textView);

textView2 = (TextView)findViewById(R.id.textView2);

textView3 = (TextView)findViewById(R.id.textView3);

sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

aSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

mSensor = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

sm.registerListener(myListener, aSensor, SensorManager.SENSOR_DELAY_NORMAL);

sm.registerListener(myListener, mSensor,SensorManager.SENSOR_DELAY_NORMAL);

//更新显示数据的方法

calculateOrientation();

}

//再次强调:注意activity暂停的时候释放

public void onPause(){

sm.unregisterListener(myListener);

super.onPause();

}

final SensorEventListener myListener = new SensorEventListener() {

public void onSensorChanged(SensorEvent sensorEvent) {

if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)

magneticFieldValues = sensorEvent.values;

if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)

accelerometerValues = sensorEvent.values;

calculateOrientation();

}

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

};

private void calculateOrientation() {

float[] values = new float[3];

float[] R = new float[9];

SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticFieldValues);

SensorManager.getOrientation(R, values);

// 要经过一次数据格式的转换,转换为度

values[0] = (float) Math.toDegrees(values[0]);

textView1.setText(Float.toString(values[0]));

values[1] = (float) Math.toDegrees(values[1]);

textView2.setText(Float.toString(values[1]));

values[2] = (float) Math.toDegrees(values[2]);

textView3.setText(Float.toString(values[2]));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值