当前位置: 移动技术网 > IT编程>移动开发>Android > 老生常谈Android HapticFeedback(震动反馈)

老生常谈Android HapticFeedback(震动反馈)

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

恩啊好硬再深一点,钢铁中转站,mp3歌曲免费下载

android中长按一个控件的时候,想以震动提示用户,除了用vibrate类来做,还可以用到(hapticfeedback)震动反馈实现。

本篇文章,我们就一起来熟悉一下android震动反馈,首先我们打开手机上的振动模式开光,这里我是以小米手机来做模拟的,位置在设置—>声音和震动—>触摸时震动,如下图所示:

震动强度,我选择了较强,以让震动更明显。

系统触发震动

下面从一个例子,来开始本篇博客,对一个button注册长按监听:

 

button click= (button) findviewbyid(r.id.click);
  click.setonlongclicklistener(new view.onlongclicklistener() {
   @override
   public boolean onlongclick(view v) {

    toast.maketext(mainactivity.this,"长按点击",toast.length_short).show();

    //触发震动反馈
    return true;
    //return false;
   }
  });

当你长按此button,弹出一个toast,并且震动了,但是,返回false并不会触发震动。

现在看源码分析一下,这是为何。

button实现setonlongclicklistener方法,在父类textview的父类view中,

view.setonlongclicklistener源码:

/**
  * register a callback to be invoked when this view is clicked and held. if this view is not
  * long clickable, it becomes long clickable.
  *
  * @param l the callback that will run
  *
  * @see #setlongclickable(boolean)
  */
 public void setonlongclicklistener(@nullable onlongclicklistener l) {
  if (!islongclickable()) {
   setlongclickable(true);
  }
  getlistenerinfo().monlongclicklistener = l;
 }

我们要看monlongclicklistener是在哪里调用的接口onlongclick方法,最终在view的源码中找到

view.performlongclick源码:

/**
  * call this view's onlongclicklistener, if it is defined. invokes the context menu if the
  * onlongclicklistener did not consume the event.
  *
  * @return true if one of the above receivers consumed the event, false otherwise.
  */
 public boolean performlongclick() {
  sendaccessibilityevent(accessibilityevent.type_view_long_clicked);

  boolean handled = false;
  listenerinfo li = mlistenerinfo;
  if (li != null && li.monlongclicklistener != null) {
   handled = li.monlongclicklistener.onlongclick(view.this);
  }
  if (!handled) {
   handled = showcontextmenu();
  }
  if (handled) {
   performhapticfeedback(hapticfeedbackconstants.long_press);
  }
  return handled;
 }

可以看到

第13行执行了onlongclick方法,并且将返回值给了变量handled,

在第18行,hangdled为true,执行performhapticfeedback(hapticfeedbackconstants.long_press);该方法最终触发了震动反馈。

这就是为什么,onlongclick返回true的时候,才会有震动效果。

自定义触发震动

上节提到,在performhapticfeedback触发震动,观察源码得知,用户可以自己通过代码来触发。

如下文所示,点击也会触发震动反馈了:

  click.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    v.performhapticfeedback(hapticfeedbackconstants.long_press);

   }
  });

现在我们就去performhapticfeedback源码看下,都执行了什么,

view.performhapticfeedback源码:

/**
  * bzzztt!!1!
  *
  * <p>provide haptic feedback to the user for this view.
  *
  * <p>the framework will provide haptic feedback for some built in actions,
  * such as long presses, but you may wish to provide feedback for your
  * own widget.
  *
  * <p>the feedback will only be performed if
  * {@link #ishapticfeedbackenabled()} is true.
  *
  * @param feedbackconstant one of the constants defined in
  * {@link hapticfeedbackconstants}
  */
 public boolean performhapticfeedback(int feedbackconstant) {
  return performhapticfeedback(feedbackconstant, 0);
 }

这里解释三个知识点:

1、只有在ishapticfeedbackenabled()为true的情况下,才会触发震动。之后会解释在为false的情况下,为何不会触发震动。

在xml里,可以通过android:hapticfeedbackenabled=”false|true”来进行设置

在java代码里,可以通过view.sethapticfeedbackenabled(boolean)来设置,

不过默认是true哦。

2、hapticfeedbackconstants的常量值,我们要用到的有三个,一个是long_press(长按),第二个是flag_ignore_view_setting(不受view的设置影响,即不受ishapticfeedbackenabled()的影响),第三个是flag_ignore_global_setting(不受系统设置的影响,即不受是否开启震动反馈的影响)

3、我们看到该方法最终是返回的performhapticfeedback(int feedbackconstant, int flags)这个方法,

view.performhapticfeedback(int feedbackconstant, int flags)源码:

/**
  * bzzztt!!1!
  *
  * <p>like {@link #performhapticfeedback(int)}, with additional options.
  *
  * @param feedbackconstant one of the constants defined in
  * {@link hapticfeedbackconstants}
  * @param flags additional flags as per {@link hapticfeedbackconstants}.
  */
 public boolean performhapticfeedback(int feedbackconstant, int flags) {
  if (mattachinfo == null) {
   return false;
  }
  //noinspection simplifiableifstatement
  if ((flags & hapticfeedbackconstants.flag_ignore_view_setting) == 0
    && !ishapticfeedbackenabled()) {
   return false;
  }
  return mattachinfo.mrootcallbacks.performhapticfeedback(feedbackconstant,
    (flags & hapticfeedbackconstants.flag_ignore_global_setting) != 0);
 }

看第15行的if语句,当flags=0时,flags & hapticfeedbackconstants.flag_ignore_view_setting为0,又ishapticfeedbackenabled()为false,整个条件为真,所以会执行17行,直接return。这也是为什么performhapticfeedback(int feedbackconstant)方法一定要在ishapticfeedbackenabled()为ture的情况下才会触发震动。

在这里说一下,&是按位与,返回数值,&&逻辑与,返回布尔值。

第19-20行,就是触发底层震动的代码了,之后代码不做分析。

hapticfeedbackconstants常量

接下来,看下hapticfeedbackconstants三个常量,还是之前的代码,如下所示:

click.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    v.performhapticfeedback(hapticfeedbackconstants.long_press
      );
   }
  });

在单击后,会触发震动,但是如果xml加上 android:hapticfeedbackenabled=”false”这句话,单击则没有震动效果了。如下所示:

<button
  android:layout_width="wrap_content"
  android:id="@+id/click"
  android:layout_height="wrap_content"
  android:hapticfeedbackenabled="false"
  android:text="make" />

如果这时,想让其震动,可以用如下方法来做:

 click.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    v.performhapticfeedback(hapticfeedbackconstants.long_press,hapticfeedbackconstants.flag_ignore_view_setting
      );
   }
  });

忽略view的属性设置。

还记得本篇文章之前,说去设置里打开触摸时震动的开关吗,其实,用户不打开,照样可以让其震动,只需要用如下的方法:

 click.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    v.performhapticfeedback(hapticfeedbackconstants.long_press,hapticfeedbackconstants.flag_ignore_global_setting
      );
   }
  });

忽略系统设置,哈哈,是不是很变态的方法,不过不建议这样做,毕竟用户禁止了触摸反馈,我们就没必要继续挑战用户极限了。

最后,我还要说一点,就是以上的方法,不需要震动权限,不需要震动权限,不需要震动权限、重要的事情说三遍。

以上这篇老生常谈android hapticfeedback(震动反馈)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

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

相关文章:

验证码:
移动技术网