Android系统带的传感器有很多种,最常见的莫过于微信的摇一摇了,那么今天我们就来看看Anroid中传感器的使用,做一个类似于微信摇一摇的效果。

OK ,废话不多说,我们就先来看看效果图吧:

当我摇动手机的时候这里的动画效果基本和微信上的动画效果一致,这里请大家自行脑补微信摇一摇画面。

那我们就动手吧。

1.布局文件

好,那我们先来看看布局文件吧,在布局文件的正中央是一个花的图片,上图大家看到的手机图片实际上是两张图片拼接在一起,将花的那张图片遮住了,当摇一摇的时候,这两张图片分别向上或者向下移动,然后花的图片就可以显示出来。OK,基本原理就是这样,我们来看看代码:

<?xml version="1.0" encoding="utf-8"?>
<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:background="#1f1f1f"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/flower"/> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"> <ImageView
android:id="@+id/up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/up"/> <ImageView
android:id="@+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/down"/>
</LinearLayout>
</RelativeLayout>

2.传感器监听手机晃动

既然要监听手机加速度的变化,那我首先需要获取系统的传感器:

//获取到一个传感器管理器
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
//获得一个加速度传感器
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

这两行代码首先是获取一个传感器管理器,然后获取加速度传感器,因为关于传感器的API 有很多,这里你需要指明自己要获取的是哪一个传感器。拿到传感器之后,需要注册监听,如下:

sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);

注册传感器的监听一共需要三个参数,第一个是监听器,第二个是加速度传感器,第三个是传感器的灵敏度,传感器的灵敏度一共分为四级,如下,从上往下灵敏度依次降低:

    1. SENSOR_DELAY_FASTEST
2. SENSOR_DELAY_GAME
3. SENSOR_DELAY_UI
4. SENSOR_DELAY_NORMAL

OK ,注册完之后,我们还是来看看这个监听器是什么吧:

private SensorEventListener listener = new SensorEventListener() {
//当手机的加速度发生变化时调用
@Override
public void onSensorChanged(SensorEvent event) {
//获取手机在不同方向上加速度的变化
float valuesX = Math.abs(event.values[0]);
float valuesY = Math.abs(event.values[1]);
float valuesZ = Math.abs(event.values[2]); if (valuesX > 17 || valuesY > 17 || valuesZ > 17) {
startAnimation();
playSound();
}
} @Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { }
};

这个listener中一共就两个方法,一个是当手机的加速度发生改变的时候调用,还有一个是当传感器的灵敏度发生改变的时候调用,当手机的加速度发生改变的时候,我们可以获取到手机在X 、Y、Z 三个维度上的变化值,拿到这个值之后,我们只需要进行简单的比较即可,如果有任意一个方向的值大于17,则认为有人在晃动手机,这个时候开启动画和声音的播放。

3.开启动画和声音

动画实际上就是两个平移动画,我们来看看:

    private void startAnimation() {
//如果两次晃动手机的时间小于1秒,则只执行一次动画
long currentTimeMillis = System.currentTimeMillis();
if (currentTimeMillis - lastTime < 1000) {
return;
}
lastTime = currentTimeMillis;
AnimationSet upSet = new AnimationSet(true);
TranslateAnimation upUp = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF,
0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1);
upUp.setDuration(1000);
TranslateAnimation upDown = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF,
0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1);
upDown.setDuration(1000);
upDown.setStartOffset(1000);
upSet.addAnimation(upUp);
upSet.addAnimation(upDown);
up.startAnimation(upSet);
AnimationSet downSet = new AnimationSet(true);
TranslateAnimation downUp = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF,
0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1);
downUp.setDuration(1000);
TranslateAnimation downDown = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF,
0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1);
downDown.setDuration(1000);
downDown.setStartOffset(1000);
downSet.addAnimation(downUp);
downSet.addAnimation(downDown);
down.startAnimation(downSet);
}

至于声音,由于我这里只是播放比较短小的音效而已,所以并没有必要使用MediaPlayer,我可以通过一个声音池来解决这个问题,代码如下:

    /**
* 初始化声音池
*/
private void initSoundPool() {
if (Build.VERSION.SDK_INT > 20) {
SoundPool.Builder builder = new SoundPool.Builder();
//1.最大并发流数
builder.setMaxStreams(3);
AudioAttributes.Builder aaBuilder = new AudioAttributes.Builder();
aaBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
builder.setAudioAttributes(aaBuilder.build());
soundPool = builder.build();
} else {
soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
}
//加载一个音频文件
sound1 = soundPool.load(this, R.raw.awe, 1);
}

在创建一个声音池的时候我采取了两种不同的方案,如果系统的版本大于20,则是用第一种方式获取声音池,否则使用第二种方式获取声音池。获取声音池之后,再通过声音池加载一个音频文件。加载完成之后,我就可以对这个音频文件进行播放了,如下:

        //1.声音的id
