Android實(shí)現(xiàn)簡單手機(jī)震動效果
手機(jī)開發(fā)中,有時候我們需要使用震動效果提示用戶當(dāng)前的軟件狀態(tài),下面以一個簡單的例子實(shí)現(xiàn)這個功能。
1.新建Android應(yīng)用程序

2.在AndroidManifest.xml中申明需要使用的震動權(quán)限
<uses-permission android:name="android.permission.VIBRATE" />
3.界面上添加按鈕
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="震動1"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="震動2"/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="震動3"/>
</LinearLayout>
4.源碼中處理按鈕效果
package com.sl.vibratordemo;
import android.os.Bundle;
import android.os.SystemClock;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Context;
public class MainActivity extends Activity
{
private Button btn1 = null;
private Button btn2 = null;
private Button btn3 = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
btn3 = (Button)findViewById(R.id.btn3);
btn1.setOnClickListener(listener);
btn2.setOnClickListener(listener);
btn3.setOnClickListener(listener);
}
OnClickListener listener = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
switch (v.getId())
{
case R.id.btn1:
vibrator.vibrate(1000);//震動1秒
break;
case R.id.btn2:
long[] pattern1 = {1000, 2000, 1000, 3000}; //等待1秒,震動2秒,等待1秒,震動3秒
vibrator.vibrate(pattern1, -1);
break;
case R.id.btn3:
long[] pattern2 = {1000, 500, 1000, 1000};
vibrator.vibrate(pattern2, 2);//-1表示不重復(fù), 如果不是-1, 比如改成1, 表示從前面這個long數(shù)組的下標(biāo)為1的元素開始重復(fù).
SystemClock.sleep(10000);//震動10s以后停止
vibrator.cancel();
break;
default:
break;
}
}
};
}
5.運(yùn)行效果

源碼下載:Android手機(jī)震動效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android基于AdapterViewFlipper實(shí)現(xiàn)的圖片/文字輪播動畫控件
這篇文章主要介紹了Android基于AdapterViewFlipper實(shí)現(xiàn)的圖片/文字輪播動畫控件,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-04-04
Android實(shí)現(xiàn)3D標(biāo)簽云效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)3D標(biāo)簽云效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
21天學(xué)習(xí)android開發(fā)教程之SurfaceView
21天學(xué)習(xí)android開發(fā)教程之SurfaceView,SurfaceView由于可以直接從內(nèi)存或者DMA等硬件接口取得圖像數(shù)據(jù),因此是個非常重要的繪圖容器,操作相對簡單,感興趣的小伙伴們可以參考一下2016-02-02
Android 懸浮窗權(quán)限各機(jī)型各系統(tǒng)適配大全(總結(jié))
這篇文章主要介紹了Android 懸浮窗權(quán)限各機(jī)型各系統(tǒng)適配大全(總結(jié)),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android編程實(shí)現(xiàn)3D滑動旋轉(zhuǎn)效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)3D滑動旋轉(zhuǎn)效果的方法,主要通過繼承Animation自定義Rotate3D來實(shí)現(xiàn)3D翻頁效果,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
Android App中使用Gallery制作幻燈片播放效果
這篇文章主要介紹了Android App中使用Gallery制作幻燈片播放效果,相冊應(yīng)用中的輪播功能也與本文中例子的原理類似,需要的朋友可以參考下2016-04-04

