安卓传感器SensorManager实现简单指南针

本文介绍如何使用Android的SensorManager创建一个简单的指南针应用。主要内容包括获取SensorManager实例、注册特定类型的传感器并监听其数据变化,以及如何根据这些数据更新UI显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:

 * 1.获取传感器管理对象SensorManager
 * 2.获取指定类型的传感器
 * 3.注册监听,通过实时监听即可获取传感器传回来的数据


效果:


代码:

package com.fe.statuslayout.statuspage;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;

import com.fe.statuslayout.R;

/**
 * Created by Administrator on 2017/3/14 0014.
 * 安卓传感器SensorManager实现简单指南针
 * 思路:
 * 1.获取传感器管理对象SensorManager
 * 2.获取指定类型的传感器
 * 3.注册监听,通过实时监听即可获取传感器传回来的数据
 */

public class SensorOrientation extends AppCompatActivity implements View.OnClickListener {
    private Button start;
    private ImageView south_background;
    private ImageView south_campass;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sensororientation);
        initView();
    }

    private void initView() {
        start = (Button) findViewById(R.id.start);
        south_background = (ImageView) findViewById(R.id.south_background);
        south_campass = (ImageView) findViewById(R.id.south_campass);

        start.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                openOrientationOld();
                break;
        }
    }


    private SensorManager sensorManagerOrientationOld;
    private SensorEventListener listenerOrientationOld;
    //记录当前的角度
    float currentDegree;

    /**
     * 方向
     */
    private void openOrientationOld() {
        //1.获取传感器管理对象SensorManager
        sensorManagerOrientationOld = (SensorManager) getSystemService(SENSOR_SERVICE);
        //2.获取指定类型的传感器
        Sensor sensorOrientation = sensorManagerOrientationOld.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        //3.注册监听,通过实时监听即可获取传感器传回来的数据
        listenerOrientationOld = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {
                //Z轴旋转的角度,0度代表为正北
                //z是指向地心的方角,其角度是表指南针的方向,其中180表示正南方
                float zValue = Math.abs(event.values[0]);
                float xValue = Math.abs(event.values[2]);
                float yValue = Math.abs(event.values[1]);
                Log.d("gsonUtils", "xValue=" + xValue + "yValue=" + yValue + "zValue=" + zValue);
                //开始旋转
                RotateAnimation animation = new RotateAnimation
                        (currentDegree, zValue, Animation.RELATIVE_TO_SELF, 0.5f, Animation.
                                RELATIVE_TO_SELF, 0.5f);
                //让动画停留在在结束的位置
                animation.setFillAfter(true);
                //记录上一次的角度
                currentDegree = zValue;
                south_campass.startAnimation(animation);
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        };
        //由于方向传感器的精确度要求通常都比较高,使用的是 SENSOR_DELAY_GAME
        sensorManagerOrientationOld.registerListener(listenerOrientationOld, sensorOrientation, SensorManager.SENSOR_DELAY_GAME);
    }

    //取消注册
    @Override
    protected void onPause() {
        super.onPause();
        sensorManagerOrientationOld.unregisterListener(listenerOrientationOld);
    }

    //取消注册
    @Override
    protected void onStop() {
        super.onStop();
        sensorManagerOrientationOld.unregisterListener(listenerOrientationOld);
    }
}

布局:

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

    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:text="指南针"/>
    <RelativeLayout android:layout_width="match_parent"
                    android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/south_background"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/south_background"/>
        <ImageView
            android:id="@+id/south_campass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/south_campass"/>
    </RelativeLayout>
</LinearLayout>

图片

south_background.png


south_campass.png


。。。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值