Pro Android with Kotlin


CHAPTER 9: User Interface


Download 5.42 Mb.
Pdf ko'rish
bet204/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   200   201   202   203   204   205   206   207   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile



258
CHAPTER 9: User Interface
Multitouch
Multitouch events in the Android OS are surprisingly easy to handle. All you need is 
to overwrite the onTouchEvent() method of a View or a ViewGroup element. Inside the 
onTouchEvent(), you fetch the masked action and act upon it.
frog.setOnTouchListener { view, event ->
true
}
Note In older versions of Android, you usually dispatch on the action as in event.action. With 
multitouch gestures, it is better to act on the maskedAction.
Inside the listener, you get the masked action by event.actionMasked and pass it to a when()
{ .. } statement.
The magic now lies in this listener being invoked for all fingers (here called pointers
consecutively. To find out how many fingers currently are registered, you use event.
pointerCount, and if you want to know which finger the event belongs to you, use val index 
= event.actionIndex. A starting point is thus as follows:
theView.setOnTouchListener { view,event ->
fun actionToString(action:Int) : String = mapOf(
MotionEvent.ACTION_DOWN to "Down",
MotionEvent.ACTION_MOVE to "Move",
MotionEvent.ACTION_POINTER_DOWN to "Pointer Down",
MotionEvent.ACTION_UP to "Up",
MotionEvent.ACTION_POINTER_UP to "Pointer Up",
MotionEvent.ACTION_OUTSIDE to "Outside",
MotionEvent.ACTION_CANCEL to "Cancel").
getOrDefault(action,"")
val action = event.actionMasked
val index = event.actionIndex
var xPos = -1
var yPos = -1
Log.d("LOG", "The action is " +
actionToString(action))
if (event.pointerCount > 1) {
Log.d("LOG", "Multitouch event")
// The coordinates of the current screen contact,
// relative to the responding View or Activity.
xPos = event.getX(index).toInt()
yPos = event.getY(index).toInt()
} else {
// Single touch event



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   200   201   202   203   204   205   206   207   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling