[Android game development 16th] Android Gesture [Touch Screen Gesture recognition] operation

Source: Internet
Author: User

This article adds: Many online gesture articles say that Android supports gestures only from SDK 1.6 (that is, 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. gesture this class is from the api-4 to support, this class of input method gesture recognition will be used! So ~

Conclusion: Touch Screen Gesture Recognition is supported from API-1. And the input method of Gesture Recognition is API-4 began to support! 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 is touch screen gesture recognition, and the other is input method gesture recognition. The second is more flexible. You can customize gestures and compare them with others! In this section, we will first introduce the first type of Gesture Recognition: Touch Screen Gesture Recognition. In the next blog, I will explain the input method of Gesture Recognition to children's shoes!

 

Put the two sheets first:

 

 

 

 

 

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

MySurfaceView. java

 

··· · 50 ······· · 90 ····· · 140 · 150

Package com. himi;

Import java. util. Vector;

Import android. content. Context;

Import android. graphics. Bitmap;

Import android. graphics. BitmapFactory;

Import android. graphics. Canvas;

Import android. graphics. Color;

Import android. graphics. Paint;

Import android. util. Log;

Import android. view. GestureDetector;

Import android. view. MotionEvent;

Import android. view. SurfaceHolder;

Import android. view. SurfaceView;

Import android. view. View;

Import android. view. GestureDetector. OnGestureListener;

Import android. view. SurfaceHolder. Callback;

Import android. view. View. OnTouchListener;

/**

* @ Author Himi

* @ Gesture (above) Touch Screen Gesture Recognition

*/

Public class MySurfaceViewAnimation extends SurfaceView implements Callback,

Runnable, OnGestureListener, OnTouchListener {

Private Thread th = new Thread (this );

Private SurfaceHolder sfh;

Private Canvas canvas;

Private Paint paint;

Private Bitmap bmp;

Private GestureDetector gd;

Private int BMP _x, BMP _y;

Private boolean isChagePage;

Private Vector <String> v_str; // Note 1

Public MySurfaceViewAnimation (Context context ){

Super (context );

V_str = new Vector <String> ();

This. setKeepScreenOn (true );

Bmp = BitmapFactory. decodeResource (getResources (),

R. drawable. himi_dream );

Sfh = this. getHolder ();

Sfh. addCallback (this );

Paint = new Paint ();

Paint. setAntiAlias (true );

This. setLongClickable (true );

// SetLongClickable (true) is required, because this is the only way,

// Our current SurfaceView (view) can process different forms of touch screen;

// For example: ACTION_MOVE, or multiple ACTION_DOWN

This. setOnTouchListener (this); // binds this class to a touch screen listener.

Gd = new GestureDetector (this );

Gd. setIsLongpressEnabled (true );

}

Public void surfaceCreated (SurfaceHolder holder ){

// The view is created only when the system calls this method, so the view width and height can be obtained here !! Some kids shoes always put things in the initialization function!

// It is best to put the thread here to start it. Because the picture in the initialization is not available, the view is abnormal when the canvas unlockCanvasAndPost is submitted!

Bmp _x = (getWidth ()-bmp. getWidth ()> 2;

Bmp _y = (getHeight ()-bmp. getHeight ()> 2;

Th. start ();

}

Public void draw (){

Try {

Canvas = sfh. lockCanvas ();

If (canvas! = Null ){

Canvas. drawColor (Color. WHITE); // fl the canvas

Canvas. drawBitmap (bmp, bmp _x, bmp _y, paint );

Paint. setTextSize (20); // you can specify the text size.

Paint. setColor (Color. WHITE );

// Draw a rectangle to show the functions called by the gesture operation.

Canvas. drawRect (50, 30,175,120, paint );

Paint. setColor (Color. RED); // you can specify the text Color.

If (v_str! = Null ){

For (int I = 0; I <v_str.size (); I ++ ){

Canvas. drawText (v_str.elementAt (I), 50, 50 + I * 30,

Paint );

}

}

}

} Catch (Exception e ){

Log. v ("Himi", "draw is Error! ");

} Finally {

Sfh. unlockCanvasAndPost (canvas );

}

}

@ Override

Public void run (){

// TODO Auto-generated method stub

While (true ){

Draw ();

Try {

Thread. sleep (100 );

} Catch (Exception ex ){

}

}

}

Public void surfaceChanged (SurfaceHolder holder, int format, int width,

Int height ){

}

Public void surfaceDestroyed (SurfaceHolder holder ){

}

// @ Override

// Public boolean onTouchEvent (MotionEvent event) {// Note 2

// Return true;

//}

@ Override

Public boolean onTouch (View v, MotionEvent event) {// Note 3

If (v_str! = Null)

V_str.removeAllElements ();

Return gd. onTouchEvent (event); // Note 4

}

// -------------- The following is the function that is rewritten when the OnGestureListener gesture is used for listening ---------

/**

* @ Parameter explanation in the following method:

* @ E1: 1st are the actions of ACTION_DOWN MotionEvent.

* @ E2: the last one is the action of raising the ACTION_UP MotionEvent (here we will look at the explanation in Remark 5)

* @ VelocityX: movement speed on the X axis, pixel/second

* @ VelocityY: The moving speed on the Y axis, pixel/second

*/

@ Override

Public boolean onDown (MotionEvent e ){

// ACTION_DOWN

V_str.add ("onDown ");

Return false;

}

@ Override

// ACTION_DOWN, do not move the short press

Public void onShowPress (MotionEvent e ){

V_str.add ("onShowPress ");

}

@ Override

// ACTION_DOWN, long-pressed, non-slide

Public void onLongPress (MotionEvent e ){

V_str.add ("onLongPress ");

}

@ Override

// ACTION_DOWN, slow sliding

Public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX,

Float distanceY ){

V_str.add ("onScroll ");

Return false;

}

@ Override

// ACTION_DOWN, fast sliding, ACTION_UP

Public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX,

Float velocityY ){

V_str.add ("onFling ");

// ------- Remarks 5 ----------

// If (e1.getAction () = MotionEvent. ACTION_MOVE ){

// V_str.add ("onFling ");

//} Else if (e1.getAction () = MotionEvent. ACTION_DOWN ){

// V_str.add ("onFling ");

//} Else if (e1.getAction () = MotionEvent. ACTION_UP ){

// V_str.add ("onFling ");

//}

// If (e2.getAction () = MotionEvent. ACTION_MOVE ){

// V_str.add ("onFling ");

//} Else if (e2.getAction () = MotionEvent. ACTION_DOWN ){

// V_str.add ("onFling ");

//} Else if (e2.getAction () = MotionEvent. ACTION_UP ){

// V_str.add ("onFling ");

//}

If (isChagePage)

Bmp = BitmapFactory. decodeResource (getResources (),

R. drawable. himi_dream );

Else

Bmp = BitmapFactory. decodeResource (getResources (),

R. drawable. himi_warm );

IsChagePage =! IsChagePage;

Return false;

}

@ Override

// Short by ACTION_DOWN, ACTION_UP

Public boolean onSingleTapUp (MotionEvent e ){

V_str.add ("onSingleTapUp ");

Return false;

}

}

 

 

Note: The Code initialization gesture has the following 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 in this class is bound with a touch-screen event listener, so we will certainly first respond to note 3, and then we note 4. Here we do not return true, but directly return it to the gesture listener for listening, let the listener find a suitable function to process user gestures. That is to say, the onTouchEvent () is overwritten and will continue to be processed!

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, and the second is MotionEvent. 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://download.csdn.net/source/2977447

 

Himi original, you are welcome to reprint, please note clearly! Thank you.

Address: http://blog.csdn.net/xiaominghimi/archive/2011/01/12/6130196.aspx

 

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.