Android利用传感器仿微信摇一摇功能
作者:小小时光机 时间:2022-09-10 18:18:18
传感器
简单的介绍一下传感器:
就是设备用来感知周边环境变化的硬件。
Android中的传感器包含在传感器框架中,属于android.hardware.*(硬件部分)
传感器框架主要包含四个部分:
① SensorManager:用来获取传感器的入口,它是一个系统的服务,还可以为传感器注册与取消注册监听
② Sensor: 具体的传感器,包含了传感器的名字,类型,采样率
③ SensorEvent:传感器事件,包含了传感器采集回来的数据,传感器的精度
④ SensorEventListener:传感器的监听,主要监测传感器数据变化,精度变化…
Android播放音频系统提供了两种方式
① MediaPlayer 播放常规的音频,视频,通常用在播放器上
② SoundPool 声音池,通常用在小而频繁播放的音乐,需要同时播放多个音乐的
VIBRATE 所震动传感器需要添加权限
<uses-permission android:name="android.permission.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);
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现淘宝秒杀聚划算抢购自动提醒源码
Java并发编程同步器CountDownLatch
![](https://img.aspxhome.com/file/2023/1/85491_0s.png)
JAVA 中Spring的@Async用法总结
Java利用Dijkstra算法求解拓扑关系最短路径
![](https://img.aspxhome.com/file/2023/0/68510_0s.jpg)
浅谈Java代码的 微信长链转短链接口使用 post 请求封装Json(实例)
Springboot整合log4j2日志全解总结
![](https://img.aspxhome.com/file/2023/8/96068_0s.jpg)
Spring bean的实例化和IOC依赖注入详解
java实现超市管理系统
![](https://img.aspxhome.com/file/2023/4/106124_0s.jpg)
Android移动应用开发指南之六种布局详解
![](https://img.aspxhome.com/file/2023/9/137449_0s.png)
C#将Excel中的数据转换成DataSet
android Tween Animation属性设置方法实例
![](https://img.aspxhome.com/file/2023/5/137915_0s.jpg)
Spring创建bean实例的几种方式分享
Java注解@Transactional事务类内调用不生效问题及解决办法
Java访问WebService返回XML数据的方法
详解Mybatis-plus中更新date类型数据遇到的坑
c#字符串编码问题的处理解决
![](https://img.aspxhome.com/file/2023/7/88407_0s.jpg)
Android app启动图适配方法实例
![](https://img.aspxhome.com/file/2023/9/137829_0s.png)
c# 图片加密解密的实例代码
IDEA 单元测试报错:Class not found:xxxx springboot的解决
![](https://img.aspxhome.com/file/2023/3/69663_0s.png)