Android传感器——方向传感器TYPE_ORIENTATION

Sensor.TYPE_ORIENTATION:
All values are angles in degrees.
  • values[0]: Azimuth, angle between the magnetic north direction and the y-axis, around the z-axis (0 to 359). 0=North, 90=East, 180=South, 270=West

    values[1]: Pitch, rotation around x-axis (-180 to 180), with positive values when the z-axis moves toward the y-axis.

    values[2]: Roll, rotation around y-axis (-90 to 90), with positive values when the x-axis moves toward the z-axis.

Note: This definition is different from yaw, pitch and roll used in aviation where the X axis is along the long side of the plane (tail to nose).

Note: This sensor type exists for legacy reasons, please use getRotationMatrix() in conjunction with remapCoordinateSystem() and getOrientation() to compute these values instead.

Important note: For historical reasons the roll angle is positive in the clockwise direction (mathematically speaking, it should be positive in the counter-clockwise direction).

由图可以知道values[0]是绕Yaw轴旋转得到的值,values[1]是绕Pitch轴旋转得到的值,values[2]是绕Roll轴旋转得到的值。

例程如下:

OrientationSensorDemoActivity.java代码:

package com.lau.orientation;

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.widget.TextView;

public class OrientationSensorDemoActivity extends Activity implements SensorEventListener {
	
	private SensorManager sensorManager = null;
	private boolean mRegister = false;
	private Sensor sensor = null;
	
	private TextView tvAzimuth = null;
	private TextView tvPitch = null;
	private TextView tvRoll = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        tvAzimuth = (TextView) findViewById(R.id.azimuth);
        tvPitch = (TextView) findViewById(R.id.pitch);
        tvRoll = (TextView) findViewById(R.id.roll);
        
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        
        
    }
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if(mRegister) {
			sensorManager.unregisterListener(this);
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mRegister = sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		tvAzimuth.setText("Azimuth 方位角: " + event.values[0] + "\n(0 - 359) 0=North, 90=East, 180=South, 270=West");
		tvPitch.setText("Pitch 倾斜角: " + event.values[1] + "\n(-180 to 180)");
		tvRoll.setText("Roll 旋转角: " + event.values[2] + "\n(-90 to 90)");
	}
    
}


 

 

布局文件main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/azimuth"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/pitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    
    <TextView
        android:id="@+id/roll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>


 




一、实例教程:Android设备功能之传感器教程篇:

1、Android 的传感器
http://www.eoeandroid.com/thread-101949-1-1.html 

2、Android 光传感器
http://www.eoeandroid.com/thread-75288-1-1.html

3、Android 磁场传感器
http://www.eoeandroid.com/thread-75282-1-1.html

4、Android 姿态传感器
http://www.eoeandroid.com/thread-75277-1-1.html

5、Android 温度传感器
http://www.eoeandroid.com/thread-75285-1-1.html

6、关于传感器的使用
http://www.eoeandroid.com/thread-62297-1-1.html

7、深入探讨 Android 传感器
http://www.eoeandroid.com/thread-21672-1-1.html

8、2010年08月28日Android底层、框架与上层应用培训- 资料共享(大荐)
http://www.eoeandroid.com/thread-33418-1-1.html

9、Android 小球重力感应实现(一)
http://www.eoeandroid.com/thread-98306-1-1.html

10、Android 梳理一下 传感器 的 数据流 和 框架
http://www.eoeandroid.com/thread-101496-1-1.html

11、Android 传感器的 数据流和框架 是怎么样让 屏幕旋转
http://www.eoeandroid.com/thread-101192-1-1.html

12、Android 游戏中的传感器
http://www.eoeandroid.com/thread-79700-1-1.html

13、【Android2D游戏开发十八】解放 手指,利用传感器开发游戏!
http://www.eoeandroid.com/thread-55574-1-1.html


二、学习笔记:Android设备功能之传感器的实践过程篇: 

1、资源三、android开发资料系统学习,大量游戏3d传感器源码
http://www.eoeandroid.com/thread-153019-1-1.html

2、Android传感器API之:加速度Accelerometer功能源码
http://www.eoeandroid.com/thread-146211-1-1.html

3、Android传感器API之:温度传感器SensorTemperature功能源码
http://www.eoeandroid.com/thread-147885-1-1.html

4、Android传感器API之:方向SensorOrientation传感器功能实现与源码分享
http://www.eoeandroid.com/thread-128607-1-1.html

5、Android传感器API之:磁场Magnetic Field源码与示例
http://www.eoeandroid.com/thread-120996-1-1.html

6、Android传感器API之:近距离感应Proximity功能源码
http://www.eoeandroid.com/thread-147719-1-1.html

7、opengl es 加传感器
http://www.eoeandroid.com/thread-66535-1-1.html

8、 Android 传感器和录制媒体样例的实例
http://www.eoeandroid.com/thread-24847-1-1.html

9、利用SensorSimulator进行传感器的模拟
http://www.eoeandroid.com/thread-114920-1-1.html

10、Android 开发中传感器使用实例
http://www.eoeandroid.com/thread-70257-1-1.html

11、转载:Sensor传感器源码的阅读与应用开发简单实例
http://www.eoeandroid.com/thread-55708-1-1.html


三、EOE社区关于Android设备功能之传感器的实践问题篇(已解答):

1、关于传感器的疑问,求大大们解释一下
http://www.eoeandroid.com/thread-166935-1-1.html

2、Android传感器的简单例子无法工作
http://www.eoeandroid.com/thread-101622-1-1.html

3、2.3版本的加速度传感器
http://www.eoeandroid.com/thread-60604-1-1.html

4、传感器,请大家帮我看一下
http://www.eoeandroid.com/thread-96235-1-1.html

5、如何利用传感器等资源判断我现在是在卫生间还是卧室?(火)
http://www.eoeandroid.com/thread-73270-1-1.html

6、GPS等传感器
http://www.eoeandroid.com/thread-118306-1-1.html


四、EOE社区关于Android设备功能之传感器的实践问题篇(未解答):

1、屏幕熄灭后 传感器不工作的问题
http://www.eoeandroid.com/thread-162993-1-1.html

2、关于传感器
http://www.eoeandroid.com/thread-165763-1-1.html

3、求问一些关于传感器的问题。
http://www.eoeandroid.com/thread-163679-1-1.html

4、传感器编程求解啊
http://www.eoeandroid.com/thread-159213-1-1.html

5、提问+探讨:关于近距离传感器的那点破事
http://www.eoeandroid.com/thread-60798-1-1.html

6、传感器记录运动轨迹
http://www.eoeandroid.com/thread-165059-1-1.html

7、传感器数据
http://www.eoeandroid.com/thread-84189-1-1.html

8、如何通过传感器在一张图片上画出你的移动轨迹
http://www.eoeandroid.com/thread-158881-1-1.html

9、Eclipse无法创建传感器端口
http://www.eoeandroid.com/thread-151546-1-1.html

10、为什么手机不动传感器值也一直在变
http://www.eoeandroid.com/thread-158187-1-1.html

11、传感器获取间隔不稳定
http://www.eoeandroid.com/thread-103108-1-1.html

12、利用传感器求位移(荐)
http://www.eoeandroid.com/thread-12163-1-1.html

13、android 传感器加速表的值不停在变
http://www.eoeandroid.com/thread-10272-1-1.html

14、关于传感器的问题
http://www.eoeandroid.com/thread-10948-1-1.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值