Android--Vibrator实现手机震动效果

手机的震动功能相信大家都不会陌生,现在就让我们解读手机的震动。

其实,要实现手机的震动并不难,只需要实现一个类,并调用其中的方法,设定相应的参数即可。

下面给出介绍:

这段文档来自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 voidvibrate(long[] pattern, int repeat)
Added in API level 1

Vibrate 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 permissionVIBRATE.

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>

2.主程序的代码
public class EX05_06 extends Activity 
{ 
  private Vibrator mVibrator01;
  /** Called when the activity is first created. */ 
  @Override public void onCreate(Bundle savedInstanceState) 
  { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    /*设定ToggleButton的对象*/
    mVibrator01 = ( Vibrator )getApplication().getSystemService( Service .VIBRATOR_SERVICE ); 
    final ToggleButton mtogglebutton1 = (ToggleButton) findViewById(R.id .myTogglebutton1); 
    final ToggleButton mtogglebutton2 = (ToggleButton) findViewById(R.id .myTogglebutton2); 
    final ToggleButton mtogglebutton3 = (ToggleButton) findViewById(R.id .myTogglebutton3); 
    /*设定ToggleButton使用OnClickListener来启动事件*/
    mtogglebutton1.setOnClickListener(new OnClickListener()
    { 
      
      public void onClick(View v) 
      { 
        if (mtogglebutton1.isChecked()) 
        { 
          
//          public abstract void vibrate (long[] pattern, int repeat) 
//          Added in API level 1
//          Vibrate 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.  

          /*设定震动的周期*/
          mVibrator01.vibrate( new long[]{100,10,100,1000},-1); 
          /*用Toast显示震动启动*/ 
          Toast.makeText(EX05_06.this, getString(R.string.str_ok) ,Toast.LENGTH_SHORT).show();
          }
        else
        { 
          /*取消震动*/ mVibrator01.cancel(); 
          /*用Toast显示震动取消*/ 
          Toast.makeText(EX05_06.this, getString(R.string.str_end) ,Toast.LENGTH_SHORT).show();
          } 
        } 
      });
    /*设定ToggleButton使用OnClickListener来启动事件*/ 
    mtogglebutton2.setOnClickListener(new OnClickListener()
    { 
      public void onClick(View v) 
      { 
        if (mtogglebutton2.isChecked())
        {
/*设定震动的周期*/ 
          mVibrator01.vibrate( new long[]{100,100,100,1000},0);
/*用Toast显示震动启动*/
Toast.makeText(EX05_06.this, getString(R.string.str_ok) ,Toast.LENGTH_SHORT).show(); 
}
        else
        { 
          /*取消震动*/
          mVibrator01.cancel(); 
          /*用Toast显示震动取消*/ 
          Toast.makeText(EX05_06.this, getString(R.string.str_end) ,Toast.LENGTH_SHORT).show();
          }
        } 
      });
    mtogglebutton3.setOnClickListener(new OnClickListener()
    { 
      public void onClick(View v) 
      { 
        if (mtogglebutton3.isChecked()) 
        {
          /*设定震动的周期*/
          mVibrator01.vibrate( new long[]{1000,50,1000,50,1000},0); 
          /*用Toast显示震动启动*/
          Toast.makeText(EX05_06.this, getString(R.string.str_ok) ,Toast.LENGTH_SHORT).show();
          }
        else
        { 
          /*取消震动*/ 
          mVibrator01.cancel(); 
          /*用Toast显示震动取消*/
          Toast.makeText(EX05_06.this, getString(R.string.str_end) ,Toast.LENGTH_SHORT).show();
          }
        } 
      }); 
    } 
  }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值