当前位置: 移动技术网 > IT编程>移动开发>Android > Android入门教程之Vibrator(振动器)

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

奥巴马就职演讲,全敏宇,金日成简介

前言:

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起来~

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网