//2.3.表示左右声道的音量
//4.优先级
//5.是否循环
//6.声音播放速率
soundPool.play(sound1, 1, 1, 0, 0, 1);

每个参数的含义都写的很清楚了,大家又不清楚的地方可以直接看源码,这里的源码注释很好懂。

最后一步就是开启手机震动了,开启手机震动,我需要首先获取震动服务,如下:

 //获取手机震动服务
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);

然后调用Vibrator类中的vibrator方法执行震动,如下:

//1.表示震动的节奏off/on/off/on/off/on......
//2.表示是否重复震动,-1表示不重复
vibrator.vibrate(new long[]{100, 200, 100, 200, 100, 200}, -1);

手机震动一定要记得添加震动权限哦,如下:

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

OK ,最后,在销毁Activity的时候要解除对传感器的监听,同时释放声音池资源,如下:

    @Override
protected void onDestroy() {
super.onDestroy();
//解除对加速度传感器的监听
sensorManager.unregisterListener(listener);
if (soundPool != null) {
//声音池释放资源
soundPool.release();
}
}

完整的Activity 代码如下:

public class MainActivity extends AppCompatActivity {
private ImageView up;
private ImageView down;
//上一次晃动手机的时间
private long lastTime;
private SoundPool soundPool;
private int sound1;
private Vibrator vibrator;
private SensorEventListener listener = new SensorEventListener() {
//当手机的加速度发生变化时调用
@Override
public void onSensorChanged(SensorEvent event) {
//获取手机在不同方向上加速度的变化
float valuesX = Math.abs(event.values[0]);
float valuesY = Math.abs(event.values[1]);
float valuesZ = Math.abs(event.values[2]); if (valuesX > 17 || valuesY > 17 || valuesZ > 17) {
startAnimation();
playSound();
}
} @Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { }
};
private SensorManager sensorManager; private void playSound() {
//1.声音的id
//2.3.表示左右声道的音量
//4.优先级
//5.是否循环
//6.声音播放速率
soundPool.play(sound1, 1, 1, 0, 0, 1);
//手机震动
//1.表示震动的节奏off/on/off/on/off/on......
//2.表示是否重复震动,-1表示不重复
vibrator.vibrate(new long[]{100, 200, 100, 200, 100, 200}, -1);
} private void startAnimation() {
//如果两次晃动手机的时间小于1秒,则只执行一次动画
long currentTimeMillis = System.currentTimeMillis();
if (currentTimeMillis - lastTime < 1000) {
return;
}
lastTime = currentTimeMillis;
AnimationSet upSet = new AnimationSet(true);
TranslateAnimation upUp = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF,
0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1);
upUp.setDuration(1000);
TranslateAnimation upDown = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF,
0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1);
upDown.setDuration(1000);
upDown.setStartOffset(1000);
upSet.addAnimation(upUp);
upSet.addAnimation(upDown);
up.startAnimation(upSet);
AnimationSet downSet = new AnimationSet(true);
TranslateAnimation downUp = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF,
0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1);
downUp.setDuration(1000);
TranslateAnimation downDown = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF,
0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1);
downDown.setDuration(1000);
downDown.setStartOffset(1000);
downSet.addAnimation(downUp);
downSet.addAnimation(downDown);
down.startAnimation(downSet);
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
up = ((ImageView) findViewById(R.id.up));
down = ((ImageView) findViewById(R.id.down));
initSensor();
initSoundPool();
//获取手机震动服务
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
} /**
* 初始化声音池
*/
private void initSoundPool() {
if (Build.VERSION.SDK_INT > 20) {
SoundPool.Builder builder = new SoundPool.Builder();
//1.最大并发流数
builder.setMaxStreams(3);
AudioAttributes.Builder aaBuilder = new AudioAttributes.Builder();
aaBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
builder.setAudioAttributes(aaBuilder.build());
soundPool = builder.build();
} else {
soundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
}
//加载一个音频文件
sound1 = soundPool.load(this, R.raw.awe, 1);
} /**
* 初始化传感器
*/
private void initSensor() {
//获取到一个传感器管理器
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
//获得一个加速度传感器
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//注册传感器监听,
//1.监听器
//2.加速度传感器
//3.传感器灵敏度
//传感器灵敏度分为四级,从上往下灵敏度依次降低
//SENSOR_DELAY_FASTEST
//SENSOR_DELAY_GAME
//SENSOR_DELAY_UI
//SENSOR_DELAY_NORMAL
sensorManager.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME);
} @Override
protected void onDestroy() {
super.onDestroy();
//解除对加速度传感器的监听
sensorManager.unregisterListener(listener);
if (soundPool != null) {
//声音池释放资源
soundPool.release();
}
}
}

以上。

