各大传感器

在这里插入图片描述

添加震动权限

<uses-permission android:name="android.permission.VIBRATE" />

第一项xml

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tv_id_01"
    android:text="显示加速度传感器的数值"
    android:textColor="@color/colorAccent"
    />

第一项java

public class SensorActivity01 extends AppCompatActivity implements SensorEventListener{
    private SensorManager sensorManager ;
    private Sensor sensor;
    private TextView mTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor01);
        mTv = (TextView) findViewById(R.id.tv_id_01);

        //TODO 1, 获取传感器的管理器对象
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        //TODO 2, 通过传感器的管理器对象, 得到传感器对象
        sensor = sensorManager.getDefaultSensor(1);//Sensor.TYPE_ACCELEROMETER = 1;

    }
    //TODO 3, 注册传感器的监听器  -- onResume() 生命周期方法中  -- 让当前的类, 实现SensorEventListener 接口,重写方法
    @Override
    protected void onResume() {
        super.onResume();
        //TODO 监听传感器时间的监听器,  传感器对象 , 刷新的频率
        //SensorManager.SENSOR_DELAY_FASTEST   最快的, 延迟最小, 同时也是最消耗资源的 -- 不推荐
        //SensorManager.SENSOR_DELAY_GAME      适合游戏的频率, 一般有实时要求高的可以使用
        //SensorManager.SENSOR_DELAY_NORMAL    正常频率, 一般有实时要求不高的可以使用
        //SensorManager.SENSOR_DELAY_UI        适合普通的应用频率, 这种模式比较省电, 系统开销小, 但是延迟大 , 适用普通小程序使用
        sensorManager.registerListener((SensorEventListener) this,sensor,SensorManager.SENSOR_DELAY_GAME);
    }

    //TODO 4, 取消注册传感器的监听器  -- onPause() 生命周期方法中
    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    //TODO ---  传感器的监听方法 ---
    @Override
    public void onSensorChanged(SensorEvent event) {
        //TODO 监听传感器的变化
        //TODO 5, 获取传感器中的数值
        //event.sensor.getType()   当前检测的传感器的类型
        float[] data = event.values;
        float x = data[0];
        float y = data[1];
        float z = data[2];

        mTv.setText("X 轴的数值: " +x +" \n Y 轴的数值: " +y +"\n  Z 轴的数值: " +z );
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        //TODO 精度发生变化的回调方法
    }
}

第二项xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="20dp"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_id_01"
            android:text="加速度传感器"
            android:textColor="@color/colorAccent"
            />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_id_02"
            android:text="陀螺仪传感器"
            android:textColor="@color/colorPrimary"
            android:layout_marginTop="10dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_id_03"
            android:text="温度传感器"
            android:textColor="#FF0000"
            android:layout_marginTop="10dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_id_04"
            android:text="光传感器"
            android:textColor="#00FF00"
            android:layout_marginTop="10dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_id_05"
            android:text="压力传感器"
            android:textColor="#000000"
            android:layout_marginTop="10dp"
            />


    </LinearLayout>

</ScrollView>

第二项java

public class SensorActivity02 extends AppCompatActivity implements SensorEventListener {
    private TextView mTv_01;
    private TextView mTv_02;
    private TextView mTv_03;
    private TextView mTv_04;
    private TextView mTv_05;
    private SensorManager sensorManager;//传感器的管理器对象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor02);

        initView();//初始化控件
        //TODO 1, 得到传感器的管理器对象
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    }
    //TODO 2, 注册传感器  -- 让当前的类型实现接口SensorEventListener
    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(1),SensorManager.SENSOR_DELAY_GAME);
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(4),SensorManager.SENSOR_DELAY_GAME);
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(7),SensorManager.SENSOR_DELAY_GAME);
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(5),SensorManager.SENSOR_DELAY_GAME);
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(6),SensorManager.SENSOR_DELAY_GAME);
    }

    //TODO 3, 取消注册传感器
    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }
    //----监听器的方法----
    @Override
    public void onSensorChanged(SensorEvent event) {
        //TODO 4, 得到每一种传感器的数值
        float[] data = event.values;
        switch (event.sensor.getType())
        {
            case 1:
                //加速度
                mTv_01.setText("加速度传感器 \n" + "X 轴: " + data[0] +"\n"+"Y轴: "+ data[1]+"\n"+"Z轴: "+ data[2]);
                break;
            case 4:
                //陀螺仪
                mTv_02.setText("陀螺仪传感器 \n" + "X 轴旋转角速度: " + data[0] +"\n"+"Y轴旋转角速度: "+ data[1]+"\n"+"Z轴旋转角速度: "+ data[2]);
                break;
            case 7:
                //温度
                mTv_03.setText("温度传感器 \n" + "当前的温度 : " + data[0]);
                //float current = data[0];if(Math.abs(current)>10) {//温度低于10度, 做操作}
                break;
            case 5:
                //光
                mTv_04.setText("光传感器 \n" + "当前光的强度 : " + data[0]);
                break;
            case 6:
                //压力
                mTv_05.setText("压力传感器 \n" + "当压力值  : " + data[0]);
                break;
        }
    }

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

    }
    /*** 初始化控件*/
    private void initView() {
        mTv_01 = (TextView) findViewById(R.id.tv_id_01);
        mTv_02 = (TextView) findViewById(R.id.tv_id_02);
        mTv_03 = (TextView) findViewById(R.id.tv_id_03);
        mTv_04 = (TextView) findViewById(R.id.tv_id_04);
        mTv_05 = (TextView) findViewById(R.id.tv_id_05);
    }
}

