Android学习-指南针(方向传感器)
Android学习-指南针
效果图(背景图片网上随便找的):
1.编写布局文件activity_main.xml文件
主要放一个东南西北方向图片,一个指针图片用来指明方向
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/linearLayout"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#fff"><ImageViewandroid:id="@+id/imageView2"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="1.0"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.0"app:srcCompat="@drawable/south_background" /><ImageViewandroid:id="@+id/imageView4"android:layout_width="57dp"android:layout_height="77dp"android:layout_marginStart="177dp"android:layout_marginLeft="177dp"android:layout_marginTop="327dp"android:layout_marginEnd="177dp"android:layout_marginRight="177dp"android:layout_marginBottom="327dp"app:layout_constraintBottom_toBottomOf="@+id/imageView2"app:layout_constraintEnd_toEndOf="@+id/imageView2"app:layout_constraintStart_toStartOf="@+id/imageView2"app:layout_constraintTop_toTopOf="@+id/imageView2"app:srcCompat="@drawable/south_campass" /></androidx.constraintlayout.widget.ConstraintLayout>
2.编写主类MainActivity类
(1)获取传感器管理对象SensorManager
(2)获取指定类型的传感器
(3)注册监听,通过实时监听即可获取传感器传回来的数据
public class MainActivity extends AppCompatActivity implements SensorEventListener {// 定义显示指南针的图片ImageView compassImage;// 记录指南针图片转过的角度float currentDegree = 0f;// 定义Sensor管理器SensorManager mSensorManager;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获取界面中显示指南针的图片compassImage = (ImageView) findViewById(R.id.imageView4);// 获取传感器管理服务mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);}@Overrideprotected void onResume() {super.onResume();// 为系统的方向传感器注册监听器mSensorManager.registerListener(this,mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_GAME);}@Overrideprotected void onPause() {// 取消注册mSensorManager.unregisterListener(this);super.onPause();}@Overrideprotected void onStop() {// 取消注册mSensorManager.unregisterListener(this);super.onStop();}@Overridepublic void onSensorChanged(SensorEvent event) {// 获取触发event的传感器类型int sensorType = event.sensor.getType();if (sensorType == Sensor.TYPE_ORIENTATION) {// 获取绕Z轴转过的角度float degree = event.values[0];// 创建旋转动画(反向转过degree度)RotateAnimation ra = new RotateAnimation(currentDegree, -degree,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);// 设置动画的持续时间ra.setDuration(200);// 运行动画compassImage.startAnimation(ra);currentDegree = -degree;}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {}
}
链接:https://pan.baidu.com/s/1gLbpD8knJotNYv8eNghnbA
提取码:w6kw
Android学习-指南针(方向传感器)相关推荐
- Android学习之手机传感器的获取
首先声明:手机传感器的获取参考了网上一些其他人的CSDN博客,本文只是记录个人的学习状况 具体步骤: 框架: 1.创建Android工程(不使用默认的Activity,自己手动创建)名称:Sensor ...
- android系统重力感应传感器 方向控制与方向切换,android sensor orientation 方向传感器 重力感应...
首先,获取方向传感器(参照http://developer.android.com/reference/android/hardware/SensorManager.html): publicclas ...
- Android传感器之-方向传感器Orientation功能实现与源码
先明确一下空间坐标系的三个方向: x 方向就是手机的水平方向,右为正: y 方向就是手机的水平垂直方向,前为正: z 方向就是手机的空间垂直方向,天空的方向为正,地球的方向为负. 方向角的定义是手机y ...
- Android 传感器之方向传感器
一般情况下,在android系统中获取手机的方位信息在api中有TYPE_ORIENTATION常量,可以像得到加速度传感器那样得到方向传感器sm.getDefaultSensor(Sensor.TY ...
- Android实例-OrientationSensor方向传感器(XE8+小米2)
相关资料: <修复 XE8 for Android 方向传感器 headingX,Y,Z 不会动的问题>:http://www.cnblogs.com/onechen/p/4497282. ...
- Android学习之重力传感器使用
一.食用解读 注意了Values[0]传回来的数是-10到10,可以根据自己的实际需求进行缩放! 二.代码示例 例子,形如此类的.. public class control extends Main ...
- Android -- 传感器使用示例, 用方向传感器做指南针
1. 传感器类型:方向.加速度(重力).光线.磁场.距离(临近性).温度等. 方向传感器: Sensor.TYPE_ORIENTATION 加速度(重力)传感器: Sensor.TYPE_ACCE ...
- Android 方向传感器与磁力计和加速度传感器之间的关系
一般情况下,在android系统中获取手机的方位信息azimuth似乎是很简单的事情,在api中有TYPE_ORIENTATION常量,可以像得到加速度传感器那样得到方向传感器sm.getDefaul ...
- Android方向传感器学习之指南针示例
刚刚跟着视频学习了关于Android中传感器的操作示例,利用方向传感器做了一个很简单的指南针应用...平时工作项目中很少有用到传感器功能,所以很多都不知道,现在自学些,当作慢慢入门吧... 首先贴出软 ...
最新文章
- 重磅!Github 开放无数量限制的免费私有仓库!
- RabbitMQ-c在Linux上编译
- final、finally与finalize的区别
- leetcode 413. Arithmetic Slices | 413. 等差数列划分(Java)
- linux不能更改密码,Linux服务器无法更改密码的解决办法--passwd: User not known
- Mysql 中 delete 与 left join 的问题
- textarea内容有换行时存入数据库丢失问题的解决 (转载)
- (93)异步FIFO结构,为什么用格雷码? ,面试必问(十七)(第19天)
- C# 连蒙带骗不知所以然的搞定USB下位机读写
- python随机数据库_Python实现生成随机数据插入mysql数据库的方法
- 慎用某60软件清理垃圾,导致三星SSD T5不识别了,折腾了一下午,终于弄好了
- wpsarm64linux,64位Ubuntu 14.04 安装wps
- (一)市场调查大赛系列——市场调查研究选题
- 栈(Stack)——后进先出(LIFO)的数据结构(Data Structures)
- 140809暑期培训
- 计算机二级考试 信息学奥赛,如何快速区分五大学科竞赛等级
- 永久免费的专业二维码生成器和二维码扫描器
- 苹果6运行内存是多少_安卓手机和苹果手机的最大不同,运行大小不同决定速度的快慢!|安卓手机|苹果手机|智能手机|手机|内存|闪存...
- 《Hadoop》之踽踽独行(一)大数据概述
- 3DUnity汽车尾气模拟