android如音符般的震动

android如音符的震动

概要

近来在做一个全屏录音的炫酷view时,感觉开始录音时太平淡,所以准备加入一个震动的效果,去网上搜了一下,例子还是挺多,但自己真正开始做的时候发现了许多细节方面的网上的文章都没有说清楚,很多人也可能只是复制粘贴,没有自己去尝试过,在经过一系列探索以后,把使用的心得记录一下。

官网介绍

首先我们来看看官网对于震动的介绍Vibrator.

类概述

这个类是操作设备上的震动的一个类。如果你的进程退出,任何你启动的震动都会停止。如果你想要获得一个系统的震动实例,可以调用%29”>getSystemService这个函数,使用VIBRATOR_SERVICE作为参数。

方法概述

abstract void   cancel() //关闭震动
Turn the vibrator off.

abstract boolean    hasVibrator()  //检查设备是否支持震动
Check whether the hardware has a vibrator.

void    vibrate(long[] pattern, int repeat, AudioAttributes attributes) //一下均是通过参数来构建震动
Vibrate with a given pattern.

void    vibrate(long[] pattern, int repeat) //震动参数,从哪震动参数数组的下标开始重复震动
Vibrate with a given pattern.

void    vibrate(long milliseconds)  //震动一段时间
Vibrate constantly for the specified period of time.

void    vibrate(long milliseconds, AudioAttributes attributes) 
Vibrate constantly for the specified period of time.

具体的意思在官网中的函数解释很明确了,这里挑一段分析一下:

public void vibrate (long[] pattern, int repeat)

    Added in API level 1
    Vibrate with a given pattern.

    Pass in an array of ints that are the durations for which to turn on or off the vibrator in milliseconds. The first value indicates the number of milliseconds to wait before turning the vibrator on. The next value indicates the number of milliseconds for which to keep the vibrator on before turning it off. Subsequent values alternate between durations in milliseconds to turn the vibrator off or to turn the vibrator on.

    To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.

    This method requires the caller to hold the permission VIBRATE.

    Parameters
    pattern an array of longs of times for which to turn the vibrator on or off.
    repeat  the index into pattern at which to repeat, or -1 if you don't want to repeat.

这里的long[] pattern说明这是一个数组,其中的数字个数可以写很多,比如说我定义了一个long[] pattern = {100,200,100,500}。这里我定义了有四个数字的数组,从官网解释中可知:

  • 第一个参数表明手机震动电机关闭的毫秒数
  • 第二个参数表明手机震动点击开启的毫秒数
  • 后面的参数按照第一第二个参数依次类推,即第三个是关,第四个是开

  • repeat参数一开始很容易让人误以为是重复的次数,其实这是不对的,从解释中我们可以看到官网是这样解释的这个repeat参数是在pattern数组中的一个下标索引,也就是说如果我定义repeat为2的话,以后就一直从pattern[3]开始到最后进行循环播放,就会一直进行100,500,100,500,100,500·····

所以我要仔细看官方文档具体怎么解释比较好,既然我们掌握了震动的规律,这样我们就可以让震动变得如音符那样具有节奏感了。这时,我们可以找一首曲子的乐谱过来,可以看到五线谱中都有节拍,每个节拍都可以换算成具体的毫秒数,具体的涉及乐理的知识就不在这里赘述了,比如我们需要定义一首曲子的话就可以在规则数组中定义乐曲了哈哈。

代码实现

单独使用震动还是很简单的,也不需要获取什么NotificationManager等,直接去系统服务获取震动的实例就行,下面就贴一下简单的实现代码。

/**
     * 添加录音震动
     * VIBRATE P1: the number of milliseconds to wait before turning the vibrator on
     *         P2: the number of milliseconds for which to keep the vibrator on before turning it off
     *         P+: alternate between durations in milliseconds to turn the vibrator off or to turn the vibrator on
     *
     *         To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating
     */
    private void sendYibrateNotification(){
        long[] pattern = {0,500};
        Vibrator vibrate = (Vibrator) mContext.getSystemService(Service.VIBRATOR_SERVICE);
        vibrate.vibrate(pattern,-1);
    }

这边我实现的效果就是不重复的简单震动500毫秒
最后不要忘了加入相关权限

<uses-permission android:name="android.permission.VIBRATE" />

总结

android的很多设备都是很开放的,开发者可以自己去定义自己的想要达到的效果,这也是android系统的迷人之处。最后附上一张刚做完的自定义的全屏录音控件,因为在模拟器中截的屏且用的是台式机,没有声音输入设备,所以声音没法录入,录音时的音波条一直是平的,实习效果很炫酷,其中的难点就是在使用时要针对于不同的view的拦截事件进行处理,比如在listview中。如果喜欢这样的效果欢迎索要。
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值