Android實現(xiàn)簡單手機震動效果
手機開發(fā)中,有時候我們需要使用震動效果提示用戶當前的軟件狀態(tài),下面以一個簡單的例子實現(xiàn)這個功能。
1.新建Android應用程序
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表示不重復, 如果不是-1, 比如改成1, 表示從前面這個long數(shù)組的下標為1的元素開始重復. SystemClock.sleep(10000);//震動10s以后停止 vibrator.cancel(); break; default: break; } } }; }
5.運行效果
源碼下載:Android手機震動效果
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android 對話框彈出位置和透明度的設置具體實現(xiàn)方法
在android中我們經(jīng)常會用AlertDialog來顯示對話框。通過這個對話框是顯示在屏幕中心的。但在某些程序中,要求對話框可以顯示在不同的位置。2013-07-07android使用ExpandableListView控件實現(xiàn)小說目錄效果的例子
這篇文章主要介紹了android使用ExpandableListView控件實現(xiàn)小說目錄效果的例子,還可以實現(xiàn)二級列表展示效果,需要的朋友可以參考下2014-07-07Android ListView的OnItemClickListener詳解
這篇文章主要介紹了Android ListView的OnItemClickListener詳解的相關(guān)資料,涉及到OnItemClickListener的position和id參數(shù)做詳細的解釋的知識點,非常不錯,具有參考借鑒價值,需要的朋友參考下2016-07-07Android內(nèi)存泄漏排查利器LeakCanary
這篇文章主要為大家詳細介紹了Android內(nèi)存泄漏排查利器LeakCanary的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03Android自定義布局實現(xiàn)仿qq側(cè)滑部分代碼
這篇文章主要為大家詳細介紹了自定義布局實現(xiàn)仿qq側(cè)滑Android部分代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03android實現(xiàn)圖片閃爍動畫效果的兩種實現(xiàn)方式(實用性高)
本文通過兩種方法給大家講解了android實現(xiàn)圖片閃爍動畫效果,實用性非常高,對這兩種方法感興趣的朋友一起通過本文學習吧2016-09-09