[Android game development 15th] performance optimization of OnTouchEvent () Touch Screen events in Android game development

Source: Internet
Author: User

 

Let's take a look at the previous Code:

 

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

Package com. himi;

Import android. app. Activity;

Import android. OS. Bundle;

Import android. util. Log;

Import android. view. MotionEvent;

Import android. view. Window;

Import android. view. WindowManager;

/**

* @ Author Himi

*/

Public class MainActivity extends Activity {

Private Object object;

Private final int TIME = 50; // Note 1

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

GetWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN, WindowManager. LayoutParams. FLAG_FULLSCREEN );

This. requestWindowFeature (Window. FEATURE_NO_TITLE );

SetContentView (R. layout. main );

Object = new Object ();

}

@ Override

Public boolean onTouchEvent (MotionEvent event ){

If (event. getAction () = MotionEvent. ACTION_DOWN ){

Log. v ("Himi", "ACTION_DOWN ");

} Else if (event. getAction () = MotionEvent. ACTION_UP ){

Log. v ("Himi", "ACTION_UP ");

} Else if (event. getAction () = MotionEvent. ACTION_MOVE ){

Log. v ("Himi", "ACTION_MOVE ");

}

Synchronized (object) {// Note 2

Try {

Object. wait (TIME );

} Catch (InterruptedException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

}

Return true; // true must be returned here! For the reason, see [9 of Android2D Game Development]

}

}

 

 

The code is very clear, mainly in Note 2:

 

I. Preface:

 

All the children's shoes must know that in the simulator, when we click a simulator screen and release it, ACTION_DOWN is triggered first and then ACTION_UP; if we move it on the screen, ACTION_MOVE is triggered; OK, right. But you need to know that this is just a simulator !!!!!

 

Ii. Differences between a real machine and a simulator:

When it comes to our small users (when it comes to users, I think of the classic line of "My name is MT" from the dark night man: Dear customer, I am a tender Dad !) Back to the topic: when our users are playing our games, especially RPG, they must press our virtual buttons on the touch screen for a long time, for example, we will draw a virtual steering wheel on the screen ~ In fact, the ACTION_MOVE event will be continuously responded by Android !!

 

3. Why does ACTION_MOVE always respond? If a user does not move his or her fingers, but does not move his or her fingers, the user will always respond?

 

There are two reasons: First, Android is very sensitive to touch screen events! Second: although our fingers are still and not moving, this is not the case! When our fingers touch the screen of the mobile phone, we feel that our fingers are still motionless. In fact, our fingers are shaking and shaking. Don't believe you try to hold your fingers at rest? Hey hey ~

 

So ~ We have to analyze it. If ACTION_MOVE has been continuously responded and processed by Android OS at this time, it will undoubtedly put a lot of pressure on our game performance!

For example, if the rendering time of the game thread is 100 ms each time, when you touch the screen with your fingers, about 10 motionevents will be generated in the short 0.1 seconds, in addition, the system will send these events to the listening thread as quickly as possible. In this way, the cpu will be busy processing onTouchEvent during this period of time, so that the image will become stuck and stuck.

In fact, we don't need to press the button to respond so many times. Instead, we need to accept the user button after each drawing or before drawing, this will prevent the frame rate from dropping too much, isn't it ?! So ~ We need to control this time, let him slow down, and work together with our drawing time ~ This reduces the burden on many system threads.

 

NOTE 2:

Some children's shoes may ask why the sleep () method is not required. In fact, if we just want to sleep the thread for a specified time, we can use the sleep () function, but there is no resource lock limit. The wait and notify methods of the Object are usually used to limit the wait time, and must be written in the synchronous code block. So ~

Some children's shoes will ask why this. wait () is not used by objects of the current class, but a new object is used:

In synchronized, the Object indicates that the wait call of the Object must have the monitoring lock of the object. Currently, we have the object lock, so we need to use the Object to call wait ~

Note 1:

We all know that the variables here are actually the sleep time we set, so here I want to tell you the value of this time, as I have mentioned above, in our game, we only need to press the button at the same time as our drawing thread. Of course, this is our ideal time! If there are character frames in the game, it is quite appropriate to set the sleep time based on the number of frames. After all, the logic of one frame of the character is executed ~ This depends on the game situation ~

 

Note: The Object. wait (long timeout) method also needs to be used with caution!

The reason is that the test found that the sleep time is actually a little longer than the time you specified, but it is okay to properly control the time.

 

 

Supplement:

1. I can see my shoes. // Note 2. Can I use this, that is, the current object? The answer is yes, but it is best not to use it like this, the reason is that a deadlock may occur if the Object needs to be synchronized elsewhere! The reason why a new object can be used is that the Code does what it should do and does not affect each other,

2. // Note 2 we can optimize it here. After all, an Object is a waste of resources. We only need one byte, so ~ We can define one as follows: byte [] lock = new byte [0]; this is the best ~

 

 

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

Address: http://blog.csdn.net/xiaominghimi/archive/2011/01/10/6127578.aspx

 

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.