Android Gesture Recognition-zoom

Source: Internet
Author: User

The previous article explains the recognition of clicks and double-click events in gesture recognition, using the Gesturedetector class and the Gesturedetectorcompat class to listen for simple actions in the user's touch screen.

Scaling

The basic usage is as follows, you can zoom a view by zooming gesture

 Public classScalegestureactivityextendsbaseactivity {PrivateView MView;PrivateScalegesturedetector Mscalegesturedetector;Private Static FinalString TAG = "scalegestureactivity";Private floatMscalefactor = 1.0f; @Overrideprotected voidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (r.layout.activity_scale_gesture);        MView = Findviewbyid (R.id.scale_view); Mscalegesturedetector =NewScalegesturedetector ( This, Mscalegesturelistener); } @Override Public BooleanOntouchevent (Motionevent event) {mscalegesturedetector.ontouchevent (event);return Super. Ontouchevent (event); }PrivateScalegesturedetector.onscalegesturelistener Mscalegesturelistener =NewScalegesturedetector.onscalegesturelistener () {@Override Public BooleanOnscale (Scalegesturedetector scalegesturedetector) {mscalefactor *= scalegesturedetector.getscalefactor ();            Mview.setscalex (Mscalefactor);            Mview.setscaley (Mscalefactor); LOG.I (TAG, "Onscale:"+ scalegesturedetector.getscalefactor () +"----"+mscalefactor);return true; } @Override Public BooleanOnscalebegin (Scalegesturedetector scalegesturedetector) {return true; } @Override Public voidOnscaleend (Scalegesturedetector scalegesturedetector) {}};}
View Code
The Scalegesturedetector official interpretation of this category is as follows:

Detects scaling transformation gestures using the supplied MotionEvent s. The ScaleGestureDetector.OnScaleGestureListener callback would notify users when a particular gesture event had occurred. This class should is used with MotionEvent s reported via touch. To use the This class:

  • Create An instance of ScaleGestureDetector yourView
  • In the onTouchEvent(MotionEvent) method ensure your call onTouchEvent(MotionEvent) . The methods defined in your callback would be executed when the events occur.

This class is constructed as follows:

Public Scalegesturedetector (context context, Scalegesturedetector.onscalegesturelistener listener)

By the construction method we know that a scaling class needs to set a scaling listener interface, the implementation of the Code to see the implementation of the Listener class interface, the following details the meaning of several functions:

    1. public boolean Onscale (Scalegesturedetector detector);

is called when zooming, and the return value indicates whether the scaling event has been processed. If it is already processed, the detector resets the scaling event, and if it is not processed, detector continues the calculation and modifies the return value of Getscalefactor () until it is processed. As a result, it is commonly used to scale only when the scale value reaches a certain value. The following code indicates that it is scaled only when the zoom multiplier is twice times, otherwise the event is not processed.

 Public boolean Onscale (Scalegesturedetector detector) {System.out.println (Detector.getscalefactor ());  If(Detector.getscalefactor () < 2) {returnfalse;} Mmatrix.prescale (Detector.getscalefactor (), Detector.getscalefactor ()); Setimagematrix (MMatrix); return true;}

2.public Boolean onscalebegin (Scalegesturedetector detector)

The zoom is called when it starts. The return value indicates whether the detector handles subsequent scaling events. When False is returned, subsequent onscale () are not performed.

3. public void Onscaleend (Scalegesturedetector scalegesturedetector)

Callback at end of zoom

http://blog.csdn.net/qiwenmingshiwo/article/details/70850406

Https://github.com/Almeros/android-gesture-detectors

Https://github.com/Almeros/android-gesture-detectors-example/blob/master/app/src/main/java/com/almeros/android /multitouch/example/touchactivity.java

Android Gesture Recognition-zoom

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.