设为首页 加入收藏

TOP

Android中实现振动效果 (How to Imeplement Vibration Effect In Andoid)
2014-11-24 12:29:31 来源: 作者: 【 】 浏览:1
Tags:Android 实现 振动 效果 How Imeplement Vibration Effect Andoid

vibrate(long[] pattern, int repeat)按照给定的模式振动。


Patterns指定振动模式,数组的每个整数是一个时间间隔,第一个整数指定等待多长时间开始振动,后面的参数依次重复指定振动持续的时间和振动间隔的时间。


Repeat指定patter数组中的一个索引值,从这个值开始不断重复振动模式,如果为-1则不重复振动。


cancel()取消振动。


下面是不同振动效果的例子(来自参考资料[2]),这些代码需要在一个Context(如Activity, Service)中进行调用。注意默认情况下当屏幕暗掉时,振动也会停止,可以通过设置屏幕常亮来使振动持续。


1.1 振动指定的时间
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);


// Vibrate for 200 milliseconds


v.vibrate(300);


1.2 按照指定的模式进行振动
按照”S-O-S”求救信号模式进行振动:


Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);




// This example will cause the phone to vibrate "SOS" in Morse Code



// In Morse Code, "s" = "dot-dot-dot", "o" = "dash-dash-dash"


// There are pauses to separate dots/dashes, letters, and words



// The following numbers represent millisecond lengths


int dot = 200; // Length of a Morse Code "dot" in milliseconds



int dash = 500; // Length of a Morse Code "dash" in milliseconds


int short_gap = 200; // Length of Gap Between dots/dashes



int medium_gap = 500; // Length of Gap Between Letters


int long_gap = 1000; // Length of Gap Between Words



long[] pattern = {


0, // Start immediately



dot, short_gap, dot, short_gap, dot, // s


medium_gap,



dash, short_gap, dash, short_gap, dash, // o


medium_gap,



dot, short_gap, dot, short_gap, dot, // s


long_gap



};



// Only perform this pattern one time (-1 means "do not repeat")


v.vibrate(pattern, -1);


1.3 重复振动直到用户取消
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);



// Start immediately



// Vibrate for 200 milliseconds


// Sleep for 500 milliseconds



long[] pattern = { 0, 200, 500 };



// The "0" means to repeat the pattern starting at the beginning


// CUIDADO: If you start at the wrong index (e.g., 1) then your pattern will be off --



// You will vibrate for your pause times and pause for your vibrate times !


v.vibrate(pattern, 0);


In another part of your code, you can handle turning off the vibrator as shown below:


view source



print


// Stop the Vibrator in the middle of whatever it is doing


// CUIDADO: Do *not* do this immediately after calling .vibrate().



// Otherwise, it may not have time to even begin vibrating!


v.cancel();


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇iOS如何隐藏TabBar? 下一篇iOS开发中nil与release

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·CPython是什么?PyPy (2025-12-26 06:50:09)
·Python|如何安装seab (2025-12-26 06:50:06)
·python要学习数据分 (2025-12-26 06:50:03)
·每日一道面试题-多线 (2025-12-26 06:20:17)
·java项目中哪些地方 (2025-12-26 06:20:14)