
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,d出一个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(震动反馈)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的老生常谈Android HapticFeedback(震动反馈)全部内容,希望文章能够帮你解决老生常谈Android HapticFeedback(震动反馈)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)