<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. 计算机网络之互联网|因特网|万维网|HTTP|HTML之间的关系辨析

    本博文基于知乎"Web 是什么意思?"一问而引起.(本文均属于博主从知乎上自身所答搬运而至). 如无特殊声明,括号()内以分号分隔的名词均等效. 本文如无特殊引用声明,则所有内容版 ...

  2. [C++]数组与指针[二维数组与指针]

  3. 2017CCPC秦皇岛 C题Crusaders Quest&&ZOJ3983【模拟+STL】

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3983 题意: 给定9个血槽,有三种物品,每次可以把连续相同的物品抵消 ...

  4. D- 泛型练习 ,继承,方法

    unit Unit3; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  5. Mybatis进阶学习笔记——关系查询——一对多查询

    一个客户拥有多个订单 <resultMap type="User" id="UserOrderResultMap"> <id column=& ...

  6. android aysncTask面试解析

  7. Spring之JDBCTemplate学习

    一.Spring对不同的持久化支持: Spring为各种支持的持久化技术,都提供了简单操作的模板和回调 ORM持久化技术 模板类 JDBC org.springframework.jdbc.core. ...

  8. openstack swift节点安装手册2-创建rings

    以下步骤需要在controller节点上进行操作: 切换到/etc/swift目录下进行如下操作: 一.创建account ring 1.创建account.builder文件 swift-ring- ...

  9. 日常用的css基础和自己常用的js封装

    css基础:base /* * 初始化 */ *::after, *::before { box-sizing: border-box; } body { font-family: 'Microsof ...

  10. 读SRE Google运维解密有感(三)

    前言 这是读“SRE Google运维解密”有感第三篇,之前的文章可访问www.addops.cn来查看.我们今天来聊聊“on call”也就是运维值班制度, 本人到目前为止也还在参与一线运维的值班, ...