Android Game development: an example of switching pictures with gesture manipulation _android

Source: Internet
Author: User
Tags event listener

For Android gestures are often used not only in software, such as the page in the browser, scrolling pages, and so on, of course, when we develop the Android game with the Android gesture operation will add a bright spot, such as the General CAG, Puz and other types of game selection hurdles , simple background, such as mobile, can use gestures to operate it, similar to the time before a very hot "Angry Birds", bird this game is really good, I saw the only bright spot is the creative game! To tell the truth, now the game has not been made out of the only good ideas not to come out. To get back to the topic, let's take a look at what Android gestures are!

Gesture Recognition Overview

The so-called gesture operation, such as Dance machine, ezdancer, such as the use of different movements and notes to make people dance, then the android gestures here just let us in the game and software operation has more tricks and play, according to the player contact screen time length, the screen sliding distance, Press the lifting time and so on the packaging, in fact, is Android to touch screen processing did the packaging and processing.

So there are actually two kinds of gesture recognition techniques in Android. One is the touch screen gesture recognition, the other is the input method gesture recognition. The second is more flexible, you can customize gestures, compare high! So in this section we first introduce the first gesture recognition: touch screen gesture recognition. In the next blog I will give children's shoes to explain the input method gesture recognition!

Gesture Recognition Example

Let's put two screenshots on the table first:

OK, first code:

Mysurfaceview.java

Java code

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, call  
  Back, 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;//Notes 1 public mysurfaceviewanimation (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 necessary because only then//our current Surfaceview (view) can handle different forms of touch screen;  
    For example: Action_move, or multiple Action_down this.setontouchlistener (this);//Bind this class to the touchscreen listener GD = new Gesturedetector (this);  
  Gd.setislongpressenabled (TRUE); public void surfacecreated (Surfaceholder holder) {//When the system calls this method to create a view so you can get a view of the wide height!!  
    Some children's shoes always put things in the initialization function!  
    The thread is best put here to start, because the painting in the initialization, the view has not yet, to submit the canvas unlockcanvasandpost when it is abnormal!  
    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);//Canvas Brush screen canvas.drawbitmap (BMP, Bmp_x, bmp_y, Paint  
        );  
        Paint.settextsize (20);//Set Text size Paint.setcolor (color.white);  
        Here's a rectangle to make it easy for children to see what the function of the gesture calls is Canvas.drawrect (175,120, paint);   
            Paint.setcolor (color.red)//Set text color if (v_str!= null) {for (int i = 0; i < v_str.size (); i++) {  
          Canvas.drawtext (V_str.elementat (i), m, + i *, 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 Widt h, int height) {} public void surfacedestroyed (Surfaceholder holder) {}//@Override//Publ  
  IC Boolean ontouchevent (Motionevent event) {//Memo 2//return true; @Override public boolean Ontouch (View V, motionevent event) {//Memo 3 if (v_str!= null) v_str.rem  
    Oveallelements (); Return Gd.ontouchevent (event);//Memo 4}//--------------The following functions are rewritten when using ongesturelistener gesture monitoring---------/** * The parameters in the following method are interpreted as follows: * @e1: The 1th one is Action_down motionevent press the action * @e2: The latter one is action_up motionevent lifting action (here to see the explanation of Note 5) * @ve Move speed on locityx:x axis, pixel/sec * @velocityY: Move speed on y axis, pixel/sec/@Override public boolean ondown (Motionevent e) {/  
    /Action_down V_str.add ("Ondown");  
  return false; @Override//Action_down, Short press does not move public void onshowpress (Motionevent e) { 
    V_str.add ("onshowpress");  
  @Override//Action_down, long press not sliding 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 velocit  
    YX, float velocityy) {v_str.add ("onfling");  
    -------Note 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 Press Action_down, ACTION_UP public boolean onsingletapup (Motionevent e) {v_str.add ("Onsinglet  
    Apup ");  
  return false; 
 }  
}

Add: When the code initializes the gesture, there is a phrase: gd.setislongpressenabled (true); This function identifies, if you set true, you turn on the long button, you can get onlongpress gesture when you touch the screen for a long time. If you set false, you will not get the support of this gesture for a long time to touch the screen without moving. This function is not set and the default is set to True.

Note 1:

Here, I'm just giving some little bit of familiarity with this definition of a vector. A brief introduction: We generally define containers as direct vector VC =new vector (); Well, yes, but the definition of this vector<string> is a generic definition, So simple to say the difference, if vector vc =new vector (), this way after the object, take the time to take the removal of a strong turn the type?! Oh, and vector<string> this definition of the time to show that this container I only loaded with String elements, so~ out of the time do not have to go strong.

NOTE 2:

The test found that the touch-screen event still responds, even if you set the touchscreen focus to Setfocusableintouchmode (FALSE)! The reason is because our view of this class is bound to the touchscreen event listener, then we will respond to note 3 First, then we note 4 There is no return true but directly to the gesture listener to listen to, let the listener find the appropriate function to handle the user's gestures, that is, no sign processing completed, So our rewrite of the Ontouchevent () will also continue to deal with!

Note 5:

Here's the code for the annotation I'm testing two actions in the end which two, because the online introduction of Android gesture posts are crazy legends:

The first one is Motionevent.action_down, and the second is motionevent.action_move. Then the first action is to press a good understanding, is the player just touch the screen action, the second is move! Is the moving point recorded??

In fact, the test results found that:

The first one is Motionevent.action_down, and the second is motionevent.action_up!.

Alas ~ Now online posts really a variety of plagiarism ~ can not be tested under?? Depressed! Since these two actions one is to press the next one is to lift that very clear meaning, we can according to these two movements know the user to exactly glide distance and so on, its distance e2.getx ()-e1.getx ().

Summarize

1, touch screen, always touch screen, evolution sequence:ondown->onshowpress->onlongpress;

2, touch screen, has been slow to touch the screen is onscroll/fast moving is onfling, finger away from the screen;

Note: Touch screen, always touch the screen to move, if the finger does not leave the screen has always been onscroll, no matter how fast you move, will never be onfling!

Ok, the gesture is quite simple, but if you use it skillfully and join the game it certainly makes your game a lot more.

This example I only do a gesture of processing, because the other action is very simple not to say more.

Supplementary content:

Many of the online gesture articles say that Android's support for gestures starts with the SDK 1.6 (API 4), but I can also identify it with a SDK1.5 simulator! (I would like to test the lower SDK support effect, but I do not have the SDK below the 1.5 version), so I checked the API discovery:

Android.view.GestureDetector.OnGestureListener; Since Api-1,

Android.view.GestureDetector; Since Api-1,

From the API to see from the beginning of api-1 has supported gestures and gesture listeners, so many said api-4 only support this sentence is also true! Because: Android.gesture This class is from the api-4 only to support, this kind of input method gesture recognition will use.

Conclusion: touch-screen gesture recognition has been supported from API-1. And the input method gesture recognition is API-4 only began to support! We need to figure this out!

The above is the implementation of the Android gesture operation to switch the image of the data collation, follow-up continue to supplement the relevant information, thank you for your support for this site!

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.