第三项xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:background="@color/colorAccent"
    android:id="@+id/layout_id">

</LinearLayout>

第三项java

public class SensorActivity03 extends AppCompatActivity implements SensorEventListener {
    //TODO 1, 传感器管理器对象
    private SensorManager sensorManager;
    private LinearLayout mLayout;
    //需要切换的图片
    private int[] images = {R.mipmap.image1,R.mipmap.image2,R.mipmap.image3};
    private int index = 0;//当前显示图片的下标

    //TODO 添加震动
    private Vibrator vibrator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor03);

        mLayout = (LinearLayout) findViewById(R.id.layout_id);

        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        //TODO 得到震动管理器对象
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    }

    //TODO 2,注册传感器的监听器  -- 让当前的类, 实现接口SensorEventListener
    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(1),SensorManager.SENSOR_DELAY_GAME);
    }

    //TODO 3,取消注册传感器的监听器
    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    //TODO ---监听器的回调方法---
    @Override
    public void onSensorChanged(SensorEvent event) {
        // 获取加速度传感器的返回值
        float[] data = event.values;
        if(Math.abs(data[0])>13 || Math.abs(data[1])>13 || Math.abs(data[2])>13)
        {
            //添加震动
            //摇晃多长时间开始震动并且震动的持续时间 long[] , 重复的次数 (-1  不重复)
            long[] pattern = {300,500};//摇晃300毫秒开始震动,  震动时长为500毫秒
            vibrator.vibrate(pattern,-1);//添加震动权限

            // TODO X, Y , Z 三个轴 的数值 , 有一个值大于13 则切换图片  -- 为控件, 更改背景图片
            //防止下标越界
            if(index>2) index = 0;
            mLayout.setBackgroundResource(images[index]);
            index++;
        }
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}
}

第四项xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:padding="20dp"
    tools:context="com.example.fancx.day09_sensor_demo.demo04.SensorActivity04">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/shakehideimg_man2"
        android:layout_centerInParent="true"
        android:id="@+id/flower_id"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/shake_logo_up"
            android:id="@+id/shake_up_id"
            />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/shake_logo_down"
            android:id="@+id/shake_down_id"
            />
    </LinearLayout>
</RelativeLayout>

第四项java

public class SensorActivity04 extends AppCompatActivity implements SensorEventListener {
    private ImageView mShakeUp;
    private ImageView mShakeDown;

    private SensorManager sensorManager;//TODO 传感器管理器对象

    //动画
    private TranslateAnimation upAnimation;
    private TranslateAnimation downAnimation;

    //震动
    private Vibrator vibrator;

    //添加声音
    private SoundPool soundPool;//音效池
    private int mLoadId;//当前加载的声音


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor04);

        mShakeUp = (ImageView) findViewById(R.id.shake_up_id);
        mShakeDown = (ImageView) findViewById(R.id.shake_down_id);

        //TODO 1, 传感器管理器对象
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

        //TODO 得到震动的管理器对象
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

        //TODO 初始化声音
        //最大值, 类型, 质量 默认0
        soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC,0);
        mLoadId = soundPool.load(this,R.raw.awe,1);

        //初始化动画
        initAnimation();
    }

    private void initAnimation() {

        //上
        upAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,
                Animation.RELATIVE_TO_SELF,0,
                Animation.RELATIVE_TO_SELF,0,
                Animation.RELATIVE_TO_SELF,-1);//0.5 一半, 1 代表是自己
        upAnimation.setDuration(3000);
        // 下
        downAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,
                Animation.RELATIVE_TO_SELF,0,
                Animation.RELATIVE_TO_SELF,0,
                Animation.RELATIVE_TO_SELF,1);//0.5 一半, 1 代表是自己
        downAnimation.setDuration(3000);
    }


    //TODO 2, 注册监听器  -- 当前类, 实现接口SensorEventListener
    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(1),SensorManager.SENSOR_DELAY_GAME);
    }
    //TODO 3, 取消注册监听器
    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    //TODO 4, 重写接口中的方法, 获取每一个数值, 启动动画, 实现微信摇一摇
    @Override
    public void onSensorChanged(SensorEvent event) {

        float[] data = event.values;
        if(Math.abs(data[0])>13 || Math.abs(data[1])>13 || Math.abs(data[2])>13)
        {
            //添加震动
            long[] pattern = {300,500};
            vibrator.vibrate(pattern,-1);

            //播放音乐
            //声音id, 左声道(0.0f -- 1.0f) , 右声道, 优先级, 是否循环(0 不循环, 1 循环) , 播放比例( 正常播放 1)
            soundPool.play(mLoadId,1.0f,1.0f,1,0,1.0f);

            //启动动画
            mShakeUp.startAnimation(upAnimation);
            mShakeDown.startAnimation(downAnimation);

        }

    }

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

    }
}

猜你喜欢

转载自blog.csdn.net/qq_42828101/article/details/83656053