1. Game Interface Layout
by Textview1 and Textview2 to display the score, 2048 game main interface consists of GridLayout, the bottom of which is to add a button to restart the game.
2. Create a game main class Gameview
Add the initialization Method Initgameview () in the build function;
Setontouchlistener () in the initialization Method Initgameview () to listen for touch events and add methods for the upper and lower left and right touch events swipeup (), Swipedown (), Swipeleft (), swiperight ();
To determine the touch event code:
Public BooleanOnTouch (View V, motionevent event) {Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:startX=Event.getx (); Starty=event.gety (); Break; CaseMotionEvent.ACTION_UP:offsetX= Event.getx ()-StartX; OffsetY= Event.gety ()-Starty; if(Math.Abs (OffsetX) >Math.Abs (OffsetY)) { if(offsetx<-5) {swipeleft (); }Else if(offsetx>5) {swiperight (); } }Else{ if(offsety<-5) {swipeup (); }Else if(offsety>5) {Swipedown (); } } Break; }
Use a custom class on an Android XML layout file
In a custom class, you must include a constructor, XXX (Context context,attributeset attr) {super (context,attr);}
Use Package.class in XML to invoke, such as registering for Com.test class as HelloWorld in XML <com.test.HelloWorld/>
The Layout_main.xml.
<com.mark.game.my2048.GameView android:id= "@+id/gameview" android:layout_width= " Fill_parent " android:layout_height=" 0DP " android:layout_weight=" 1 " > < /com.mark.game.my2048.gameview>
where android:layout_height= "0DP" android:layout_weight= "1" is used to indicate the amount of space to allocate. 1 means weight
2048 Little Games Little notes