Skip to content

Instantly share code, notes, and snippets.

@yasukotelin
Created October 31, 2020 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasukotelin/1b86c0cd4f6a14f4d88699e49c364e87 to your computer and use it in GitHub Desktop.
Save yasukotelin/1b86c0cd4f6a14f4d88699e49c364e87 to your computer and use it in GitHub Desktop.
import android.view.HapticFeedbackConstants
import android.view.MotionEvent
import android.view.View
/**
* Viewへのタップイベントに対してHaptic feedbackするヘルパー
*/
class HapticOnTapHelper : HapticOnTouchHelper {
override fun onTouch(v: View, e: MotionEvent): Boolean {
val isHandled = v.onTouchEvent(e)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O_MR1) {
if (isHandled) {
when (e.actionMasked) {
MotionEvent.ACTION_DOWN -> {
v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
}
MotionEvent.ACTION_UP -> {
v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY_RELEASE)
}
}
}
}
return isHandled
}
}
import android.view.MotionEvent
import android.view.View
/**
* [View.OnTouchListener]に処理を委譲する用のヘルパーインターフェイス
* BindingAdapterなどでOnTouchListenerに実装すると他のタップイベントを処理できなくなるため、
* Haptic用のヘルパーを使ってDIする
*/
interface HapticOnTouchHelper {
/**
* onTouch時にHaptic Feedbackを[v]に与える
*/
fun onTouch(v: View, e: MotionEvent): Boolean
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment