Android_方向传感器
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_方向传感器的更多相关文章
- 简单的方向传感器SimpleOrientationSensor
SimpleOrientationSensor是一个简单的方向传感器.能够识别手机如下表的6种方向信息: SimpleOrientation枚举变量 方向 NotRotated 设备未旋转 Rotat ...
- 修复 XE8 for Android 方向传感器 headingX,Y,Z 不会动的问题
问题:XE8 for Android 方向传感器无法正常运作(在 XE7 是正常的) 测试:官方示例 Samples\Object Pascal\Mobile Snippets\Orientation ...
- UWP开发中的方向传感器
在UWP开发中,我们能使用的到方向有三种: OrientationSensor下的四元数:Compass罗盘的HeadingMagneticNorth:以及SimpleOrientationSenso ...
- Android实例-OrientationSensor方向传感器(XE8+小米2)
相关资料: <修复 XE8 for Android 方向传感器 headingX,Y,Z 不会动的问题>:http://www.cnblogs.com/onechen/p/4497282. ...
- Android 百度地图 SDK v3.0.0 (二) 定位与结合方向传感器
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37730469 在上一篇博客中,我们成功把地图导入了我们的项目.本篇我们准备为地图 ...
- Android 方向传感器
一般情况下,在Android系统中获取手机的方位信息在api中有TYPE_ORIENTATION常量,可以像得到加速度传感器那样得到方向传感器sm.getDefaultSensor(Sensor.TY ...
- Android利用方向传感器获得手机的相对角度实例说明
http://www.jb51.net/article/37710.htm 1.android 的坐标系是如何定义x, y z 轴的 x轴的方向是沿着屏幕的水平方向从左向右,如果手机不是正方形的话,较 ...
- 关于android方向传感器的使用
Android2.2以后 orientation sensors 就被deprecated了 官方建议用acceleration and magnetic sensor 来算 关于这个问题,CSDN上 ...
- Android百度地图(二)结合方向传感器我们自己定位哪里走
Android百度地图(二)结合方向传感器我们自己定位哪里走 本文代码在http://blog.csdn.net/xyzz609/article/details/51943556的基础上进一步改动.有 ...
随机推荐
- Django-cookie与session操作
添加cookie: def login(req): if req.method=="POST": uf = UserInfoForm(req.POST) if uf.is_vali ...
- 腾讯云,搭建LAMP服务
lamp (Web应用软件) 编辑 Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被 ...
- PAT 1098. Insertion or Heap Sort
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 《hello-world》第八次团队作业:Alpha冲刺-Scrum Meeting 5
项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 <hello--worl ...
- mysql-索引、导入、导出、备份、恢复
1.索引 索引是一种与表有关的结构,它的作用相当于书的目录,可以根据目录中的页码快速找到所需的内容. 当表中有大量记录时,若要对表进行查询,没有索引的情况是全表搜索:将所有记录一一取出,和查询条件进行 ...
- Curious Cupid
There are K different languages in the world. Each person speaks one and only one language. There ar ...
- paste deploy初探
这段时间刚着手开始研究Openstack Swift源码,为后续开发做准备. Swift依据python WSGI规范.WSGI(Web Server Gateway Interface)是Pytho ...
- Surround the Trees HDU 1392 凸包
Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...
- Swift和Objective-C混合编程——Swift调用OC
在iOS应用的开发中.Swift必将代替OC,两者的趋势是"短期共存,长期代替".但曾经有太多的代码是用OC语言完毕的,而Swift的开发也从 OC中继承了非常多的特性.两者也是有 ...
- UVA 12683 Odd and Even Zeroes(数学—找规律)
Time Limit: 1000 MS In mathematics, the factorial of a positive integer number n is written as n! an ...