使用加速度传感器完成摇一摇功能

来源:互联网 发布:孩子网络借贷怎么办 编辑:程序博客网 时间:2024/06/06 07:28

传感器字面上的意思就是传递感觉的仪器,哪些感觉呢?
视觉、听觉、味觉、触觉、嗅觉等等。
所以有人说,传感器的存在和发展,让物体有了触觉、味觉和嗅觉等感官,让物体慢慢变得活了起来。
当前Android设备中已经集成进数十个传感器,我们比较常见的有加速度传感器、陀螺仪、地磁传感器等。
虽然种类繁多,但在Framework中仅仅提供了几个类和接口就把传感器相关的功能搞定了。下面我们以加速度传感器为例,引领大家走入android传感器的世界。

传感器世界的坐标系

这里写图片描述

x轴:从左到右
y轴:从下到上
z轴:从内到外

这个坐标系与Android 2D API中的不同,传感器中的返回值都以此坐标系为准。

下面将使用加速度传感器完成摇一摇功能
结构图:
这里写图片描述

布局文件activity_main.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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.day9sensor.MainActivity" >    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    <TextView        android:id="@+id/tv1"        android:layout_width="wrap_content"        android:layout_below="@+id/tv"        android:layout_height="wrap_content"/></RelativeLayout>

MainActivity.java:

import java.util.List;import android.app.Activity;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.os.Bundle;import android.os.SystemClock;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {    TextView tv,tv1;    boolean flag = true;    private SensorManager manager;    MyAcc acc;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv = (TextView) findViewById(R.id.tv);        tv1 = (TextView) findViewById(R.id.tv1);        acc = new MyAcc();        manager = (SensorManager) getSystemService(SENSOR_SERVICE);        //步骤2:使用传感器的管理器  获取当前设备中的所有传感器信息        List<Sensor> sensorList = manager.getSensorList(Sensor.TYPE_ALL);        for (Sensor sensor : sensorList) {            tv.append("\r\n" + sensor.getName());        }        //使用加速度传感器完成摇一摇功能        //1、获取到加速度传感器        Sensor sensorAcc = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);        //2、使用加速度传感器  注册一个监听        //参数1:监听器        //参数2:使用的传感器对象        //参数3:传感器精度            //          SENSOR_DELAY_NORMAL, 传感器的默认延时        //          SENSOR_DELAY_UI,     传感器适用于 UI更新的延时        //          SENSOR_DELAY_GAME,   传感器适用于 游戏的延时        //          SENSOR_DELAY_FASTEST  传感器最快延时        manager.registerListener(acc, sensorAcc, SensorManager.SENSOR_DELAY_UI);    }    class MyAcc implements SensorEventListener {        //当传感器数据方法改变时调用该方法        @Override        public void onSensorChanged(SensorEvent event) {            // TODO Auto-generated method stub            float x = event.values[0];            float y = event.values[1];            float z = event.values[2];            tv1.setText(x + "\r\n" + y + "\r\n" + z + "\r\n");            if(Math.abs(x) > 17 || Math.abs(y) > 17 || Math.abs(z) > 17){                if(flag){                    Toast.makeText(MainActivity.this, "摇一摇", 0).show();                }                flag = false;                new Thread(new Runnable() {                    @Override                    public void run() {                        // TODO Auto-generated method stub                        SystemClock.sleep(10000);                        flag = true;                    }                }).start();            }        }        //当传感器精度方法改变时 调用该方法        @Override        public void onAccuracyChanged(Sensor sensor, int accuracy) {            // TODO Auto-generated method stub        }    }    @Override    protected void onPause() {        manager.unregisterListener(acc);        super.onPause();    }}

一个小小的程序,希望对大家有所帮助!(^…^)

原创粉丝点击