传感器的简单介绍

MainActivity

package bw.com.bw_day09;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import bw.com.bw_day09.demo01.SensorActivity01;
import bw.com.bw_day09.demo02.SensorActivity02;
import bw.com.bw_day09.demo03.SensorActivity03;
import bw.com.bw_day09.demo04.SensorActivity04;

public class MainActivity extends AppCompatActivity {

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

    public void onClick(View view) {

        Intent intent  = new Intent();

        switch (view.getId())
        {
            case R.id.but_01:
                intent.setClass(this, SensorActivity01.class);
                break;
            case R.id.but_02:
                intent.setClass(this, SensorActivity02.class);
                break;
            case R.id.but_03:
                intent.setClass(this, SensorActivity03.class);
                break;
            case R.id.but_04:
                intent.setClass(this, SensorActivity04.class);
                break;
        }
        startActivity(intent);
    }
}

MainActivity布局

<?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"
    tools:context="bw.com.bw_day09.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="重力加速度传感器的介绍"
        android:id="@+id/but_01"
        android:onClick="onClick"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="传感器的类型"
        android:id="@+id/but_02"
        android:onClick="onClick"/>


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="摇一摇切换图片"
        android:id="@+id/but_03"
        android:onClick="onClick"/>


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="微信  摇一摇"
        android:id="@+id/but_04"
        android:onClick="onClick"/>

</LinearLayout>

demo01

   重力加速度传感器的介绍

package bw.com.bw_day09.demo01;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import bw.com.bw_day09.R;

public class SensorActivity01 extends AppCompatActivity implements SensorEventListener{

    private TextView mTv;
    private  SensorManager sensorManager;
    private Sensor sensor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor01);

        mTv = (TextView) findViewById(R.id.tv_id);

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

        //TODO 2, 得到传感器对象
        sensor = sensorManager.getDefaultSensor(1);//TODO TYPE_ACCELEROMETER = 1  重力加速度传感器

    }

    //TODO 3, 注册传感器  -- onResume()
    @Override
    protected void onResume() {
        super.onResume();
        //todo  传感器的监听器对象  -- 让当前类实现 SensorEventListener接口, 传感器, 刷新频率
        sensorManager.registerListener(this,sensor,SensorManager.SENSOR_DELAY_NORMAL);
    }


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


    //TODO --------传感器的监听器回调方法------------
    @Override
    public void onSensorChanged(SensorEvent event) {
        //传感器值发生变化的回调方法
        // TODO 5, 获取数据
        //获取传感器的值
        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) {
        //精度变化的回调方法
        }
}
   布局

<?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"
    tools:context="bw.com.bw_day09.demo01.SensorActivity01">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_id"
        android:textSize="30sp"
        android:textColor="@color/colorAccent"
        android:text="显示传感器的数据"
        />

</LinearLayout>
  Demo02

   传感器的类型

package bw.com.bw_day09.demo02;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

import bw.com.bw_day09.R;

public class SensorActivity02 extends AppCompatActivity  implements SensorEventListener{

    private EditText mEt_01,mEt_02,mEt_03;

    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, 注册传感器
    @Override
    protected void onResume() {
        super.onResume();
        //监听器对象  -- 让当前的类实现接口 SensorEventListener, 传感器对象, 刷新频率
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(1),SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(6),SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(7),SensorManager.SENSOR_DELAY_NORMAL);
    }


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

    //初始化控件
    private void initView() {

        mEt_01 = (EditText) findViewById(R.id.et_id_01);
        mEt_02 = (EditText) findViewById(R.id.et_id_02);
        mEt_03 = (EditText) findViewById(R.id.et_id_03);
    }

    //TODO  ------监听器的回调方法 -----
    @Override
    public void onSensorChanged(SensorEvent event) {
        //TODO 传感器数据发生变化的回调方法
        //TODO 4, 获取数据
        float[] data = event.values;

        //得到当前检测到的传感器的类型
        int type = event.sensor.getType();

        switch (type)
        {
            case 1:
                mEt_01.setText("加速度传感器 " + "\n X 轴 : " + data[0]+ "\n Y 轴 : " + data[1]+ "\n Z 轴 : " + data[2]);
                break;
            case 6:
                mEt_01.setText("压力传感器 " + "\n 压力值 : " + data[0]);
                break;
            case 7:
                mEt_01.setText("温度传感器 " + "\n 温度 : " + data[0]);
                break;
        }

    }

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


布局

  

<?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:background="#FFFFFF"
    android:padding="20dp"
    android:orientation="vertical">

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


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_id_02"
        android:text="压力传感器"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et_id_03"
        android:text="温度传感器"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"
        />