玩转Android之加速度传感器的使用,模仿微信摇一摇的更多相关文章

  1. Android的加速度传感器模拟摇一摇的效果-android学习之旅(66)

    主要介绍一下android的加速传感器的简单用法,模拟摇一摇 ,如果x,y,z三个方向的加速度超过了15,就会弹出Toast,当然你可以设置更复杂的策略,比如判断间隔 代码如下 public clas ...

  2. Android 使用加速度传感器实现摇一摇功能及优化

    如有转载,请声明出处: 时之沙: http://blog.csdn.net/t12x3456 目前很多应用已经实现了摇一摇功能,这里通过讲解该功能的原理及实现回顾一下加速度传感器的使用: 1.首先获得 ...

  3. Android Studio精彩案例(三)《模仿微信ViewPage+Fragment实现方式二》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 写在前面的话:此专栏是博主在工作之余所写,每一篇文章尽可能写的思路清晰一些,属于博主的"精华"部分,不同于以往专栏 ...

  4. 安卓高级5 传感器和震动 模仿微信摇一摇Ui效果

    效果图: 所用的Ui就三张图: 案例代码: 结构 MainActivity.java package com.example.myapp; import android.content.Intent; ...

  5. wing带你玩转自定义view系列(3)模仿微信下拉眼睛

    发现了爱神的自定义view系列,我只想说一个字:凸(艹皿艹 ) !!相见恨晚啊,早看到就不会走这么多弯路了 另外相比之下我这完全是小儿科..所以不说了,这篇是本系列完结篇....我要从零开始跟随爱哥脚 ...

  6. Android加速度传感器

    Android加速度传感器 效果图 手机平放桌面的两张截屏,数据一直在刷新 源码 下载地址(Android Studio工程):http://download.csdn.net/detail/q487 ...

  7. Android加速度传感器实现“摇一摇”,带手机振动

    由于代码有点多,所以就分开写了,注释还算详细,方便学习 Activity package com.lmw.android.test;   import android.app.Activity; im ...

  8. Android的重力传感器(3轴加速度传感器)简单实例

    重力感应主要是依靠手机的加速度传感器(accelerometer)来实现 在Android的开发中一共有八种传感器但是不一定每一款真机都支持这些传感器.因为很多功能用户根本不care的所以可能开发商会 ...

  9. Android传感器——加速度传感器

    步骤如下: 1. 调用Context的getSystemService(Context.SENSOR_SERVICE)方法获取SensorManager,SensorManager对象代表系统的传感器 ...

随机推荐

  1. 屏幕尺寸,屏幕分辨率,屏幕密度,各种长宽单位(px,sp,dp,in.pt,mm)

    常见长宽单位表 名称 单位缩写 单位全拼 介绍 屏幕尺寸 '' 或 in inch 屏幕的大小,通常用屏幕对角线的长度表示.单位是寸 屏幕分辨率 px pixels 整个屏幕的像素数,一般用屏幕的像素 ...

  2. 【HDOJ】1225 Football Score

    这种结构体排序的题,十分容易考上机题,qsort+结构体解决.马上就要机考了,多练习一下这样的题目也好. #include <stdio.h> #include <string.h& ...

  3. 获取本机的ip

    https://4sysops.com/archives/ipv6-tutorial-part-6-site-local-addresses-and-link-local-addresses/ In ...

  4. Aspcms所有标签调用

    错误号3002: 出现此问题一般是有些频道模板不全,或者缺少一部分模板导致,或者没有单独设置html文件夹,并将所有html页面放进去 基本语法 {aspcms:sitepath} 网站终极目录(可放 ...

  5. bzoj3611

    会构建虚树之后就是noip提高组的题目了稍微难一点的是求代价和,只要注意按一个方向避免重复计算贡献即可 ; type node=record po,next:longint; end; ..] of ...

  6. 【转载】CentOS LVM磁盘扩容

    转自:http://blog.sina.com.cn/s/blog_8882a6260101cpfs.html EXSI5.1主机有一个linux虚拟机,系统是centos运行httpd服务,因为是多 ...

  7. 【原创】LoadRunner Java Vuser开发环境配置指南

    1 编写目的 本文主要介绍Java运行环境的配置,同时通过编写HelloWorld程序,讲解在LoadRunner下如何开发简单的Java Vuser脚本.关于Java语言的深入学习,大家可以参考其他 ...

  8. Nginx反向代理+DNS轮询+IIS7.5 千万PV 百万IP 双线 网站架构案例

    原文地址:http://www.jb51.net/article/31844.htm Nginx  ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 ...

  9. 【HTML】Advanced2:Conditional Comments

    1.try and figure out what is sensible for you to support. Are your web site visitors likely to be us ...

  10. Tools that help you scrape web data----帮助你收集web数据的工具

    There are many programs that can be used to extract bulk information from a web site, including brow ...