Android : SensorManager 传感器入门 简单应用

功能介绍:转动手机 图片跟着旋转

界面:

activity_main.xml

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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/baseline_img" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

java 复制代码
package com.example.mysensormanager;

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;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

/**
 * 1.获取SensorManager对象
 * 2.获取Sensor 对象
 * 3.注册Sensor 对象
 * 4.重写 onAccuracyChanged, onSensorChanged 方法
 * 5.注销Sensor对象
 */
public class MainActivity extends AppCompatActivity implements SensorEventListener {
    SensorManager mSensorManager;
    Sensor sensor;
    ImageView imageView;
    private float startDegree = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.imageView);

        //获取传感器服务
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        /**获取传感器对象
         * Sensor.TYPE_ACCELEROMETER:加速度传感器,用于检测手机在X、Y、Z三个方向上的加速度变化。
         * Sensor.TYPE_GRAVITY:重力传感器,用于检测手机受到的重力加速度变化。
         * Sensor.TYPE_GYROSCOPE:陀螺仪传感器,用于检测手机的角速度变化。
         * Sensor.TYPE_MAGNETIC_FIELD:磁力传感器,用于检测设备周围的磁场强度和方向。
         * Sensor.TYPE_LIGHT:光线传感器,用于检测手机周围的光线强度。
         * Sensor.TYPE_PRESSURE:气压传感器,用于检测设备周围的大气压强。
         * Sensor.TYPE_LINEAR_ACCELERATION:线性加速度传感器,用于检测手机在X、Y、Z三个方向上的线性加速度变化。
         * Sensor.TYPE_ORIENTATION:方向传感器,用于检测手机的朝向变化。
         * Sensor.TYPE_PROXIMITY:距离传感器,用于检测设备与目标物体之间的距离
         * Sensor.TYPE_ROTATION_VECTOR:旋转矢量传感器,用于检测手机的旋转变化。
         * Sensor.TYPE_TEMPERATURE:温度传感器,用于检测手机的温度变化。
         * Sensor.TYPE_FINGERPRINT:指纹传感器,用于检测设备的指纹信息。
         * Sensor.TYPE_HEART_RATE:心率传感器,用于检测用户的心率变化。
         * Sensor.TYPE_STEP_COUNTER:步数传感器,用于检测用户行走的步数。
         * */
        //方向传感器,用于检测手机的朝向变化。
        sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

    }

    @Override
    protected void onResume() {
        super.onResume();
        //打开应用:创建onCreate --> 开始onStart-->运行onResume
        //重新打开应用:重新启动onRestart--> 开始onStart-->运行onResume
        //返回当前页:重新启动onRestart--> 开始onStart-->运行onResume
        //注册Sensor
        /** 传感器延时:
         * 多久获取一次数据  最快
         SENSOR_DELAY_FASTEST = 0
         适合游戏
         SENSOR_DELAY_GAME = 1
         绘画
         SENSOR_DELAY_UI = 2
         普通界面
         SENSOR_DELAY_NORMAL =3
         */
        if (sensor != null) {
            mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
        } else {
            // 设备可能不支持ORIENTATION传感器,处理这种情况...
            Toast.makeText(this, "设备可能不支持ORIENTATION传感器", Toast.LENGTH_SHORT).show();
        }


    }

    @Override
    protected void onPause() {
        super.onPause();
        //退出应用:暂停onPause --> 停止onStop-->销毁onDestroy
        // home : 暂停onPause --> 停止onStop
        //跳转页面时:当前页:暂停onPause --> 停止onStop
        //注销Sensor对象
        if (sensor != null) {
            mSensorManager.unregisterListener(this);
        }

    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        //传感器发生改变
        //校验  if(event.sensor.getType() == Sensor.TYPE_ORIENTATION){}
        //values[0]: Acceleration minus Gx on the x-axis
        //values[1]: Acceleration minus Gy on the y-axis
        //values[2]: Acceleration minus Gz on the z-axis
        if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {

            float degree = (float) event.values[0]; // 获取当前旋转的角度(以弧度为单位)并转换为度数格式
            //设置动画
            /** RotateAnimation
             * fromDegrees:旋转的开始角度。
             * toDegrees:旋转的结束角度。
             * pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
             * pivotXValue:X坐标的伸缩值。
             * pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
             * pivotYValue:Y坐标的伸缩值。
             * */
            RotateAnimation rotateAnimation = new RotateAnimation(startDegree, degree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            //设置动画时间
            rotateAnimation.setDuration(300);
            imageView.startAnimation(rotateAnimation);
            startDegree = degree; // 更新上次旋转的角度为当前旋转的角度
        }
    }


    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // 在这里可以处理传感器精度的变化,但与ORIENTATION传感器无关,因此可以忽略。


    }
}

注意事项:

  1. 要注销Sensor对象

2.要在真机上测试,模拟器不支持

3.不要阻塞 onSensorChanged() 方法

4.避免使用过时的方法 或 传感器类型

5.在使用前先验证传感器

6.谨慎选择传感器延时

相关推荐
2401_8949155317 分钟前
Geo搜索优化排名源码部署搭建全流程详解
android·开发语言·flask·php·精选
Yang_jie_0332 分钟前
笔记:数据结构(栈是否使用底指针以及头指针的初始化值)
数据结构·笔记·算法
星释1 小时前
鸿蒙智能体开发实战:34.鸿蒙壁纸大师 - 提示词工程与优化
android·华为·harmonyos·鸿蒙
blanks20202 小时前
android 编译问题记录
android
一杯奶茶¥2 小时前
c语言程序设计大学期末考试考研知识点重点笔记题库复习资料C语言重点考点题库资料合集
c语言·笔记·考研·c语言程序设计·c语言笔记
依然范特东2 小时前
动手学深度学习笔记--数据操作、线性代数
人工智能·笔记·深度学习
bobuddy2 小时前
平台总线(platform bus)
android
plainGeekDev3 小时前
libs 目录 → Gradle 依赖管理
android·java·kotlin
FakeOccupational3 小时前
【电路笔记 信号】传输线方程:从RLGC推导传输线特性阻抗
笔记
帅次4 小时前
Android 高级工程师面试:Flutter Widget 体系 近1年高频追问 20 题
android·flutter·面试·element·widget·setstate·renderobject