</LinearLayout>

Demo03

摇一摇切换图片

package bw.com.bw_day09.demo03;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
import bw.com.bw_day09.R;

/**
 * 利用加速度传感器  切换图片
 * 添加震动的效果
 */
public class SensorActivity03 extends AppCompatActivity  implements SensorEventListener{

    private LinearLayout mLayout;

    private SensorManager sensorManager;

    //需要切换的图片
    private int[] imgs = {R.mipmap.image1,R.mipmap.image2,R.mipmap.image3};
    private int index = 0;

    //TODO 6, 添加震动的效果  -- 震动的管理器对象
    private Vibrator vibrator;


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

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

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

        //TODO 6, 添加震动的效果  -- 震动的管理器对象
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    }

    //TODO 2, 注册传感器
    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(1),SensorManager.SENSOR_DELAY_NORMAL);
    }

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


    //TODO ------监听器的回调方法------
    @Override
    public void onSensorChanged(SensorEvent event) {
        //TODO 4, 获取数据
        float[] data = event.values;
        float x = data[0];
        float y = data[1];
        float z = data[2];

        //如果数值 接近 9, 则表示趋于禁止的状态 ,   > 9  那么表示晃动手机
        // 19  -19
        if(Math.abs(x) > 13 || Math.abs(y)>13 || Math.abs(z)>13)
        {
            //TODO 7, 震动 -- 添加震动权限
            //毫秒  300: 摇晃多长时间开始震动   500: 震动的持续时间
            long[] pattern = {300,500};
            vibrator.vibrate(pattern,-1);//第二个参数: 重复次数  (-1 不重复)

            //TODO 5, 切换图片
            if(index>2) index=0;//防止下标越界
            mLayout.setBackgroundResource(imgs[index]);
            index++;
        }
    }

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

布局

<?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"
    tools:context="bw.com.bw_day09.demo03.SensorActivity03"
    android:orientation="vertical"
    android:id="@+id/layout_id"
    android:background="@color/colorAccent">

</LinearLayout>

Demo04

微信  摇一摇

package bw.com.bw_day09.demo04;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import bw.com.bw_day09.R;

public class SensorActivity04 extends AppCompatActivity implements SensorEventListener{


    private ImageView mShakeUp;
    private ImageView mShakeDown;

    //定义动画
    private TranslateAnimation animationUp;
    private TranslateAnimation animationDown;

    private SensorManager sensorManager;

    //todo 添加震动 -- 添加权限
    private Vibrator vibrator;

    //TODO 添加声音
    private SoundPool soundPool;//声音池
    private int mLoadId;//当前播放的声音

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor04);
        initView();
        initAnimation();//初始化动画

        //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);
    }

    //TODO 2, 注册传感器
    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this,sensorManager.getDefaultSensor(1),SensorManager.SENSOR_DELAY_NORMAL);
    }
    //TODO 3, 取消注册传感器
    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    private void initView() {
        mShakeUp = (ImageView) findViewById(R.id.shake_up_id);
        mShakeDown = (ImageView) findViewById(R.id.shake_down_id);
    }

    private void initAnimation()
    {
        //补间动画 ---  位移
        animationUp = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,
                                            Animation.RELATIVE_TO_SELF,0,
                                            Animation.RELATIVE_TO_SELF,0,
                                            Animation.RELATIVE_TO_SELF,-1);
        animationUp.setDuration(3000);

        animationDown = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,
                                            Animation.RELATIVE_TO_SELF,0,
                                            Animation.RELATIVE_TO_SELF,0,
                                            Animation.RELATIVE_TO_SELF,1);

        animationDown.setDuration(3000);
    }

    //----监听器的回调方法---
    @Override
    public void onSensorChanged(SensorEvent event) {
        //TODO 4, 获取数据, 晃动手机, 播放动画
        float[] data = event.values;

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

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

            //TODO 5, 启动动画
            mShakeUp.startAnimation(animationUp);
            mShakeDown.startAnimation(animationDown);
        }

    }

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

    }
}

布局

<?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"
    tools:context="bw.com.bw_day09.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>


权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="bw.com.bw_day09">

    <!-- 添加震动权限 -->
    <uses-permission android:name="android.permission.VIBRATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".demo01.SensorActivity01" />
        <activity android:name=".demo02.SensorActivity02" />
        <activity android:name=".demo03.SensorActivity03" />
        <activity android:name=".demo04.SensorActivity04"></activity>
    </application>

</manifest>
其中raw文件夹里面装的是音频文件,一般音频文件都放这里面。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值