歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android--Vibrator實現手機震動效果

Android--Vibrator實現手機震動效果

日期:2017/3/1 10:02:03   编辑:Linux編程

手機的震動功能相信大家都不會陌生,現在就讓我們解讀手機的震動。其實,要實現手機的震動並不難,只需要實現一個類,並調用其中的方法,設定相應的參數即可。

下面給出介紹:

這段文檔來自Google SDK文檔


Class that operates the vibrator on the device.

If your process exits, any vibration you started with will stop.

To obtain an instance of the system vibrator, call getSystemService(String) with VIBRATOR_SERVICE as argument.

我們通常實現的方法如下:

public abstract void vibrate (long[] pattern, int repeat)
Added in API level 1Vibrate 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.


下面給出一個具體的實例實現震動效果(需要在真機上運行)
1.布局文件的代碼

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:Android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 建立第一個TextView -->
<TextView
android:id="@+id/myTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<!-- 建立第二個TextView -->
<TextView
android:id="@+id/myTextView2"
android:layout_width="127px"
android:layout_height="35px"
android:layout_x="90px"
android:layout_y="33px"
android:text="@string/str_text1"
/>
<!-- 建立第三個TextView -->
<TextView
android:id="@+id/myTextView3"
android:layout_width="127px"
android:layout_height="35px"
android:layout_x="90px"
android:layout_y="115px"
android:text="@string/str_text2"
/>
<!-- 建立第四個TextView -->
<TextView
android:id="@+id/myTextView4"
android:layout_width="127px"
android:layout_height="35px"
android:layout_x="90px"
android:layout_y="216px"
android:text="@string/str_text3"
/>
<!-- 建立第一個ToggleButton -->
<ToggleButton
android:id="@+id/myTogglebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="29px"
android:layout_y="31px"
/>
<!-- 建立第二個ToggleButton -->
<ToggleButton
android:id="@+id/myTogglebutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="29px"
android:layout_y="114px"
/>
<!-- 建立第三個ToggleButton -->
<ToggleButton
android:id="@+id/myTogglebutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="29px"
android:layout_y="214px"
/>
</AbsoluteLayout>

Copyright © Linux教程網 All Rights Reserved