不说话,贴代码:

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]));

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