前言
绅士们,今天我们来了解下西金热常用的道具效果——震动效果!!!想想是不是很激动,好了,废话不多说,我们开始。

在安卓中,震动器是Vibrator这个类,是系统提供的一个服务(前提是你的手机支持震动),具体的操作如下:
Vibrator vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
震动需要调用的方法有:
方法 | 说明 |
---|---|
cancel() | 关闭或者停止振动器。 |
hasVibrator() | 判断硬件是否有振动器。 |
vibrate(long milliseconds) | 控制手机振动时长(毫秒) |
vibrate(long[] pattern,int repeat) | 指定手机以pattern指定的模式振动。 参数1:是一个数组,里面的数字下标为单数时,为停止时间,偶数时为震动时间,比如 [100,100] 就为停止100ms后震动100ms,参数2 repeat:重复次数,如果是-1的只振动一次,如果是0的话则一直振动 。 |
然后,我们就可以对手机为所欲为了。
比如,我们让手机让手机震动3秒就可以这么做
if (vibrator.hasVibrator()){
effect = VibrationEffect.createOneShot(3000, VibrationEffect.DEFAULT_AMPLITUDE);
vibrator.vibrate(effect);
}else {
Log.e("TOP","该设备没有震动器");
}
咦???VibrationEffect.createOneShot这又是啥东西???上面的方法里面没有出现呀!!!大家莫慌!!!

本来我也是准备用vibrate(long milliseconds)这个方法的,可是,代码告诉我,它已经过时了,让我换一种方式操作,呐,这是证据
/**
* Vibrate constantly for the specified period of time.
*
* @param milliseconds The number of milliseconds to vibrate.
*
* @deprecated Use {@link #vibrate(VibrationEffect)} instead.
*/
@Deprecated
@RequiresPermission(android.Manifest.permission.VIBRATE)
public void vibrate(long milliseconds) {
vibrate(milliseconds, null);
}
它让我用vibrate(VibrationEffect)这个方法,然后,我看了下VibrationEffect这个类,emmm,算了,还是看看它之前是怎么实现的。于是,就找到了这个
public void vibrate(long milliseconds, AudioAttributes attributes) {
try {
// This ignores all exceptions to stay compatible with pre-O implementations.
VibrationEffect effect =
VibrationEffect.createOneShot(milliseconds, VibrationEffect.DEFAULT_AMPLITUDE);
vibrate(effect, attributes);
} catch (IllegalArgumentException iae) {
Log.e(TAG, "Failed to create VibrationEffect", iae);
}
}
啊哈哈哈哈,被我找到了,它原来是用的这个方法
VibrationEffect effect =
VibrationEffect.createOneShot(milliseconds, VibrationEffect.DEFAULT_AMPLITUDE);
所以,我就写成了上面那种方式。

找个带震动器的手机运行,嘿嘿嘿,一直震动一直爽。。。 恩????? 怎么震动不了?怎么点都无法震动????莫慌,老夫忘记告诉你加权限了....
<uses-permission android:name="android.permission.VIBRATE"/>
实现了持续震动,万一有人想间接性震动咋办,莫慌,到vibrate(long[] pattern,int repeat)这个方法上场了,我们可以这么实现
if (vibrator.hasVibrator()){
vibrator.cancel();
effect = VibrationEffect.createWaveform(new long[]{100,100}, 0);
vibrator.vibrate(effect);
}else {
Log.e("TOP","该设备没有震动器");
}
1毫秒1毫秒的震动,这节奏.....
有心的绅士可能发现了,我特么又没有用到上面的方法,又是一个上面没有出现的方法,其实,vibrate(long[] pattern,int repeat)这个方法也过时了,但是,我们顺藤摸瓜找到了它的替代方法
public void vibrate(long[] pattern, int repeat, AudioAttributes attributes) {
// This call needs to continue throwing ArrayIndexOutOfBoundsException but ignore all other
// exceptions for compatibility purposes
if (repeat < -1 || repeat >= pattern.length) {
Log.e(TAG, "vibrate called with repeat index out of bounds" +
" (pattern.length=" + pattern.length + ", index=" + repeat + ")");
throw new ArrayIndexOutOfBoundsException();
}
try {
vibrate(VibrationEffect.createWaveform(pattern, repeat), attributes); //1
} catch (IllegalArgumentException iae) {
Log.e(TAG, "Failed to create VibrationEffect", iae);
}
}
大家注意标注1的那句,没错,这就是我们需要的,于是乎,就有了上面的这种写法了。
好了,震动器的大体用法就到这里,咱们有缘(车)再见!!

好了,这篇文章先到这里,后期有新的的再补充(客套话)。

网友评论