Foreword: Recently compare like multithreading, see some thread case, here summarizes the whole process of playing the hamster game.
1. First, the main activity to load the layout, I generally like to write this earlier, so that in the main activity to play with these controls. Gossip not to say, a framelayout, has been what I used is what relativelayout,linearlayout, in fact, this thing would like to make a layout, only that. Inside there is a imageview, mainly is the mouse picture.
2. Next, declare some variables or constants in Mainactivity, which are temporary-----get things from others and use them yourself. The main member variables are the number of mice, the ImageView object, the handler object, the mouse position of the two-dollar array, which is the coordinates on the screen.
3. Open a thread directly in the OnCreate. Thread T=new thread (new Runnable) {This contains a run method}-----The Run method has an index value that records the mouse position index, and then a while (! Thread.CurrentThread (). isinterrupted ()) {.... Generates a random number-----gets a message (using the handler defined later)-----Some identification of the message-----handler.sendmessage (m) Send a message to handler-----then the thread sleeps for a while-----is also random}
4. Continue fencing in OnCreate, Handler=new handler () {Duplicate a handlermessage (Message) { If it's the MSG from the front-----get the location index value-----Set the x-coordinate-----Set the y-coordinate-----Set the mouse to display-----such as a imageview.setvisibility (view.visible)}----- ImageView is the mouse's picture name}
5. Add a touch listener to the mouse-----first let it disappear-----then the number of mice hit by the user +1. Mouse. Setontouchlistener (new Ontouchlistener () {...}) -----Make a copy of the Ontouch method, the parameter is a view and an event, which will be the view. Setvisibility (view.invisible), then the global i++, and then a toast is displayed to the user.
6.That's all. It was so simple to hit the hamster. is the simple multithreading interaction.
Good Morning (づ ̄3 ̄) Old-----2017/7/14 8:00
Android Multi-threaded Hamster game