Android手机中的震动由Vibrator实现

本文介绍如何在Android应用中设置不同的震动效果,包括短震动、长震动及节奏震动,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设置震动事件,需要知道其震动的时间长短、震动的周期等。

Android Vibrator中,震动的时间一毫秒计算(1/1000秒),所以如果设置的时间值太小,会感觉不出来。

通过调用Vibrator的vibrate(long[] pattern, int repeat)方法实现。

前一个参数为设置震动的效果的数组,第二个参数为 -1表示只震动一次,为0则震动会一直持续。

一个demo:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package  com.shao.vibrator;
 
import  android.app.Activity;
import  android.os.Bundle;
import  android.os.Vibrator;
import  android.widget.CompoundButton;
import  android.widget.Toast;
import  android.widget.CompoundButton.OnCheckedChangeListener;
import  android.widget.ToggleButton;
 
public  class  VibratorActivity  extends  Activity {
     /** Called when the activity is first created. */
 
     private  Vibrator vibrator;
     private  ToggleButton tog1;
     private  ToggleButton tog2;
     private  ToggleButton tog3;
 
     @Override
     public  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
         init();
         tog1.setOnCheckedChangeListener( new  OnCheckedChangeListener() {
 
             @Override
             public  void  onCheckedChanged(CompoundButton buttonView,
                     boolean  isChecked) {
                 // TODO Auto-generated method stub
                 if  (isChecked) {
                     // 设置震动周期
                     vibrator.vibrate( new  long [] {  1000 10 100 1000  }, - 1 );
                     showToast( "OK" );
                 else  {
                     // 取消震动
                     vibrator.cancel();
                     showToast( "CANCEL" );
                 }
             }
         });
         tog2.setOnCheckedChangeListener( new  OnCheckedChangeListener() {
 
             @Override
             public  void  onCheckedChanged(CompoundButton buttonView,
                     boolean  isChecked) {
                 // TODO Auto-generated method stub
                 if  (isChecked) {
                     // 设置震动周期
                     vibrator.vibrate( new  long [] {  100 100 100 1000  },  0 );
                     showToast( "OK" );
                 else  {
                     // 取消震动
                     vibrator.cancel();
                     showToast( "CANCEL" );
                 }
             }
         });
         tog3.setOnCheckedChangeListener( new  OnCheckedChangeListener() {
 
             @Override
             public  void  onCheckedChanged(CompoundButton buttonView,
                     boolean  isChecked) {
                 // TODO Auto-generated method stub
                 if  (isChecked) {
                     // 设置震动周期
                     vibrator.vibrate( new  long [] {  1000 50 1000 50 1000  }, 0 );
                     showToast( "OK" );
                 else  {
                     // 取消震动
                     vibrator.cancel();
                     showToast( "CANCEL" );
                 }
             }
         });
 
     }
 
     private  void  init() {
         tog1 = (ToggleButton) findViewById(R.id.tog1);
         tog2 = (ToggleButton) findViewById(R.id.tog2);
         tog3 = (ToggleButton) findViewById(R.id.tog3);
         vibrator = (Vibrator)  this .getSystemService(VIBRATOR_SERVICE);
     }
 
     private  void  showToast(String msg) {
         Toast.makeText( this , msg,  1 ).show();
     }
}

xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<? xml  version = "1.0"  encoding = "utf-8" ?>
< LinearLayout  xmlns:android = "http://schemas.android.com/apk/res/android"
     android:orientation = "vertical"
     android:layout_width = "fill_parent"
     android:layout_height = "fill_parent" >
     < RelativeLayout
         android:layout_marginTop = "20dp"
         android:orientation = "horizontal"
         android:layout_width = "fill_parent"
         android:layout_height = "wrap_content" >
         < TextView
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:text = "短震动"  />
         < ToggleButton
             android:id = "@+id/tog1"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:textOn = "关闭"  android:textOff = "打开"
             android:layout_alignParentRight = "true"  />
     </ RelativeLayout >
     < RelativeLayout
         android:layout_marginTop = "20dp"
         android:orientation = "horizontal"
         android:layout_width = "fill_parent"
         android:layout_height = "wrap_content" >
         < TextView
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:text = "长震动"  />
         < ToggleButton
             android:id = "@+id/tog2"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:textOn = "关闭"  android:textOff = "打开"
             android:layout_alignParentRight = "true"  />
     </ RelativeLayout >
     < RelativeLayout
         android:layout_marginTop = "20dp"
         android:orientation = "horizontal"
         android:layout_width = "fill_parent"
         android:layout_height = "wrap_content" >
         < TextView
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:text = "节奏震动"  />
         < ToggleButton
             android:id = "@+id/tog3"
             android:layout_width = "wrap_content"
             android:layout_height = "wrap_content"
             android:textOn = "关闭"
             android:textOff = "打开"
             android:layout_alignParentRight = "true"  />
     </ RelativeLayout >
</ LinearLayout >

Vibrator效果图

最后别忘了加上<uses-permission  android:name="android.permission.VIBRATE"/>权限。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值