[Android game development 16th] Android gesture: [Touch Screen gesture recognition] operation! Use touch screen gestures to implement a simple picture switching function!

Source: Internet
Author: User

Li huaming himiOriginal, reprinted must be explicitly noted:

Reprinted from[Heimi gamedev block]Link: http://www.himigame.com/android-game/337.html

Many kids shoes say that after my code is run, clicking home or back will cause a program exception. If you have encountered this, you certainly haven't carefully read the himi blog, in article 19th, himi specifically wrote about the causes and solutions of these errors. I have added my remarks on this blog, and my provincial children's shoes are always confused. Please click here to contact us for further reading:

[Android game development 19th] (required) surfaceview Running Mechanism explanation-analyze back and home buttons and switch to the background to handle exceptions!

This article adds:Many online gestures articles say that android's support for gestures starts with SDK 1.6 (API 4), but I can also identify them using sdk1.5 simulators !. (I wanted to test the support effects of lower sdks, but I didn't have sdks earlier than version 1.5... my mobile SDK 2.2-,), so I checked the API and found that:

Android. View. gesturedetector. ongesturelistener; Since api-1,

Android. View. gesturedetector; Since api-1,

From the API, from the beginning of the api-1 has supported the gesture and gesture listener, so many say api-4 to support this sentence is also true! Because:Android. gestureThis class is fromApi-4Is supported. This classInput Method Gesture RecognitionWill be used in! So ~

Conclusion: Touch Screen Gesture RecognitionIs supported from API-1. WhileInput Method Gesture RecognitionIs API-4 to start supporting! Here we need to figure it out!

 

Android gestures are not often used in software, such as page turning and Page scrolling in browsers; of course, when we develop an android game, the android gesture operation will add a bright spot to the game, such as the general CAG, puz and other types of games can be switched off and moved with simple backgrounds. You can use gestures to perform operations, similar to the popular "Angry Birds!" Ke and I am sorry for making a mistake. It's Angry Birds, because I always listen to the group, and my friends say that the new version of the bird is coming, and the bird is coming out of the PC hard disk version! Alas ~ You can say it can make people angry. To be honest, the bird game is really good. The only highlight I see is the idea of this game! To be honest, the current game is not able to do anything but to think of creative ideas and Keke. Back to the topic, let's take a look at what Android gestures are!

 

The so-called gesture operations are similar to dance machines and ezdancer ~ These uses different actions and notes to make people dance, so the gestures here on Android only give us more tricks And gameplay for operations in games and software, according to the length of time when the player is exposed to the screen, the sliding distance on the screen and the push time are packed. In fact, Android encapsulates and processes the touch screen processing.

 

In Android, there are actually two Gesture Recognition Technologies. One isTouch screen Gesture Recognition, The other isInput Method Gesture RecognitionThe two are compared. The second is more flexible. You can customize gestures, which is relatively high! In this section, we will first introduce the first type of gesture recognition:Touch screen Gesture Recognition;In the next blog, I will explain to my shoesInput Method Gesture Recognition!

 

Put the two sheets first:

 

 

 

OK. In the old mode, run the code first:

Mysurfaceview. Java

