Android入门教程之Vibrator(振动器)

所属分类: 软件编程 / Android 阅读数: 1727
收藏 0 赞 0 分享

前言:

Vibrator简介:

 下面我们就来写个简单的例子,来熟悉下这个Vibrator的用法!

1.获得Vibrator实例:

Vibrator vb = (Vibrator)getSystemService(Service.VIBRATOR_SERVICE);

2.可以使用的相关方法:

1.stract void cancel():关闭或者停止振动器

2.tract boolean hasVibrator():判断硬件是否有振动器

3.id vibrate(long milliseconds):控制手机振动为milliseconds毫秒

4.id vibrate(long[] pattern,int repeat):指定手机以pattern指定的模式振动! 比如:pattern为new int[200,400,600,800],就是让他在200,400,600,800这个时间交替启动与关闭振动器! 而第二个则是重复次数,如果是-1的只振动一次,如果是0的话则一直振动 还有其他两个方法用得不多~ 对了,使用振动器还需要在AndroidManifest.xml中添加下述权限: <uses-permission android:name="android.permission.VIBRATE"/>

3.使用示例:设置频率不同的震动器:

对于Vibrator用的最广泛的莫过于所谓的手机按摩器类的app,在app市场一搜,一堆,笔者随便下了 几个下来瞅瞅,都是大同小异的,这点小玩意竟然有8W多的下载量...好吧,好像也不算多, 不过普遍功能都是切换振动频率来完成,而所谓的按摩效果,是否真的有效就不得而知了, 那么接下来我们就来实现一个简单的按摩器吧! 核心其实就是:vibrate()中的数组的参数,根据自己需求写一个数组就可以了! 下述代码需要在真机上进行测试!

运行效果图:

实现代码:

简单的布局文件,五个按钮:activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  
  <Button
    android:id="@+id/btn_hasVibrator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="判断是否有振动器" />


  <Button
    android:id="@+id/btn_short"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="短振动" />

  <Button
    android:id="@+id/btn_long"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="长振动" />

  <Button
    android:id="@+id/btn_rhythm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="节奏振动" />

  <Button
    android:id="@+id/btn_cancle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="取消振动" />
</LinearLayout>

接着是MainActivity.java部分:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

  private Button btn_hasVibrator;
  private Button btn_short;
  private Button btn_long;
  private Button btn_rhythm;
  private Button btn_cancle;
  private Vibrator myVibrator;
  private Context mContext;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //获得系统的Vibrator实例:
    myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
    mContext = MainActivity.this;
    bindViews();
  }

  private void bindViews() {
    btn_hasVibrator = (Button) findViewById(R.id.btn_hasVibrator);
    btn_short = (Button) findViewById(R.id.btn_short);
    btn_long = (Button) findViewById(R.id.btn_long);
    btn_rhythm = (Button) findViewById(R.id.btn_rhythm);
    btn_cancle = (Button) findViewById(R.id.btn_cancle);

    btn_hasVibrator.setOnClickListener(this);
    btn_short.setOnClickListener(this);
    btn_long.setOnClickListener(this);
    btn_rhythm.setOnClickListener(this);
    btn_cancle.setOnClickListener(this);
  }


  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.btn_hasVibrator:
        Toast.makeText(mContext, myVibrator.hasVibrator() ? "当前设备有振动器" : "当前设备无振动器",
            Toast.LENGTH_SHORT).show();
        break;
      case R.id.btn_short:
        myVibrator.cancel();
        myVibrator.vibrate(new long[]{100, 200, 100, 200}, 0);
        Toast.makeText(mContext, "短振动", Toast.LENGTH_SHORT).show();
        break;
      case R.id.btn_long:
        myVibrator.cancel();
        myVibrator.vibrate(new long[]{100, 100, 100, 1000}, 0);
        Toast.makeText(mContext, "长振动", Toast.LENGTH_SHORT).show();
        break;
      case R.id.btn_rhythm:
        myVibrator.cancel();
        myVibrator.vibrate(new long[]{500, 100, 500, 100, 500, 100}, 0);
        Toast.makeText(mContext, "节奏振动", Toast.LENGTH_SHORT).show();
        break;
      case R.id.btn_cancle:
        myVibrator.cancel();
        Toast.makeText(mContext, "取消振动", Toast.LENGTH_SHORT).show();
    }
  }
}

对了,别漏了振动器权限哦!

<uses-permission android:name="android.permission.VIBRATE"/>

小结:

好了,本文我们学习了Vibrator(振动器)的基本使用,代码非常简单,还不赶紧加入到 你的APP中,让你的应用HI起来~

更多精彩内容其他人还在看

Android设计登录界面、找回密码、注册功能

这篇文章主要为大家详细介绍了Android设计登录界面的方法,Android实现找回密码、注册功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android通过手势实现答题器翻页效果

这篇文章主要为大家详细介绍了Android通过手势实现答题器翻页效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android采用双缓冲技术实现画板

这篇文章主要为大家详细介绍了Android采用双缓冲技术实现画板的相关资料,思路清晰,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发之毛玻璃效果实例代码

这篇文章主要给大家分享android开发之毛玻璃效果的实例代码,非常具有参考借鉴价值,感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

Android实现桌面悬浮窗、蒙板效果实例代码

这篇文章主要介绍了Android实现桌面悬浮窗、蒙板效果实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

深入解读Android的Volley库的功能结构

这篇文章主要介绍了Android的Volley开发框架的功能结构,Volley是Android开发中网络部分的一大利器,包含很多HTTP协议通信的相关操作,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发中使用Volley库发送HTTP请求的实例教程

这篇文章主要介绍了Android开发中使用Volley库发送HTTP请求的实例教程,包括创建Volley单例的基本知识与取消Request请求的技巧等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿QQ聊天撒花特效 很真实

本文写的这个特效,是关于聊天的,你肯定遇到过,就是你跟人家聊天的时候,比如发送应(么么哒),然后屏幕上全部就是表情了,今天我们就是做这个,撒花的特效,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android的HTTP操作库Volley的基本使用教程

这篇文章主要介绍了Android的HTTP操作库Volley的基本使用教程,包括JSON请求与图片加载等用法的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿水波纹流量球进度条控制器

这篇文章主要介绍了Android仿水波纹流量球进度条控制器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多