美文网首页
Vibraotr(震动器)的总结

Vibraotr(震动器)的总结

作者: Fastcv | 来源:发表于2019-07-31 15:37 被阅读0次

前言

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

在安卓中,震动器是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的那句,没错,这就是我们需要的,于是乎,就有了上面的这种写法了。

好了,震动器的大体用法就到这里,咱们有缘(车)再见!!

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


相关文章

  • Vibraotr(震动器)的总结

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

  • 树莓派+震动传感器+光线亮度传感器+红绿灯LED(2020-09

    震动传感器工作原理如下:一旦传感器检测到震动,弹簧就会震动并与触发针接触,从而产生触发信号。可通过调节电位计用于设...

  • Arduino震动传感器

    代码

  • 活动指示器、音量震动条、立体折叠、倒影

    活动指示器 音量震动条 立体折叠 倒影

  • 系统震动or震动反馈

    一.系统震动 1.AudioServicesPlaySystemSound() 系统震动是在iOS10.0之前存在...

  • 油浸式电力变压器的安装调试方案(7)

    变压器吊芯检查的条件及要求: 1、变压器经过长距离运输,会受到较大的震动,需要进行器身检查。变压器的器身检查分为吊...

  • 震动

    引入头文件: #import "AudioToolbox/AudioToolbox.h" 调用函数: AudioS...

  • 震动

    忽然被带来的坏情绪。 我不擅长处理人际关系。如果关系只是表面有裂隙,那凑合着还过得去。不过是打着马虎眼地嘻嘻哈哈就...

  • 震动

    生活总是充满了意外,让人猝不及防,很多时候,并不是努力就可以的。比如学习,努力不一定能取的高分,比如工作,拼尽全力...

  • 震动

    我怀着一个虚假的肚子,走在人间的道路上,精神状态似乎不是太好,走起来就摇摇晃晃的,上下跌跌撞撞的。 在无意之间,点...

网友评论

      本文标题:Vibraotr(震动器)的总结

      本文链接:https://www.haomeiwen.com/subject/xzftdctx.html