这里有新鲜出炉的精品教程,程序狗速度看过来!
Android 是一种基于 Linux 的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由 Google 公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用 "安卓" 或 "安致"。
这篇文章主要为大家详细介绍了 Android 利用传感器仿微信摇一摇功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
传感器
简单的介绍一下传感器: 就是设备用来感知周边环境变化的硬件。
Android 中的传感器包含在传感器框架中,属于 android.hardware.*(硬件部分)
传感器框架主要包含四个部分:
① SensorManager: 用来获取传感器的入口,它是一个系统的服务,还可以为传感器注册与取消注册监听 ② Sensor: 具体的传感器,包含了传感器的名字,类型,采样率 ③ SensorEvent: 传感器事件,包含了传感器采集回来的数据,传感器的精度 ④ SensorEventListener: 传感器的监听,主要监测传感器数据变化,精度变化…
Android 播放音频系统提供了两种方式
① MediaPlayer 播放常规的音频,视频,通常用在播放器上 ② SoundPool 声音池,通常用在小而频繁播放的音乐,需要同时播放多个音乐的
VIBRATE 所震动传感器需要添加权限
实现之前先来看看手机上的传感器有哪些?(此处可以略过…) onCreat(); 中设置 TextView
- sensor = (TextView) findViewById(R.id.sensor);
- mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
- List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
- StringBuilder builder = new StringBuilder();
- builder.append("传感器总数: "+sensorList.size()+"\n");
- for (int i = 0; i < sensorList.size(); i++) {
- Sensor sensor = sensorList.get(i);
- builder.append("传感器名称: "+sensor.getName()+", 传感器生产厂商: "+sensor.getVendor()+"\n");
- }
- sensor.setText(builder.toString());
惊奇的发现小米 5 上竟然有 41 个传感器, 可以哈… 厉害了 好了, 好了接下来言归正传—微信摇一摇
界面的话三张图片你的层叠, RelativeLayout 进行布局
在 onCreate 中封装的方法
- //摇一摇---->加速传感器
- //1、初始化控件
- initView();
- //2、初始化音乐SoundPool
- initSoundPool();
- //3、震动
- initVibrator();
上面盖的两张图片初始化
- up_logo = (ImageView) findViewById(R.id.up_logo);
- down_logo = (ImageView) findViewById(R.id.down_logo);
在初始化 SoundPool 的时候, 发现 new SoundPool 已经不推荐使用了, 新版的 (API>21) 使用 Builder 构建, 所以在这里使用版本进行判断了一下
- private void initSoundPool() {
- if(Build.VERSION.SDK_INT>=21){
- SoundPool.Builder builder = new SoundPool.Builder();
- //设置
- builder.setMaxStreams(1);
- AudioAttributes attributes = new AudioAttributes.Builder()
- .setLegacyStreamType(AudioManager.STREAM_MUSIC)
- .build();
- builder.setAudioAttributes(attributes);
- mSoundPool = builder.build();
- }else {
- //已经过时,老版本
- mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);//参数三保留参数
- }
- //添加音乐
- //参数三是音乐池中音乐播放的优先级
- mSoundPool_id = mSoundPool.load(this, R.raw.awe, 1);
- }
初始化振动器:(是一个系统的服务)
mVibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
摇一摇利用的是加速度传感器, 需要监听它的变化状态 SensorManager 的注册也是成对出现的
- @Override
- protected void onStart() {
- super.onStart();
- //4、设置传感器监听,加速传感器
- initSensor();
- }
- @Override
- protected void onStop() {
- super.onStop();
- //解除注册
- mSensorManager.unregisterListener(this);
- }
- private void initSensor() {
- Sensor accelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
- //通过SensorManager实现监听加速传感器
- mSensorManager.registerListener(this,accelerometerSensor,SensorManager.SENSOR_DELAY_UI);
- }
这里的监听需要手动去实现 (这里是比较不智能的…) implements SensorEventListener 会实现两个方法
- //数据发生变化
- @Override
- public void onSensorChanged(SensorEvent event) {
- }
- //精度发生变化,传感器的,该方法用不到
- @Override
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- }
下面是方法的具体实现:
- @Override public void onSensorChanged(SensorEvent event) { //数据发生变化
- Sensor sensor = event.sensor;
- int type = sensor.getType();
- switch (type) {
- case Sensor.TYPE_ACCELEROMETER:
- //加速传感器
- float[] values = event.values;
- //x,y,z 三个方向
- //9.8
- float x = values[0];
- float y = values[1];
- float z = values[2];
- if (Math.abs(x) > 25 || Math.abs(y) > 25 || Math.abs(z) > 25) {
- //触发摇一摇
- //音乐播放
- mSoundPool.play(mSoundPool_id, 1, 1, 0, 0, 1);
- //震动(-1代表只执行一次)
- mVibrator.vibrate(new long[] {
- 200,
- 300,
- 400,
- 200
- },
- -1);
- //动画执行
- initAnimation();
- }
- break;
- }
- }
动画没什么好解释的, 直接上代码了
- private void initAnimation() {
- //up_logo 向上移动,同时有上下震动
- AnimationSet set_up = new AnimationSet(true);
- TranslateAnimation up_up = new TranslateAnimation(
- TranslateAnimation.RELATIVE_TO_SELF,0,//x轴起点
- TranslateAnimation.RELATIVE_TO_SELF,0,//x轴终点
- TranslateAnimation.RELATIVE_TO_SELF,0,//y轴起点
- TranslateAnimation.RELATIVE_TO_SELF,-1//y轴终点
- );
- up_up.setDuration(1000);
- TranslateAnimation up_down = new TranslateAnimation(
- TranslateAnimation.RELATIVE_TO_SELF,0,//x轴起点
- TranslateAnimation.RELATIVE_TO_SELF,0,//x轴终点
- TranslateAnimation.RELATIVE_TO_SELF,-1,//y轴起点
- TranslateAnimation.RELATIVE_TO_SELF,0//y轴终点
- );
- up_down.setDuration(1000);
- //延迟执行set中的某一动画
- up_down.setStartOffset(500);
- set_up.addAnimation(up_up);//移动上去;
- set_up.addAnimation(up_down);//拉下来
- up_logo.startAnimation(set_up);
- //----------
- AnimationSet set_down = new AnimationSet(true);
- TranslateAnimation down_down = new TranslateAnimation(
- TranslateAnimation.RELATIVE_TO_SELF,0,//x轴起点
- TranslateAnimation.RELATIVE_TO_SELF,0,//x轴终点
- TranslateAnimation.RELATIVE_TO_SELF,0,//y轴起点
- TranslateAnimation.RELATIVE_TO_SELF,1//y轴终点
- );
- down_down.setDuration(1000);
- TranslateAnimation down_up = new TranslateAnimation(
- TranslateAnimation.RELATIVE_TO_SELF,0,//x轴起点
- TranslateAnimation.RELATIVE_TO_SELF,0,//x轴终点
- TranslateAnimation.RELATIVE_TO_SELF,1,//y轴起点
- TranslateAnimation.RELATIVE_TO_SELF,0//y轴终点
- );
- down_up.setDuration(1000);
- down_up.setStartOffset(500);
- set_down.addAnimation(down_down);//向下移动
- set_down.addAnimation(down_up);//往上拉动
- down_logo.startAnimation(set_down);
- }
来源: http://www.phperz.com/article/17/0605/334657.html