<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. kan

    http://blog.csdn.net/yahohi/article/details/7427724 http://duanhengbin.iteye.com/blog/1706635 http:/ ...

  2. GCC 符号表小结【转】

    转自:https://blog.csdn.net/swedenfeng/article/details/53417085 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog ...

  3. getOutputStream与getWriter方法

    getOutputStream方法用于返回Servlet引擎创建的字节输出流对象,Servlet程序可以按字节形式输出响应正文.getWriter方法用于返回Servlet引擎创建的字符输出流对象,S ...

  4. java 基础(转自索宁)

    一.方法函数 函数也称为方法,就是定义在类中的具有特定功能的一段独立代码.用于定义功能,提高代码的复用性. 函数的特点1> 定义函数可以将功能代码进行封装,便于对该功能进行复用:2> 函数 ...

  5. rsync使用详解

    1.什么是Rsync Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 程两个 ...

  6. compile php with openssl on mac osx error 填坑

    从源码手动编译 PHP 时出现如下错误:           Default   1 2 3 4 5 6 7 8 9 10 11 12 13 14 Undefined symbols for arch ...

  7. RHEL7 配置iSCSI模拟环境

    在之前文章<multipath多路径实验01-构建iSCSI模拟环境>中,已经介绍了如何构建iSCSI模拟环境(RHEL6),但在RHEL7中已经不适用,本文记录下新的配置方法. 环境:R ...

  8. 100以内奇偶数(for循环)

  9. vue scoped 穿透_vue 修改内部组件样式问题

    何为scoped? 在vue文件中的style标签上,有一个特殊的属性:scoped.当一个style标签拥有scoped属性时,它的CSS样式就只能作用于当前的组件,也就是说,该样式只能适用于当前组 ...

  10. js数值进制互转

    十进制转换为二进制: var num = 100; console.log(num.toString(2)); toString()方法可把一个 Number 对象转换为一个字符串,并返回结果. 语法 ...