Android摇一摇振动效果Demo
前言
原理
Android传感器Sensor使用
Sensor使用步骤
传感器事件接口
public interface SensorEventListener {
/**
* Called when sensor values have changed.
* <p>See {@link android.hardware.SensorManager SensorManager}
* for details on possible sensor types.
* <p>See also {@link android.hardware.SensorEvent SensorEvent}.
*
* <p><b>NOTE:</b> The application doesn't own the
* {@link android.hardware.SensorEvent event}
* object passed as a parameter and therefore cannot hold on to it.
* The object may be part of an internal pool and may be reused by
* the framework.
*
* @param event the {@link android.hardware.SensorEvent SensorEvent}.
*/
public void onSensorChanged(SensorEvent event);
/**
* Called when the accuracy of a sensor has changed.
* <p>See {@link android.hardware.SensorManager SensorManager}
* for details.
*
* @param accuracy The new accuracy of this sensor
*/
public void onAccuracyChanged(Sensor sensor, int accuracy);
}
Android振动实现
import android.app.Activity;
import android.app.Service;
import android.os.Vibrator; public class VibratorHelper {
public static void Vibrate(final Activity activity, long milliseconds) {
Vibrator vibrator = (Vibrator) activity
.getSystemService(Service.VIBRATOR_SERVICE);
vibrator.vibrate(milliseconds);
} public static void Vibrate(final Activity activity, long[] pattern,
boolean isRepeat) {
Vibrator vibrator = (Vibrator) activity
.getSystemService(Service.VIBRATOR_SERVICE);
vibrator.vibrate(pattern, isRepeat ? 1 : -1);
}
}
同一时候,还须要在AndroidManifest.xml里添加振动权限:
<uses-permission android:name="android.permission.VIBRATE"/>
解释一下Vibrate方法的參数:
摇一摇振动Demo实现
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast; public class MainActivity extends Activity {
private SensorManager sensorManager;
private SensorEventListener shakeListener;
private AlertDialog.Builder dialogBuilder; private boolean isRefresh = false; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
shakeListener = new ShakeSensorListener(); dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setPositiveButton("确定", new OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
isRefresh = false;
dialog.cancel();
}
}).setMessage("摇到了一个美丽妹子!").create();
} @Override
protected void onResume() {
sensorManager.registerListener(shakeListener,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_FASTEST);
super.onResume();
} @Override
protected void onPause() {
// acitivity后台时取消监听
sensorManager.unregisterListener(shakeListener); super.onPause();
} private class ShakeSensorListener implements SensorEventListener {
private static final int ACCELERATE_VALUE = 20; @Override
public void onSensorChanged(SensorEvent event) { // Log.e("zhengyi.wzy", "type is :" + event.sensor.getType()); // 推断是否处于刷新状态(比如微信中的查找附近人)
if (isRefresh) {
return;
} float[] values = event.values; /**
* 一般在这三个方向的重力加速度达到20就达到了摇晃手机的状态 x : x轴方向的重力加速度,向右为正 y :
* y轴方向的重力加速度,向前为正 z : z轴方向的重力加速度。向上为正
*/
float x = Math.abs(values[0]);
float y = Math.abs(values[1]);
float z = Math.abs(values[2]); Log.e("zhengyi.wzy", "x is :" + x + " y is :" + y + " z is :" + z); if (x >= ACCELERATE_VALUE || y >= ACCELERATE_VALUE
|| z >= ACCELERATE_VALUE) {
Toast.makeText(
MainActivity.this,
"accelerate speed :"
+ (x >= ACCELERATE_VALUE ? x
: y >= ACCELERATE_VALUE ? y : z),
Toast.LENGTH_SHORT).show(); VibratorHelper.Vibrate(MainActivity.this, 300);
isRefresh = true;
dialogBuilder.show();
} } @Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
} } }
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3p5XzE5ODg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
Android摇一摇振动效果Demo的更多相关文章
- android 摇一摇+震动+声音效果
文章链接:https://mp.weixin.qq.com/s/n6EXvfmpNPtWM1kEnGgwUA 摇一摇红包效果已经是老生常谈的了,利用手机的传感器识别摇一摇,同时过程中进行动画+震动+声 ...
- Android的加速度传感器模拟摇一摇的效果-android学习之旅(66)
主要介绍一下android的加速传感器的简单用法,模拟摇一摇 ,如果x,y,z三个方向的加速度超过了15,就会弹出Toast,当然你可以设置更复杂的策略,比如判断间隔 代码如下 public clas ...
- Android 摇一摇之双甩功能
Android 摇一摇之双甩功能 最近做一个摇一摇的功能 网上相关代码很多 但是这次的需求有点奇葩 要求是摇两次才生效 看起来好像很简单 但真正要做遇到的问题还是很多 时间限制 机型灵敏性 摇动的方式 ...
- Android仿iPhone晃动撤销输入功能(微信摇一摇功能)
重力传感器微信摇一摇SensorMannager自定义alertdialogSensorEventListener 很多程序中我们可能会输入长文本内容,比如短信,写便笺等,如果想一次性撤销所有的键入内 ...
- 玩转Android之加速度传感器的使用,模仿微信摇一摇
Android系统带的传感器有很多种,最常见的莫过于微信的摇一摇了,那么今天我们就来看看Anroid中传感器的使用,做一个类似于微信摇一摇的效果. OK ,废话不多说,我们就先来看看效果图吧: 当我摇 ...
- HTML5实现“摇一摇”效果
在HTML5中,DeviceOrientation特性所提供的DeviceMotion事件封装了设备的运动传感器时间,通过改时间可以获取设备的运动状态.加速度等数据(另还有deviceOrientat ...
- iOS-高仿微信摇一摇动画效果加震动音效
概述 摇一摇动画效果 (加震动音效) 详细 代码下载:http://www.demodashi.com/demo/10707.html 众所周知, 微信中的摇一摇功能: 搜索人/歌曲/电视,同样在一些 ...
- android摇一摇实现(仿微信)
这个demo模仿的是微信的摇一摇,是一个完整的demo,下载地址在最下面.下面是demo截图: 步驟: 1.手机摇动监听,首先要实现传感器接口SensorEventLi ...
- Android 摇一摇 之 传感器片
要监视原始的传感器数据,你需要实现两个通过SensorEventListener接口暴露的回调方法:onAccuracyChanged()和onSensorChanged(). 传感器数据的速度值,这 ...
随机推荐
- C#判断程序是否以管理员身份运行,否则以管理员身份重新打开
/// <summary> /// 判断程序是否是以管理员身份运行. /// </summary> public static bool IsRunAsAdmin() { Wi ...
- Oracle中REGEXP_SUBSTR函数(转)
Oracle中REGEXP_SUBSTR函数 Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下:在oracle中,使用一条语句实现将'17,20,23'拆分成'17','20','2 ...
- linux操作笔记
[fedora可以ping通但是isReachAble返回false的原因] fedora18+ 防火墙设置原因,导致tcp端口不通,返回no route to host错误. 关闭防火墙命令: sy ...
- mysql 分区信息查看
select partition_name part,partition_expression expr,partition_description descr,table_rows from INF ...
- MySQL和MsSQL实时自动同步---SyncNavigator 数据库同步软件
需要MySQL数据库支持的狐友们有福了,MySQL和MsSQL实时自动同步---SyncNavigator 数据库同步软件 使用SyncNavigator轻松实现数据库异地同步.断点续传.异构同步 ...
- idHTTP最简洁的修改和取得Cookie例子
procedure TForm1.Button1Click(Sender: TObject); var HTTP: TidHTTP; html, s: string; i: integer; begi ...
- 统计建模与R软件习题二答案
# 习题2 # 2.1 x=c(1,2,3) y=c(4,5,6) e=c(rep(1,3)) z=2*x+y+e;z x%*%y # 若x,y如答案那样定义为矩阵,则不能用%*%,因为,维数不对应, ...
- Python交互模式下方向键出现乱码
解决办法如下: 1.安装readline模块 readline库是bash shell用的库,包含许多功能,如命令行自动补全等. ubuntu下安装的命令: sudo apt-get instal ...
- ASP.NET MVC学习之路:模板页
1.MVC开发步骤: 控制器-视图. 2.每一个视图都会有一个默认的模板页:_ViewStart.cshtml. 名字不能改,只能叫_ViewStart. 3.Layout=”~/Views/Shar ...
- Windows 批处理文件
窗口自动关闭:批处理文件执行完之后,窗口会自动关闭,若想执行完之后,窗口不自动关闭的话,在文件末尾添加1. 批处理文件执行完之后,窗口会自动关闭2. 若想执行完之后,窗口不自动关闭的话,在文件末尾添加 ...