Android 使用Vibrator服務(wù)實(shí)現(xiàn)點(diǎn)擊按鈕帶有震動(dòng)效果
Vibrator
振動(dòng)器,是手機(jī)自帶的振動(dòng)器哦,不要想成島國(guó)用的那種神秘東西哦~~Vibrator是Android給我們提供的用于機(jī)身震動(dòng)的一個(gè)服務(wù)哦 更多詳情可見(jiàn)官方API文檔:Vibrator
如何使用?
首先添加震動(dòng)權(quán)限:
<uses-permission android:name='android.permission.VIBRATE' />
獲得Vibrator實(shí)例:
Vibrator mVibrator= (Vibrator) getSystemService(VIBRATOR_SERVICE);
點(diǎn)擊按鈕,震動(dòng)開(kāi)啟
mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //震動(dòng)30毫秒 mVibrator.vibrate(30); //todo } });
Vibrator相關(guān)方法:
//關(guān)閉或者停止振動(dòng)器 cancel() //判斷硬件是否有振動(dòng)器 hasVibrator() //控制手機(jī)振動(dòng)為milliseconds毫秒 vibrate(long milliseconds) /* * 指定手機(jī)以pattern指定的模式振動(dòng) * 比如:pattern為new int[200,400,600,800],就是讓他在200,400,600,800這個(gè)時(shí)間交替啟動(dòng)與關(guān)閉振動(dòng)器 * repeat是重復(fù)次數(shù),如果是-1的只振動(dòng)一次,如果是0的話則一直振動(dòng) */ vibrate(long[] pattern,int repeat)
舉例子:
//短振動(dòng) mVibrator.vibrate(new long[]{100, 200, 100, 200}, 0); //長(zhǎng)振動(dòng) mVibrator.vibrate(new long[]{100, 100, 100, 1000}, 0); //節(jié)奏振動(dòng) mVibrator.vibrate(new long[]{500, 100, 500, 100, 500, 100}, 0); //取消振動(dòng) mVibrator.cancel();
參考文章: Vibrator(振動(dòng)器)
總結(jié)
到此這篇關(guān)于Android 使用Vibrator服務(wù)實(shí)現(xiàn)點(diǎn)擊按鈕帶有震動(dòng)效果的文章就介紹到這了,更多相關(guān)android點(diǎn)擊按鈕震動(dòng)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. asp中response.write("中文")或者js中文亂碼問(wèn)題2. 詳解CSS偽元素的妙用單標(biāo)簽之美3. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)4. 詳解JS前端使用迭代器和生成器原理及示例5. XML入門的常見(jiàn)問(wèn)題(四)6. html小技巧之td,div標(biāo)簽里內(nèi)容不換行7. 使用css實(shí)現(xiàn)全兼容tooltip提示框8. ASP編碼必備的8條原則9. ASP中格式化時(shí)間短日期補(bǔ)0變兩位長(zhǎng)日期的方法10. php bugs代碼審計(jì)基礎(chǔ)詳解
