Android-Gesture Recognition (common gesture recognition: top, bottom, left, and right; custom Gesture Recognition: Right and Wrong)

Source: Internet
Author: User
Tags gety

Android-Gesture Recognition (common gesture recognition: top, bottom, left, and right; custom Gesture Recognition: Right and Wrong)

1. Review

I learned about 10 instances of Android system services in the previous article. Of course, more system services are not used. The regret is that the demo was not shared last night;

 

 

2. Key Points

(1) OnTouchListener for upper, lower, and left Gesture Recognition

(2) OnTouchListener + SimpleOnGestureListener + GestureDetector for upper, lower, and left Gesture Recognition

(3) GestureOverlayView + GestureLibrary + ongesturew.medlistener for custom Gesture Recognition

 

3.

Common Gesture Recognition: Custom gesture recognition:

 

4. Image preparation

In order to achieve better results, we made nine Images Using ps for a more intuitive display;

(1) standby

 

(2) normal gesture judgment: upper, lower, and left

(3) custom gesture judgment: Right, wrong, circle, and thunder

 

5. OnTouchListener for upper, lower, and left Gesture Recognition

5.1 ideas

(1) OnTouchListener has been used before and used in ViewFlipper;

(2) Set OnTouchListener for judgment in the same way as the basic idea;

5.2 Implementation

Set listening for images or la S;

 

/*** Method 1: * add a touch event listener * @ author yuan **/class imgtestTouchListener implements OnTouchListener {private int x = 0; private int y = 0; @ Overridepublic boolean onTouch (View v, MotionEvent event) {// you can use x and y to determine the sliding gesture. // However, you can only determine whether the slider is on, down, or left, right-sliding switch (event. getAction () {case MotionEvent. ACTION_DOWN: // when the finger is pressed: Initialize x, y value x = (int) event. getX (); y = (int) event. getY (); break; case MotionEvent. ACTION_MOVE: // do not move it, Because judging the gesture here is only related to the position of the press and the position of the lift; break; case MotionEvent. ACTION_UP:/** triggered when the finger is raised, so the ** 1. obtain the end x, y * 2. determine */int upx = (int) event. getX (); int upy = (int) event. getY (); String result = drawTouch (upx, upy); // reminds textView1.setText (result); break;} return true;} private String drawTouch (int upx, int upy) {String str = No sliding; // horizontal sliding if (upx-x> 100) {str = sliding to the right; // change the image img_test.setImageResource (R. drawable. icon_right);} else if (x-upx> 100) {str = sliding to the left; // change the image img_test.setImageResource (R. drawable. icon_left);} else if (upy-y> 100) {str = slide down; // change the image img_test.setImageResource (R. drawable. icon_down);} else if (y-upy> 100) {str = slide up; // change the image img_test.setImageResource (R. drawable. icon_up) ;}return str ;}}

6. OnTouchListener + SimpleOnGestureListener + GestureDetector for upper and lower-left Gesture Recognition

 

6.1 ideas

(1) Like OnTouchListener, The MotionEvent object is handed over to the GestureDeteCtor object for processing.

(2) GestureDetector implements not only the upper and lower sides of the judgment, but also the onFling () method.

 

6.2 Implementation

(1) declare and instantiate GestureDetector

 

      private GestureDetector gestureDetector;

 

(2) It must be instantiated in OnCreate. Otherwise, a null pointer exception is reported.

// 00. initialize the GestureDetector object gestureDetector = new GestureDetector (MainActivity. this, new GestureDelectorSimlpeListener ());

(3) Implement the SimpleOnGestureListener listener

 

 

/*** 2. inherit the SimpleOnGestureListener * overload gesture of Interest * @ author yuan **/class extends SimpleOnGestureListener {@ Overridepublic boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {/** slide using the onFling () method * 3. judge */String result = drawTouch (e1.getX (), e1.getY (), e2.getX (), e2.getY (); textView1.setText (result); return true ;} /*** gesture judgment ** @ param x * @ param y * @ param upx * @ param upy * @ return */private String drawTouch (float x, float y, float upx, float upy) {String str = no slide; // horizontal slide if (upx-x> 100) {str = slide to the right; // change the image img_test.setImageResource (R. drawable. icon_right);} else if (x-upx> 100) {str = sliding to the left; // change the image img_test.setImageResource (R. drawable. icon_left);} else if (upy-y> 100) {str = slide down; // change the image img_test.setImageResource (R. drawable. icon_down);} else if (y-upy> 100) {str = slide up; // change the image img_test.setImageResource (R. drawable. icon_up) ;}return str ;}}

(4) OnTouchListener listener

 

 

/*** Second method ** use GestureDetector to operate **/class GestureOntouchListener implements OnTouchListener {@ Overridepublic boolean onTouch (View v, MotionEvent event) {// 1. obtain the motionEvent object gestureDetector. onTouchEvent (event); return true ;}}

 

Related Article

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.