Sensor传感器(摇一摇)
<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传感器(摇一摇)的更多相关文章
- 利用传感器(sensor)实现微信摇一摇动画
所需要的权限: <uses-permission android:name="android.permission.VIBRATE"></uses-permiss ...
- iOS开发 传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- iOS开发——高级篇——传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- Android 摇一摇 之 传感器片
要监视原始的传感器数据,你需要实现两个通过SensorEventListener接口暴露的回调方法:onAccuracyChanged()和onSensorChanged(). 传感器数据的速度值,这 ...
- 玩转Android之加速度传感器的使用,模仿微信摇一摇
Android系统带的传感器有很多种,最常见的莫过于微信的摇一摇了,那么今天我们就来看看Anroid中传感器的使用,做一个类似于微信摇一摇的效果. OK ,废话不多说,我们就先来看看效果图吧: 当我摇 ...
- Android 使用加速度传感器实现摇一摇功能及优化
如有转载,请声明出处: 时之沙: http://blog.csdn.net/t12x3456 目前很多应用已经实现了摇一摇功能,这里通过讲解该功能的原理及实现回顾一下加速度传感器的使用: 1.首先获得 ...
- Android的加速度传感器模拟摇一摇的效果-android学习之旅(66)
主要介绍一下android的加速传感器的简单用法,模拟摇一摇 ,如果x,y,z三个方向的加速度超过了15,就会弹出Toast,当然你可以设置更复杂的策略,比如判断间隔 代码如下 public clas ...
- Android 摇一摇功能的注意事项
/**开始重力传感器的检测*/ public void start() { // 获得传感器管理器 sensorManager = (SensorManager) mContext.getSystem ...
- android摇一摇实现(仿微信)
这个demo模仿的是微信的摇一摇,是一个完整的demo,下载地址在最下面.下面是demo截图: 步驟: 1.手机摇动监听,首先要实现传感器接口SensorEventLi ...
- android手电筒(摇一摇也可开启手电筒)
package com.firefly.myflashlight; import android.app.Activity; import android.hardware.Camera; impor ...
随机推荐
- JS 中对变量类型判断的几种方式
文章整理搬运,出处不详,如有侵犯,请联系~ 数据类型判断和数据类型转换代码工具 在 JS 中,有 5 种基本数据类型和 1 种复杂数据类型,基本数据类型有:Undefined, Null, Boo ...
- 三维dp
https://cn.vjudge.net/contest/245857#problem/C 代码: #include<iostream> #include<string> # ...
- 列式数据库~clickhouse 场景以及安装
一 简介:列式数据库clickhouse的安装与基本操作二 基本介绍:ClickHouse来自俄罗斯,是一款列式数据库三 适用场景: 简单类型的大数据统计四 限制 1 不支持更新操作,不支持事 ...
- Picasso的使用
相信做Android开发的对Square公司一定不会陌生,大名鼎鼎的网络请求框架Retrofit就来源于它,今天学习的是该公司出品的图片加载框架Picasso. 项目地址 https://github ...
- Shiro后台实现验证权限
今天发现一个问题:使用shiro的时候,虽然隐藏掉了一些菜单,但是当我们通过get请求直接访问菜单的时候还是会访问到,也就是shiro可以在界面实现隐藏一些信息,但是没有真正的根据权限码验证请求,于是 ...
- jquery菜单插件
原理很简单. 涉及到知识点: 1.jquery的position注意这里是jquery的position,不是css的position offset的概念 2.>的概念. 3..ulh>l ...
- jquery筛选数组之grep、each、inArray、map的用法及遍历son对象(转)
grep [传入的参数为返回bool类型的函数] <script type='text/javascript' src="/jquery.js"></script ...
- 【ARTS】01_11_左耳听风-20190121~20190127
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- Linux性能分析的前60000毫秒【转】
Linux性能分析的前60000毫秒 为了解决性能问题,你登入了一台Linux服务器,在最开始的一分钟内需要查看什么? 在Netflix我们有一个庞大的EC2 Linux集群,还有非常多的性能分析工具 ...
- X-Forwarded-For 的一些理解
X-Forwarded-For 是一个 HTTP 扩展头部, 主要是为了让 Web 服务器获取访问用户的真实 IP 地址(其实这个真实未必是真实的,后面会说到). 那为什么 Web 服务器只有通过 X ...