How to Detect Long Clicks in Android Using OpenGL-ES
To detect when the user presses a surface being rendered by an OpenGL-ES application, developers typically use the onTouchEvent(MotionEvent event) method. However, this method does not have built-in functionality for detecting long clicks.
One approach is to register for the ACTION_DOWN event. Then, in onTouchEvent, schedule a Runnable to run after a certain time delay. If the Runnable is canceled before it runs, due to an ACTION_UP or ACTION_MOVE event, it indicates that the user did not perform a long click.
Alternatively, Android provides a more sophisticated solution: GestureDetector, which can be used to detect a variety of gestures, including long clicks.
Using GestureDetector
To use GestureDetector, follow these steps:
Here is an example of using GestureDetector to detect long clicks:
class MyActivity : AppCompatActivity() {
private lateinit var gestureDetector: GestureDetector
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
gestureDetector = GestureDetector(this, LongClickListener())
}
override fun onTouchEvent(event: MotionEvent): Boolean {
gestureDetector.onTouchEvent(event)
return super.onTouchEvent(event)
}
inner class LongClickListener : OnGestureListener {
override fun onLongPress(e: MotionEvent?) {
// Handle long click here.
}
// Implement other gesture methods as needed.
}
}
By using GestureDetector, you can easily detect long clicks in your OpenGL-ES applications.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3