<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>
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 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 = (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);
}
}

Sensor传感器(摇一摇)的更多相关文章

  1. 利用传感器(sensor)实现微信摇一摇动画

    所需要的权限: <uses-permission android:name="android.permission.VIBRATE"></uses-permiss ...

  2. iOS开发 传感器(加速计、摇一摇、计步器)

    一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...

  3. iOS开发——高级篇——传感器(加速计、摇一摇、计步器)

    一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...

  4. Android 摇一摇 之 传感器片

    要监视原始的传感器数据,你需要实现两个通过SensorEventListener接口暴露的回调方法:onAccuracyChanged()和onSensorChanged(). 传感器数据的速度值,这 ...

  5. 玩转Android之加速度传感器的使用,模仿微信摇一摇

    Android系统带的传感器有很多种,最常见的莫过于微信的摇一摇了,那么今天我们就来看看Anroid中传感器的使用,做一个类似于微信摇一摇的效果. OK ,废话不多说,我们就先来看看效果图吧: 当我摇 ...

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

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

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

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

  8. Android 摇一摇功能的注意事项

    /**开始重力传感器的检测*/ public void start() { // 获得传感器管理器 sensorManager = (SensorManager) mContext.getSystem ...

  9. android摇一摇实现(仿微信)

    这个demo模仿的是微信的摇一摇,是一个完整的demo,下载地址在最下面.下面是demo截图:                 步驟: 1.手机摇动监听,首先要实现传感器接口SensorEventLi ...

  10. android手电筒(摇一摇也可开启手电筒)

    package com.firefly.myflashlight; import android.app.Activity; import android.hardware.Camera; impor ...

随机推荐

  1. 青云VPC网络配置

    1 创建VPC网络 2 申请公网IP 3 回到VPC图形界面绑定公网ip 4 创建私有网络,并绑定私有网络 5 创建3台主机 6 新建防火墙,并绑定到VPC 7 配置VPC端口转发规则 8 添加防火墙 ...

  2. python - classs内置方法 solt

    solt # __solt__ # 是一个类变量,变量值可以是列表.元组或者是可迭代对象,也可以是一个字符串 # (以为这所有实例只有一种数据属性) # # 作用:(为了节省内存空间,减少过多的实例属 ...

  3. 【Udacity并行计算课程笔记】- Lesson 2 The GPU Hardware and Parallel Communication Patterns

    本小节笔记大纲: 1.Communication patterns gather,scatter,stencil,transpose 2.GPU hardware & Programming ...

  4. Docker三要素

    一.镜像(Image) Docker镜像(Image)就是一个只读的模板,镜像可以用来创建Docker容器,一个镜像可以创建很多容器. Docker 面向对象 镜像 类(class) 容器 实例对象 ...

  5. Generative Adversarial Nets(原生GAN学习)

    学习总结于国立台湾大学 :李宏毅老师 Author: Ian Goodfellow • Paper: https://arxiv.org/abs/1701.00160 • Video: https:/ ...

  6. java多线程系列五、并发容器

    一.ConcurrentHashMap 1.为什么要使用ConcurrentHashMap 在多线程环境下,使用HashMap进行put操作会引起死循环,导致CPU利用率接近100%,HashMap在 ...

  7. 巧用CASE WHEN 验证用户登录信息

    最近逛博客园的时候偶然看到一个很巧妙的SQL,巧妙利用CASE WHEN 实现一个简单的 SQL 同时验证用户帐号是否存在.密码是否正确.晓菜鸟之前的做法都是根据用户名和密码一起验证,如果验证失败直接 ...

  8. 『实践』Matlab实现Flyod求最短距离及存储最优路径

    Matlab实现Flyod求最短距离及存储最优路径 一.实际数据 已知图中所有节点的X.Y坐标. 图中的节点编号:矩阵中的编号 J01-J62:1-62; F01-F60:63-122; Z01-Z0 ...

  9. oracle 分区 查询

    显示数据库所有分区表的信息:DBA_PART_TABLES 显示当前用户可访问的所有分区表信息:ALL_PART_TABLES 显示当前用户所有分区表的信息:USER_PART_TABLES 显示表分 ...

  10. TYpeScript接口的使用

    1.接口中的属性值的使用: // 作用是强制类型检查 interface Iperson { name: string; age: string; } class Person { construct ...