Created
October 31, 2020 15:14
-
-
Save yasukotelin/1b86c0cd4f6a14f4d88699e49c364e87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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