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(). 传感器数据的速度值,这 ...
随机推荐
- CSS3 用户界面
CSS3用户界面 在CSS3中,新的用户界面特性包括重设元素尺寸,盒尺寸以及轮廓等. 用户界面属性: resize box-sizing outline-offset 浏览器支持 属性 浏览器支持 r ...
- Lesson 5: Typography in Product Design
Lesson 5: Typography in Product Design Article 1: Interactive Guide to Blog Typography 布局(Layout) 用空 ...
- SQL 2008R2 日期转换
--SQL CONVERT日期转换 print GETDATE() --相同 Select CONVERT(varchar(100), GETDATE(), 0) AS NDateTime --: 0 ...
- C++拾遗(四)指针相关
指针声明与初始化 在将指针初始化为一个确定的地址后,才能安全的对指针使用 *操作. 将整数赋值给指针时要使用强制转换(typeName *). 分配内存 C中用malloc(); C++更提倡使用ne ...
- 利用谷歌 kaptcha 进行验证码生成
package main.com.smart.controller; import com.google.code.kaptcha.Producer; import main.com.smart.ut ...
- HTML5 canvas 合成属性
合成属性 globalAlpha 设置或返回绘图的当前 alpha 或透明值 globalCompositeOperation ...
- div边框阴影的实现【转载】
box-shadow:阴影水平偏移值(可取正负值): 阴影垂直偏移值(可取正负值):阴影模糊值:阴影颜色: Firefox支持Box Shadow(阴影):-moz-box-shadow:2px 2p ...
- css3动画和jquery动画使用中要注意的问题
前一阵子写demo的时候遇到这样一个问题,就是给元素添加css3或者jquery动画时,在动画结束前不能准确取到元素的css属性. 1. css3动画讨论 先看代码: html: <div id ...
- php的header()函数前有echo输出情况分析
php的header()方法一般被用来告知浏览器做一些什么样的操作,比如跳转,刷新等等:而调用header方法前,如果使用echo输出一些内容,会发生什么情况呢?会对header函数有影响吗?我们实验 ...
- Unity3D 经验记录
1.using UnityEngine.SceneManagement; 当在01场景调用02场景时,再载入回01场景时,代码保存的变量不会初始化,预制物体脚本内的变量会初始化. 2.当子物体太多时, ...