Android方向传感器小案例,主要代码如下:

package com.hb.direction;

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.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView; public class MainActivity extends Activity {
private SensorManager sm;
private ImageView iv_compress; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_compress=(ImageView) findViewById(R.id.iv_compass);
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//地磁场
Sensor magnetic = sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
//加速度
Sensor acceleromter = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(listener, magnetic, SensorManager.SENSOR_DELAY_GAME);
sm.registerListener(listener, acceleromter, SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onDestroy() {
super.onDestroy();
if( sm!=null){
sm.unregisterListener(listener);
}
}
private SensorEventListener listener=new SensorEventListener() {
float[] magneticValues=new float[3];
float[] acceleromterValues=new float[3];
private float lastRotateDegree;
@Override
public void onSensorChanged(SensorEvent event) {
//判断当前是加速度还是地磁场传感器
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
acceleromterValues=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, acceleromterValues, magneticValues);
SensorManager.getOrientation(R, values);
//toDegreess转化为度
// tv_direction.setText("values is"+Math.toDegrees(values[0]));
// Toast.makeText(MainActivity.this, "values is"+Math.toDegrees(values[0]), 0).show();
//将计算出的旋转角取反用于指南针背景图
float roteteDegree=-(float)Math.toDegrees(values[0]);
if((Math.abs(roteteDegree)-lastRotateDegree)>1){
RotateAnimation animation = new RotateAnimation(lastRotateDegree, roteteDegree,Animation.RELATIVE_TO_SELF,
0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setFillAfter(true);
iv_compress.startAnimation(animation);
lastRotateDegree=roteteDegree; }
} @Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}; }

布局文件xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" > <ImageView
android:id="@+id/iv_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/arrow3" /> <ImageView
android:id="@+id/iv_compass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/compass" /> </RelativeLayout>

源码下载地址:http://pan.baidu.com/s/1i4HIMSP

Android_方向传感器的更多相关文章

  1. 简单的方向传感器SimpleOrientationSensor

    SimpleOrientationSensor是一个简单的方向传感器.能够识别手机如下表的6种方向信息: SimpleOrientation枚举变量 方向 NotRotated 设备未旋转 Rotat ...

  2. 修复 XE8 for Android 方向传感器 headingX,Y,Z 不会动的问题

    问题:XE8 for Android 方向传感器无法正常运作(在 XE7 是正常的) 测试:官方示例 Samples\Object Pascal\Mobile Snippets\Orientation ...

  3. UWP开发中的方向传感器

    在UWP开发中,我们能使用的到方向有三种: OrientationSensor下的四元数:Compass罗盘的HeadingMagneticNorth:以及SimpleOrientationSenso ...

  4. Android实例-OrientationSensor方向传感器(XE8+小米2)

    相关资料: <修复 XE8 for Android 方向传感器 headingX,Y,Z 不会动的问题>:http://www.cnblogs.com/onechen/p/4497282. ...

  5. Android 百度地图 SDK v3.0.0 (二) 定位与结合方向传感器

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37730469 在上一篇博客中,我们成功把地图导入了我们的项目.本篇我们准备为地图 ...

  6. Android 方向传感器

    一般情况下,在Android系统中获取手机的方位信息在api中有TYPE_ORIENTATION常量,可以像得到加速度传感器那样得到方向传感器sm.getDefaultSensor(Sensor.TY ...

  7. Android利用方向传感器获得手机的相对角度实例说明

    http://www.jb51.net/article/37710.htm 1.android 的坐标系是如何定义x, y z 轴的 x轴的方向是沿着屏幕的水平方向从左向右,如果手机不是正方形的话,较 ...

  8. 关于android方向传感器的使用

    Android2.2以后 orientation sensors 就被deprecated了 官方建议用acceleration and magnetic sensor 来算 关于这个问题,CSDN上 ...

  9. Android百度地图(二)结合方向传感器我们自己定位哪里走

    Android百度地图(二)结合方向传感器我们自己定位哪里走 本文代码在http://blog.csdn.net/xyzz609/article/details/51943556的基础上进一步改动.有 ...

随机推荐

  1. UNIX C 总结

    --day01--王建立QQ:2529866769今天的内容:一.计算机的框架什么是操作系统?(汽车)加油系统 油门 用户跟加油子系统交互的窗口.(接口)方向系统 方向盘 用户跟方向系统的交互接口.导 ...

  2. 爬虫文件存储-2:MongoDB

    1.连接MongoDB 连接 MongoDB 我们需要使用 PyMongo 库里面的 MongoClient,一般来说传入 MongoDB 的 IP 及端口即可,第一个参数为地址 host,第二个参数 ...

  3. 使用vscode,新建.vue文件,tab自动生成vue代码模板

    第一步: 新建模板并保存 文件 --> 首选项 --> 用户代码片段 --> 输入vue,选择vue.json -->复制 第三步中的模板内容中内容保存 第二步: 添加配置,让 ...

  4. 2、ceph-deploy之配置使用RBD

    说明 部署完ceph集群(active+clean状态)之后, 我们来实践下ceph block device(即RBD或RADOS block device). 我们需要在一台新的client节点上 ...

  5. 混合了RBAC和ACL的权限系统(二) -- 基于RBAC的系统权限

    http://fightplane.iteye.com/blog/1278464 1. 概念说明 A 系统级权限:从角色的角度出发,不特定于任何实际的资源的权限.比如“用户是否可以修改标题”这个权限, ...

  6. MySql 内存表使用

    MySql 内存表使用 内存表使用哈希散列索引把数据保存在内存中,因此具有极快的速度,适合缓存中小型数据库,但是使用上受到一些限制,以下是蓝草使用的一些感受. 1.heap对所有用户的连接是可见的,这 ...

  7. poj 1273最大流dinic算法模板

    #include<stdio.h> #include<string.h> #define N 300 #define inf 0x7fffffff #include<qu ...

  8. 动态加入的HTML的自己主动渲染

    这两天在写一个用EasyUI的前台,遇到动态向Layout加入HTML内容时没有自己主动渲染的问题.查了一下网上的资料后得以解决.详细例如以下: $("#content").htm ...

  9. HDU 4363

    这题是记忆化搜索很容易想到,但状态却不好设 dp[i][j][u][d][l][r][k].对于矩形为i*j,它的四周的颜色分别为u,d,l,r,横竖切的状态为k的种数. 其中要注意一个问题是,停止不 ...

  10. python 003 os模块 example

    import os for tmpdir in ('/tmp',r'C:/Users/Administrator/PycharmProjects/'): if os.path.isdir(tmpdir ...