In some cases, an object needs to be manipulated on the screen. This can be done using the stretch action, and the most natural way to do this is to simply use a two-finger pinch gesture.
Adding support for pinch-to-zoom is just a matter of passing the touch events to an instance of ScaleGestureDetector
and implementing the IOnScaleGestureListener
interface:
ScaleGestureDetector.IOnScaleGestureListener
interface:public class MyView : View, ScaleGestureDetector.IOnScaleGestureListener { public bool OnScaleBegin(ScaleGestureDetector detector) { return true; } public bool OnScale(ScaleGestureDetector detector) { return true; } public void OnScaleEnd(ScaleGestureDetector detector) { } }
ScaleGestureDetector
type:scaleDetector = new ScaleGestureDetector(Context, this);
OnTouchEvent()
method to pass the event to the scale gesture detector:public override bool OnTouchEvent(MotionEvent e) { var handled = scaleDetector.OnTouchEvent(e); return handled || base.OnTouchEvent(e); }
OnScale()
method to handle the actual gesture:public bool OnScale(ScaleGestureDetector detector) { scale *= scaleDetector.ScaleFactor; return true; }
Just as a user can touch the screen with one finger, gestures can be performed using multiple fingers. This creates an entirely new set of gestures that can now be performed. Some of these gestures can be pinches and twists, which are used to zoom or rotate respectively.
Using multitouch gestures allows the user to interact with the device in a more natural manner, and the app translates those gestures into the action that the user is trying to achieve.
Once such gesture is the pinch-to-zoom gesture. This is performed by making use of a ScaleGestureDetector
instance.
At the time of implementing the GestureDetector
object, we need to implement the IOnScaleGestureListener
interface. We then pass the implementation of the interface to a new instance of ScaleGestureDetector
.
The OnScaleBegin()
and OnScale()
methods on the interface should return true
when we are handling the event. When the user starts the scale gesture, the OnScaleBegin()
method is invoked. If we return false
here, the scale gesture will be canceled. This is useful if there is only a small region of the view that should be scaled. This allows us to make a decision to start a scale gesture if the user is touching the appropriate region.
To process the touch events, we override the OnTouchEvent()
method of the view and pass the MotionEvent
argument to the OnTouchEvent()
method of the gesture detector. This allows the gesture detector to invoke the various methods when a scale gesture is detected. Like with the simple gesture detector, the result of the gesture detector's OnTouchEvent()
method is used to determine whether the base OnTouchEvent()
method of the view should be invoked.
Once a scale gesture has commenced, the OnScale()
method will be invoked every time the user moves their fingers across the screen. In this method, we can obtain the values from the gesture detector using the various properties. The most important of these is the ScaleFactor
property, which is the amount scaled since the last time the method was invoked. The others include the FocusX
and FocusY
properties, which represent the focal point coordinates between the fingers, and the CurrentSpan
property, which represents the linear distance between the two fingers.