java Android:触觉反馈:视图中的 onClick() 事件与 hapticFeedbackEnabled

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2830044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:03:25  来源:igfitidea点击:

Android: Haptic feedback: onClick() event vs hapticFeedbackEnabled in the view

javaandroid

提问by dreeves

If you want a button to provide haptic feedback (ie, the phone vibrates very briefly so you can feel that you really pushed the button), what's the standard way to do that? It seems you can either explicitly set an onClick() event and call the vibrate() function, giving a number of milliseconds to vibrate, or you can set hapticFeedbackEnabled in the view.

如果您想要一个按钮来提供触觉反馈(即,手机非常短暂地振动,以便您能感觉到您真的按下了按钮),那么标准的做法是什么?似乎您可以显式设置 onClick() 事件并调用 vibrate() 函数,给出若干毫秒的振动时间,或者您可以在视图中设置 hapticFeedbackEnabled。

The documentation seems to indicate that the latter only works for long-presses or virtual on-screen keys: http://developer.android.com/reference/android/view/View.html#performHapticFeedback(int)

该文档似乎表明后者仅适用于长按或虚拟屏幕键:http: //developer.android.com/reference/android/view/View.html#performHapticFeedback(int)

If that's right, then I need to either make my button a virtual on-screen key or manually set the onClick() event.

如果这是对的,那么我需要将我的按钮设为虚拟屏幕键或手动设置 onClick() 事件。

What do you recommend?

你有什么建议吗?

Also, if I want the vibrating to happen immediately when the user's finger touches the button, as opposed to when their finger "releases" the button, what's the best way to accomplish that?

另外,如果我希望在用户的手指触摸按钮时立即发生振动,而不是当他们的手指“释放”按钮时,那么最好的方法是什么?

Related question: How to enable haptic feedback on button view

相关问题: 如何在按钮视图上启用触觉反馈

回答by Meier

There is a longer description in the Android Cookbook - Chapter Haptic Feedback

Android Cookbook - Chapter Haptic Feedback中有更长的描述

Abstract: there are several step needed:

摘要:需要几个步骤:

  • enable vibration in manifest
  • enable haptic feedback for the button/view
  • register an onTouch Listener for your button
  • call performHapticFeedback()on Touch down
  • 在清单中启用振动
  • 为按钮/视图启用触觉反馈
  • 为您的按钮注册一个 onTouch 侦听器
  • 呼叫performHapticFeedback()触地

You can have both a onTouchLister for the haptic feedback and a onClickHandler for your action, just make sure the the onTouchHandler returns false, otherwise the event is marked as consumed and not given to the onClickHandler.

您可以同时拥有用于触觉反馈的 onTouchLister 和用于您的操作的 onClickHandler,只需确保 onTouchHandler 返回 false,否则该事件将被标记为已消费且不会提供给 onClickHandler。

回答by Edward Falk

I use vibrate() in my calculator app, but that was only because the system had no haptic feedback options when I wrote it. I don't recommend that approach for a number of reasons. In particular, every phone has different characteristics (mass, vibrator motor, motor position, etc.) so what "feels" right on one phone won't be right on another.

我在我的计算器应用程序中使用了 vibrate(),但这只是因为在我编写它时系统没有触觉反馈选项。出于多种原因,我不推荐这种方法。特别是,每部手机都有不同的特性(质量、振动电机、电机位置等),因此在一部手机上“感觉”正确的内容在另一部手机上并不正确。

However, it's fair to assume that the individual phone manufacturers have done at least a little "tuning" on their particular products, so the built-in haptic feedback has a better chance of feeling right across the board.

但是,可以假设个别手机制造商至少对其特定产品进行了一些“调整”,因此内置的触觉反馈更有可能全面感受。

Additionally, as pointed out above, if you implement your own haptic feedback, you'll need to implement your own setting for it in your preferences. Far better to let the user set it on a system-wide basis.

此外,如上所述,如果您实施自己的触觉反馈,则需要在您的偏好中实施自己的设置。让用户在系统范围的基础上设置它要好得多。

Time permitting (hah!) I plan to switch from using vibrate() to using the system's built-in haptic feedback.

如果时间允许(哈!)我计划从使用 vibrate() 切换到使用系统的内置触觉反馈。

回答by MounirReg

I think that calling vibrate() manually is not a good idea. It would mean someone who doesn't want haptic feedback will have his phone vibrating. Plus I don't know how it reacts on non-vibrating products. setHapticFeedbackEnabled is definitely the good way to do it; the method was created for this very specific purpopse.

我认为手动调用 vibrate() 不是一个好主意。这意味着不想要触觉反馈的人会让他的手机振动。另外我不知道它对非振动产品的反应如何。setHapticFeedbackEnabled 绝对是做到这一点的好方法;该方法是为这个非常具体的目的而创建的。

回答by stork

My only solution so far has been to put a call to performHapticFeedbackin the buttons OnClickListener. This works, but it seems like a bit of a hack.

到目前为止,我唯一的解决方案是performHapticFeedback在按钮 OnClickListener 中调用。这有效,但似乎有点黑客。

回答by Taranfx

Immersion has developed a Android Haptic Feedback SDK, brings the reality to android os and apps. Cant wait for Games :)

Immersion 开发了Android Haptic Feedback SDK,为 android 操作系统和应用程序带来现实。等不及游戏了:)