Package COM. himi; <br/> Import Java. util. vector; <br/> Import android. content. context; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapfactory; <br/> Import android. graphics. canvas; <br/> Import android. graphics. color; <br/> Import android. graphics. paint; <br/> Import android. util. log; <br/> Import android. view. gesturedetector; <br/> Import android. view. motionevent; <br/> Import android Oid. view. surfaceholder; <br/> Import android. view. surfaceview; <br/> Import android. view. view; <br/> Import android. view. gesturedetector. ongesturelistener; <br/> Import android. view. surfaceholder. callback; <br/> Import android. view. view. ontouchlistener; <br/>/** <br/> * @ author himi <br/> * @ gesture (above) touch screen Gesture Recognition <br/> */<br/> public class mysurfaceviewanimation extends surfaceview implements callback, <br /> Runnable, ongesturelistener, ontouchlistener {<br/> private thread th = new thread (this); <br/> private surfaceholder SFH; <br/> private canvas; <br/> private paint; <br/> private bitmap BMP; <br/> private gesturedetector Gd; <br/> private int BMP _x, BMP _y; <br/> private Boolean ischagepage; <br/> private vector <string> v_str; // Note 1 <br/> Public mysurfaceviewanimation (context) {<br /> Super (context); <br/> v_str = new vector <string> (); <br/> This. setkeepscreenon (true); <br/> BMP = bitmapfactory. decoderesource (getresources (), <br/> r. drawable. himi_dream); <br/> SFH = This. getholder (); <br/> SFH. addcallback (this); <br/> paint = new paint (); <br/> paint. setantialias (true); <br/> This. setlongclickable (true); <br/> // setlongclickable (true) is required, because only in this way can <br/> // our current surfaceview (View) Process Different from Touch Screen mode; <br/> // For example: action_move, or multiple action_down <br/> This. setontouchlistener (this); // bind this class to the touch screen listener <br/> GD = new gesturedetector (this); <br/> GD. setislongpressenabled (true); <br/>}< br/> Public void surfacecreated (surfaceholder holder) {<br/> // when the system calls this method, the view is created. Therefore, the view width and height can be obtained here !! Some kids shoes always put things in the initialization function! <Br/> // It is recommended that the thread be started here. Because the picture in the initialization is not available, the view is abnormal when the canvas unlockcanvasandpost is submitted! <Br/> BMP _x = (getwidth ()-BMP. getwidth ()> 2; <br/> BMP _y = (getheight ()-BMP. getheight ()> 2; <br/> th. start (); <br/>}< br/> Public void draw () {<br/> try {<br/> canvas = SFH. lockcanvas (); <br/> If (canvas! = NULL) {<br/> canvas. drawcolor (color. white); // canvas screen flushing <br/> canvas. drawbitmap (BMP, BMP _x, BMP _y, paint); <br/> paint. settextsize (20); // set the text size <br/> paint. setcolor (color. white); <br/> // draw a rectangle to show the functions called by the gesture operation. <br/> canvas. drawrect (50, 30,175,120, paint); <br/> paint. setcolor (color. red); // set the text color <br/> If (v_str! = NULL) {<br/> for (INT I = 0; I <v_str.size (); I ++) {<br/> canvas. drawtext (v_str.elementat (I), 50, 50 + I * 30, <br/> paint ); <br/>}< br/>} catch (exception e) {<br/> log. V ("himi", "Draw is error! "); <Br/>}finally {<br/> SFH. unlockcanvasandpost (canvas); <br/>}< br/> @ override <br/> Public void run () {<br/> // todo auto-generated method stub <br/> while (true) {<br/> draw (); <br/> try {<br/> thread. sleep (100); <br/>} catch (exception ex) {<br/>}< br/> Public void surfacechanged (surfaceholder holder, int format, int width, <br/> int height) {<br/>}< br/> Public void surfacede Stroyed (surfaceholder holder) {<br/>}< br/> // @ override <br/> // public Boolean ontouchevent (motionevent event) {// Note 2 <br/> // return true; <br/> //} <br/> @ override <br/> Public Boolean ontouch (view V, motionevent event) {// Note 3 <br/> If (v_str! = NULL) <br/> v_str.removeallelements (); <br/> return GD. ontouchevent (event ); // Note 4 <br/>}< br/> // -------------- The following is the rewrite function --------- <br/>/** <br/> * @ parameter explanation in the following method: <br/> * @ E1: the first action is triggered by action_down motionevent. <br/> * @ E2: the last one is the action of raising the action_up motionevent (which should be explained in note 5) <br/> * @ velocityx: The moving speed on the X axis, pixel/second <br/> * @ velocityy: moving speed on the Y axis, pixel/second <br/> */<br/> @ override <br/> Public Boolean on Down (motionevent e) {<br/> // action_down <br/> v_str.add ("ondown"); <br/> return false; <br/>}< br/> @ override <br/> // action_down, do not move the short press <br/> Public void onshowpress (motionevent E) {<br/> v_str.add ("onshowpress "); <br/>}< br/> @ override <br/> // action_down, long press without moving <br/> Public void onlongpress (motionevent E) {<br/> v_str.add ("onlongpress"); <br/>}< br/> @ override <br/> // action_down, slow sliding <br/> Public Bo Olean onscroll (motionevent E1, motionevent E2, float distancex, <br/> float distancey) {<br/> v_str.add ("onscroll"); <br/> return false; <br/>}< br/> @ override <br/> // action_down, slide, action_up <br/> Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, <br/> float velocityy) {<br/> v_str.add ("onfling"); <br/> // ------- remarks 5 ---------- <br/> // If (e1.getaction () = motionevent. action_move) {<Br/> // v_str.add ("onfling"); <br/> //} else if (e1.getaction () = motionevent. action_down) {<br/> // v_str.add ("onfling"); <br/> //} else if (e1.getaction () = motionevent. action_up) {<br/> // v_str.add ("onfling"); <br/>/}< br/> // If (e2.getaction () = motionevent. action_move) {<br/> // v_str.add ("onfling"); <br/> //} else if (e2.getaction () = motionevent. action_down) {<br/> // v_str.add ("onfling"); <br/> //} e LSE if (e2.getaction () = motionevent. action_up) {<br/> // v_str.add ("onfling"); <br/>/}< br/> If (ischagepage) <br/> BMP = bitmapfactory. decoderesource (getresources (), <br/> r. drawable. himi_dream); <br/> else <br/> BMP = bitmapfactory. decoderesource (getresources (), <br/> r. drawable. himi_warm); <br/> ischagepage =! Ischagepage; <br/> return false; <br/>}< br/> @ override <br/> // short by action_down, action_up <br/> Public Boolean onsingletapup (motionevent E) {<br/> v_str.add ("onsingletapup"); <br/> return false; <br/>}< br/>} 


Add:When the code initializes the gesture, there is such a sentence: Gd. setislongpressenabled (true); this function ID enables long buttons if you set true. You can get the onlongpress gesture when the touch screen remains unchanged for a long time, if it is set to false, the touch screen won't be supported for a long time ~ This function is neither set nor set to true by default.

 

Note 1:

Here I will just give a brief introduction to some children's shoes who are not familiar with the definition of the vector method: We usually define the container directly when vector Vc = new vector (); well, that's right, however, the definition of this vector <string> is a generic definition, so the difference is simply put: If vector Vc = new vector (); after loading the object in this way, do I need to convert the extracted data type to another type ?! The definition of vector <string> indicates that this container only contains elements of the string type ~ You don't have to turn it around again when you pull it out.

NOTE 2:The test shows that the touch screen time is still returned, even if you set the touch screen focus to setfocusableintouchmode (false), it will be called !!! The reason is that the view of this class is bound with a touch-screen event listener, so it will certainly respond first.Note 3And then weNote 4Here, we didn't return true, but directly returned it to the gesture listener for listening, asking the listener to find a suitable function to process the user's gesture. That is to say, there is no sign to complete the processing, so our rewritten ontouchevent () will continue to process it!

Note 5:

The Code commented here is to test which two actions are actually two, because the android gestures posted on the Internet are crazy:

The first is motionevent. action_down, and the second is motionevent. action_move! In this case, the first action is to press the button to understand the player's touch screen action, and the second action is to move! Have all the moving points been recorded ??

 Test results show that:

The first is motionevent. action_down. The second isMotionevent. action_up!

Alas ~ The posts on the Internet are all copied ~ Cannot be tested ?? Depressed! Since one of the two actions is to push one by one, it makes sense clearly.

The two actions know the sliding distance of the user, and the distance between them is e2.getx ()-e1.getx ();

 

Summary:

1. After the touch screen, the touch screen remains unchanged and the evolution sequence is: ondown-> onshowpress-> onlongpress;

2. After the touch screen is reached, the slow movement of the touch screen is onscroll/The Fast movement is onfling, And the finger leaves the screen;

Note: After the touch screen, the touch screen is always moving. If your fingers do not leave the screen, it will always be onscroll. No matter how fast you move, it will never be onfling!

 

OK, although the gesture is quite simple, if you are proficient in using it and joining the game, it will definitely increase the color of your game ~

 

I will provide the source code here: I only process one gesture for this instance, because other actions are simple and not much to say ~ OK, good night ~

Source code: http://www.himigame.com/android-game/337.html

 


 


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.