Develop Android from scratch 2048 (ii) Get gesture information

Source: Internet
Author: User
Tags gety

Today is the second day to try to start the Android version of 2048 mini-games. Today, I mainly learned how to get the user's swipe gesture on the screen, as well as a little bit of good layout.

gets the gesture of the user action, which is smoother than the direction left. Slide to the right, etc.) is mainly used in Gesturedetector. This class can help us get some common user-to-screen operations. such as clicking, double-clicking, pressing, dragging, and so on. Detailed instructions for use. We can check the API can also go to Baidu, or see me before there is a use gesturedetector implementation of the sample (http://blog.csdn.net/xiapinnong/article/details/21970419) There 's not much to say here.


let's look at the code below. Relative to the first, I add an inner class mygesturedetector in the code . And let this class implement the two interfaces, Ontouchlistener and Ongesturelistener.

Ontouchlistener This interface is mainly used for my gridlayout binding to touch this action of listening, and then transfer the detailed action to gesturedetector. By matching the user's detailed actions to different gestures.


public class Mygesturedetector implements ongesturelistener,ontouchlistener{@Overridepublic boolean OnTouch (View V, Motionevent event) {//TODO auto-generated method Stubreturn gd.ontouchevent (event);} @Overridepublic Boolean Ondown (motionevent e) {//TODO auto-generated method Stubreturn false;}              @Overridepublic boolean onfling (motionevent E1, motionevent E2, float velocityx,float velocityy) {//number of parameters explained: E1:1th action_down motionevent//E2: The movement speed on the last Action_move motionevent//velocityx:x axis, like M/S//velocityy:y the movement speed on the axis. Pixels/sec//Trigger Condition://x-axis coordinate displacement greater than fling_min_distance, and move faster than fling_min_velocity pixels per second if (e1.ge TX ()-e2.getx () >100) {System.out.println ("left"); return true;} ElseIf (E1.getx ()-e2.getx () <-100) {System.out.println ("right"); return true;} ElseIf (E1.gety ()-e2.gety () >100) {System.out.println ("up"); return true; ElseIf (E1.gety ()-e2.gety () <-00) {System.out.println ("down"); return true;} return false;} @Overridepublic void Onlongpress (Motionevent e) {//TODO auto-generated Method stub} @Overridepublic Boolean Onscroll ( Motionevent E1, Motionevent e2,float Distancex, float Distancey) {//TODO auto-generated method Stubreturn false;} @Overridepublic void Onshowpress (Motionevent e) {//TODO auto-generated Method stub} @Overridepublic Boolean Onsingletapup (motionevent e) {//TODO auto-generated method Stubreturn false;}}

Onfiling () This callback function mainly refers to the user tapping the screen and moving a short distance. Then let go.

The four parameters in this method:

E1 means: 1th action_down motionevent

E2 means: The last action_move motionevent   

movement speed on velocityx:x axis, pixels per second   

The speed of movement on the velocityy:y axis. Pixels per second

(Other callback functions and the description of the parameters, can refer to this blog http://blog.csdn.net/xiapinnong/article/details/21970419)


Infer the direction of the user's finger swipe:

Calculated by comparing the coordinates of the two motinevent of E1 and E2.

For example, the x-coordinate of E1 minus the x-coordinate of E2 is greater than 0. Indicates that the user's finger is sliding to the left


then we need to declare and initialize a gesturedetector corresponding, and pass in our Mygesturedetector object, and then bind to our GridLayout Ontouchlistener


The complete activity code such as the following:

Package Com.example.t2048;import Java.util.arraylist;import Java.util.list;import java.util.random;import Android.app.activity;import Android.os.bundle;import Android.view.gesturedetector;import Android.view.gesturedetector.ongesturelistener;import Android.view.menu;import Android.view.MotionEvent;import Android.view.view;import Android.view.view.ontouchlistener;import Android.widget.gridlayout;import Android.widget.imageview;public class Mainactivity extends Activity {GridLayout GridLayout = null;//where to save spaces list< integer> spacelist = new arraylist<integer> ()//for holding a position with a number lattice list<integer> stufflist = new arraylist< Integer> (); Gesturedetector GD = null;/** * Icon Array */private final int[] icons = {r.drawable.but_empty, r.drawable.but2,r.drawable.but4 , R.drawable.but8, R.drawable.but16,r.drawable.but32, R.drawable.but64, r.drawable.but128,r.drawable.but256, r.drawable.but512, r.drawable.but1024,r.drawable.but2048, r.drawable.but4096};p rotected void OnCreate (Bundle SavedINstancestate) {System.out.println ("program start"); Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); gridLayout = (gridLayout) Findviewbyid (R.ID.GRIDLAYOUT1); init (); Mygesturedetector mg = new Mygesturedetector (); gd = new Gesturedetector (mg); Gridlayout.setontouchlistener (MG); Gridlayout.setlongclickable (TRUE);} Initialize interface public void init () {System.out.println ("initialize");//First fill in 16 various blanks with blank picture for (int i=0;i<16;i++) {View view = View.inflate (this, r.layout.item, null); ImageView image = (ImageView) View.findviewbyid (r.id.image); Image.setbackgroundresource (Icons[0]); Spacelist.add (i); Gridlayout.addview (view);} Randomly add two x 2 or 4addRandomItem () to the interface, Addrandomitem ();} Randomly get the location public int Getrandomindex () {Random random = new random () from the empty column table, and if (Spacelist.size () >0) return Random.nextint (Spacelist.size ()); else return-1;} Randomly increases the number 2public void Addrandomitem () {int index = Getrandomindex () in the blank lattice, if (index!=-1) {//To get the corresponding Viewview view = Gridlayout.getchildat (Spacelist.get (index)); ImageView image = (ImagevieW) View.findviewbyid (r.id.image);//randomly generate a number 1 or 2int i = (int) math.round (math.random () +1);// Replace the picture of the current grid with 2 or 4image.setbackgroundresource (Icons[i]);//Remove the grid spacelist.remove (index) from the blank list;}} public class Mygesturedetector implements ongesturelistener,ontouchlistener{@Overridepublic boolean OnTouch (View V, Motionevent event) {//TODO auto-generated method Stubreturn gd.ontouchevent (event);} @Overridepublic Boolean Ondown (motionevent e) {//TODO auto-generated method Stubreturn false;}              @Overridepublic boolean onfling (motionevent E1, motionevent E2, float velocityx,float velocityy) {//number of parameters explained: E1:1th action_down motionevent//E2: The movement speed on the last Action_move motionevent//velocityx:x axis, like M/S//velocityy:y the movement speed on the axis. Pixels/sec//Trigger Condition://x-axis coordinate displacement greater than fling_min_distance, and move faster than fling_min_velocity pixels per second if (e1.ge TX ()-e2.getx () >100) {System.out.println ("left"); return true;} ElseIf (E1.getx ()-e2.getx () <-100) {System.out.println ("toRight "); return true;} ElseIf (E1.gety ()-e2.gety () >100) {System.out.println ("up"); return true; ElseIf (E1.gety ()-e2.gety () <-00) {System.out.println ("down"); return true;} return false;} @Overridepublic void Onlongpress (Motionevent e) {//TODO auto-generated Method stub} @Overridepublic Boolean Onscroll ( Motionevent E1, Motionevent e2,float Distancex, float Distancey) {//TODO auto-generated method Stubreturn false;} @Overridepublic void Onshowpress (Motionevent e) {//TODO auto-generated Method stub} @Overridepublic Boolean Onsingletapup (motionevent e) {//TODO auto-generated method Stubreturn false;}} public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the action bar if it is present.get Menuinflater (). Inflate (R.menu.main, menu); return true;}}

The program execution interface such as the following (the interface is more unsightly):




Swipe on the gridlayout with your finger to print a detailed slide direction in log



Develop Android from scratch 2048 (ii) Get gesture information

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.