FIT2081 Lecture Notes - Lecture 11: X&Y, Code Bloat
GestureDetector
="detects"various"gestures"and"events"using"the"supplied"MotionEvents
onTouchEvent(MotionEvent)
Pass"all"MotionEvents to"GestureDetector
○
Returns"a"boolean from"the"callback"the"MotionEvent invokes
○
•
SimpleOnGestureListener
•
(Simple"used"if"you"only"want"to"listen"for"a"subset,"easier)
="notifies"users"when"a"particular"motion"event"has"occured
Subclassed"so"inherits."
○
Extended"by"private class MyGestureListener
Convenience class
§
="a"basic"implementation"for"all"interfaces
Implements"all"the"method"headers"of"one/more"
interfaces"as"implemented"but"empty"methods
□
Normally"when"you"implement"an"interface,"you"must"
implement"ALL"its"methods"and"override"them
Just"deleting"callbacks"you"don't"need"won't"work
®
□
But"now,"can"override"only"the"methods"you're"
interested"in
No"clutter"of"implemented"but"empty"methods
®
□
onScroll(MotionEvent, MotionEvent, float, float)
vs"onFling(MotionEvent, MotionEvent, float,
float):"
MotionEvents are"the"DOWN"and"UP"motion"events"
that"define"the"start"and"end"of"the"gesture
□
X&Y"velocity"of"finger"motion"respectively,"while"
onScroll"is"change"in"distance
□
Called"once"per"gesture,"while"onScroll is"called"
many"times.
□
Move"with"more"speed,"while"onScroll moves"with"
more"distance
□
§
○
onLongPress(MotionEvent)
Notified"when"a"long"press"occurs"with"the"initial"DOWN
MotionEvent that"triggered"it
§
○
OnDoubleTapListener
onDoubleTap(MotionEvent)
Notified"when"a"double"tap"occurs
§
○
onSingleTapConfirmed
Notified"when"a"single"tap"occurs
§
vs"onSingleTapUp (SimpleOnGestureListener)"which"is"
invoked"when"the"up"event"signaling"the"end"of"the"tap"
occurs,"and"slightly"faster"than"onSingleTapConfirmed
which"has"a"slight"time"delay,"since"…Confirmed needs"a"
small"time"frame"to"tell"the"difference"between"a"single"and"
double"tap,"and"is"only"invoked"if"we"know"for"sure"that"there"
is"only"ONE"single"tap,"not"two"that"form"a"double
§
○
•
*return true/false from"above"callbacks
ScaleGestureDetector
="detects"scaling"transformation"gestures"using"the"supplied"MotionEvents
onTouchEvent(MotionEvent)
Pass"all"MotionEvents to"ScaleGestureDetector
○
Returns"a"boolean from"the"callback"the"MotionEvent invokes
○
•
SimpleOnScaleGestureListener
•
(Simple"used"if"you"only"want"to"listen"for"a"subset,"easier)
Subclassed"so"inherits."
○
Extended"by"private class MyScaleListener extends
ScaleGestureDetector
Convenience"class"(see"above)
§
onScaleBegin(ScaleGestureDetector)
Responds"to"the"beginning"of"a"scaling"gesture
§
Reported"by"new"points"going"down
§
Returns"boolean indicating"if"event"should"continue"to"be"
recognised
§
○
onScale(ScaleGestureDetector)
Returns"boolean indicating"if"event"has"been"handled
§
detector.getScaleFactor()
Returns"a"float,"the"scaling"factor"from"the"previous"
scale"event"to"the"current"event
□
§
○
onScaleEnd(ScaleGestureDetector)
When"pinching"has"ended
§
Returns"nothing
§
○
*return true/false from"above"callbacks
Gestures_Student
How$do$you$set$up$a$View$so$touch$events$on$it$can$be$detected$and$
responded$to?
After"giving"the"View"an"ID"so"it"can"be"referenced,"set"a"new"
View.onTouchListener()"containing"a"method"onTouch"with"2"parameters,"the"
View"and"a"MotionEvent.
Inside"the"onTouch"method,"create"two"objects,"one"to"detect"gestures"
(GestureDetector)"and"one"to"detect"scale"gestures"(ScaleDetector),"to"
pass"the"MotionEvent to.
These"objects"each"come"with"their"own"built-in"callbacks"that"will"be"invoked"
on"the"MotionEvent depending"on"the"gesture"patterns"it"matches."
These"callbacks"can"be"implemented"by"creating"private/Convenience"classes"
within"the"Activity"that"extend"and"the"listeners"for"each"detector,"and"
overriding"the"methods"you"need"to"use"in"your"app.
What$problem$does$it$solve?
When"the"implemented"callback"method"returns"false,"this"indicates"they"
haven't"handled"their"event."In"other"words,"we"override"the"callbacks"we"want"
to"react"to"and"return"true."By"doing"so"we"inherit"all"the"other"classes"but"only"
one"returns"true,"preventing"code"bloat.
What's$special$about$a$Convenience$class$for$a$listener$interface?
In"this"case,"callback"methods"that"have"returned"false"haven't"handled"the"
event."The"other"callbacks,"although"inherited,"return"false"and"the"event"will"be"
passed"into"the"view"to"further"event"handling."This"does"not"simply"stop"the"
handling"of"the"event.
Why$is$it$special?
The"callbacks"return"true"or"false"depending"on"whether"the"event"has"been"
handled."This"enables"the"event"to"be"handled"by"the"view"even"if"the"callback"
doesn't"handle"it.
What$is$the$effect$of$the$resolver.query$call?$What$does$it$rely$on?
It"gets"the"newest"added"value"from"the"table,"in"this"case"the"last"shape"added"
(the"newest)."So"it"allows"the"system"to"choose"the"newest"shape.
It"sorts"by"ID,"and"since"IDs"are"automatically"incremented,"it"selects"the"newest"
shape."It"then"limits"by"one,"pointing"the"cursor"at"the"newest"shape"and"only"
the"newest"shape.
What$is$the$effect$of$the$resolve.update$call?
It"will"change"the"onscreen"shape"with"new"values.
What$is$the$effect$of$this$code?
When"the"pinch"gesture"has"ended"(the"user"lifts"their"hands"up)"the"shape"will"
change"its"radius/width"and"height"to"match"that"of"the"final"position"of"the"
gesture."The"code"gets"the"final"position"of"the"pointers"and"uses"that"
updateLastShape"function"to"change"the"size"of"the"shape.
Week$11
Tuesday," 29"May"2018
11:09
Document Summary
= detects various gestures and events using the supplied motionevents ontouchevent(motionevent) Simpleongesturelistener (simple used if you only want to listen for a subset, easier) = notifies users when a particular motion event has occured. Implements all the method headers of one/more interfaces as implemented but empty methods. Normally when you implement an interface, you must implement all its methods and override them. Just deleting callbacks you don"t need won"t work. But now, can override only the methods you"re interested in. No clutter of implemented but empty methods onscroll(motionevent, motionevent, float, float) vs onfling(motionevent, motionevent, float, float): Motionevents are the down and up motion events that define the start and end of the gesture. X&y velocity of finger motion respectively, while onscroll is change in distance. Called once per gesture, while onscroll is called many times. Move with more speed, while onscroll moves with more distance onlongpress(motionevent) Notified when a long press occurs with the initial down.