新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android震動(dòng)代碼解讀
大家可能還記得我們以前介紹的有關(guān)在游戲開發(fā)中需要使用Android重力感應(yīng)的文章。相信大家可能對Android游戲開發(fā)應(yīng)該有一些了解。在這里我們將會為大家?guī)碛嘘P(guān)Android震動(dòng)的實(shí)現(xiàn),這一功能同樣在游戲中應(yīng)用廣泛。

正在開發(fā)第二個(gè)游戲,計(jì)時(shí)就要結(jié)束的時(shí)候,為了營造緊張的氣氛,會利用手機(jī)自身的震動(dòng)模擬心跳效果,其實(shí)這個(gè)心跳效果做起來真的非常的簡單。所以直接上代碼了(注意模擬器是模擬不了震動(dòng)的,得真機(jī)測試哦):
Android震動(dòng)實(shí)現(xiàn)代碼:
- package com.ray.test;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Vibrator;
- import android.view.MotionEvent;
- public class TestViberation extends Activity {
- Vibrator vibrator;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- @Override
- protected void onStop() {
- if(null!=vibrator){
- vibrator.cancel();
- }
- super.onStop();
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- if(event.getAction() == MotionEvent.ACTION_DOWN){
- vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
- long[] pattern = {800, 50, 400, 30}; // OFF/ON/OFF/ON...
- vibrator.vibrate(pattern, 2);
//-1不重復(fù),非-1為從pattern的指定下標(biāo)開始重復(fù)- }
- return super.onTouchEvent(event);
- }
- }
Android震動(dòng)具體實(shí)現(xiàn)方法就為大家介紹到這里。
【編輯推薦】
- Android移植實(shí)際應(yīng)用要點(diǎn)解析
- Android啟動(dòng)Java程序應(yīng)用方法詳解
- Android使用Animation技巧講解
- Android logcat應(yīng)用指南
- Android界面布局基本知識簡述
網(wǎng)站標(biāo)題:Android震動(dòng)代碼解讀
標(biāo)題鏈接:http://fisionsoft.com.cn/article/cohdpdj.html


咨詢
建站咨詢
