Android實現震動效果

來源:互聯網
上載者:User

Android實現震動效果看下面代碼

布局檔案main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.   
  12.     <Button  
  13.         android:id="@+id/btn"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="震動測試" />  
  17.   
  18. </LinearLayout>  

Activity檔案

  1. package com.suxh.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Service;  
  5. import android.content.pm.ActivityInfo;  
  6. import android.os.Bundle;  
  7. import android.os.Vibrator;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.Window;  
  11. import android.view.WindowManager;  
  12. import android.widget.Button;  
  13.   
  14. public class Activity1 extends Activity {  
  15.     private Button btn;  
  16.     private Vibrator mVibrator01;  //聲明一個震動器對象   
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         // 設定為無標題列   
  21.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  22.         // 設定為全螢幕模式   
  23.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  24.         // 設定為橫屏   
  25.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
  26.         setContentView(R.layout.main);  
  27.           
  28.         btn = (Button)findViewById(R.id.btn);  
  29.           
  30.         btn.setOnClickListener(new OnClickListener() {  
  31.             @Override  
  32.             public void onClick(View v) {  
  33.                 mVibrator01 = ( Vibrator ) getApplication().getSystemService(Service.VIBRATOR_SERVICE);  
  34.                 mVibrator01.vibrate( new long[]{100,10,100,1000},-1);  
  35.             }  
  36.         });  
  37.   
  38.     }  
  39. }  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.