[Android game development 9th] (detailed handling) Bug solutions in touch screen events and prevention of horizontal and vertical screen Switching

Source: Internet
Author: User

 

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

Address: http://blog.csdn.net/xiaominghimi/archive/2010/12/29/6104731.aspx

 

Let's first explain the bugs we need to improve in touch screen event processing! Then we will show you how to disable switching between landscape and landscape screens! And what is the switching process in android OS.

First look at a piece of code:

 

 

 

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

@ Override

Public boolean onTouchEvent (MotionEvent event ){

Log. v ("test", "onTouchEvent ");

BMP _y ++;

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

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

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

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

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

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

}

Return true;

// Return super. onTouchEvent (event); // Note 1

}

Public boolean onKeyDown (int keyCode, KeyEvent event ){

Log. v ("test", "onKeyDown ");

BMP _x ++;

Return super. onKeyDown (keyCode, event );

}

 

 

The code is very simple. One is to process the response time of the entity button, and the other is the response event of the touch screen. Here we will talk about two points:

First:

In surfaceview, although our onKeyDown overrides the view function, we still need to declare the get focus at initialization, setFocusable (true); if this method is not called, the key is invalid. The reason is that if you define a class that inherits the View, after the onKeyDown method is re-implemented, the onKeyDown method is called only when the View gets the focus, the onKeyDown method in Actvity is called only when all controls do not process the key event.

 

Second point:

It is also the function of the touch screen response that needs to be discussed today, onTouchEvent ()! When rewriting this function, the last sentence by default is based on the return Method of the base class, return super. onTouchEvent (event); then we determine MotionEvent in it. ACTION_MOVE, MotionEvent. ACTION_DOWN, MotionEvent. ACTION_UP corresponds to the drag, press, and lift operations on the touch screen. Everything is correct, but logs are found when the project is running. v ("Himi", "ACTION_MOVE"); here, the log "ACTION_MOVE" will never be executed !!! To solve this problem, I have found a solution to explain why such a problem occurs first.

Explanation:

OnTouchEvent (), by default, the Oeverride method is used. Normally, the system calls super. onTouchEvent () and returns the Boolean value. Note that if you call super. onTouchEvent () is likely not to do anything in super, and false is returned. Once false is returned, it is likely that the following events (such as Action_Move and Action_Up) are returned) will not receive, so in order to ensure that the event can be received after the warranty, pay attention to whether to directly call super. touchEvent ().

 

For example:

 

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

@ Override

Public boolean onTouchEvent (MotionEvent event ){

Log. I ("ConanLog", "Event" + event. getAction ());

Return super. onTouchEvent (event );

}

 

 

In this example, when you Touch Down, the event will be sent in, then the Log will be printed, and then the super onTouchEvent () will be called and the Boolean value will be returned. At this time, false will be returned, and the Touch Move or Touch Up event will no longer be received. To ensure that you can receive the event, you must return true, so pay attention to it here.

 

This problem was also found when this function was used at the time. I found a lot of information to find its explanation, so the last one when I used the onTouchEvent () function in the future

Return super. onTouchEvent (event );

Be sure to change

Return true;

 

Note: Do not forget setFocusableInTouchMode (true) during initialization; get focus in Touch Screen mode, which is similar to setFocusable (true );

-- SetFocusable (true); // This method is used to respond to keys! If you define a class that inherits the View, and implement the onKeyDown method again, the onKeyDown method is called only when the View gets the focus, the onKeyDown method in Actvity is called only when all controls do not process the key event.

 

Here we will show you how to disable horizontal and vertical screen switching!

 

In some games, we may need to disable horizontal and vertical screen switching. In fact, this requirement is very simple, as long

Android: screenOrientation = "landscape" is added to AndroidManifest. xml (landscape is horizontal and portrait is vertical ).

In android, the Activity is restarted every time the screen is switched. Therefore, you should save the status of the current Activity before the Activity is destroyed and load the configuration when the Activity is created again. Adding the android: configChanges = "keyboardHidden | orientation" attribute to the activity will not restart the activity. Instead, you will call onConfigurationChanged (Configuration newConfig). In this way, you can adjust the display mode in this method.

 

MainActivity:

 

 

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

Public void onConfigurationChanged (Configuration newConfig ){

Try {

Super. onConfigurationChanged (newConfig );

If (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_LANDSCAPE ){

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

} Else if (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_PORTRAIT ){

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

}

} Catch (Exception ex ){

}

}

 

 

In AndroidManifest. xml:

 

 

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

<? Xml version = "1.0" encoding = "UTF-8"?>

<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"

Package = "com. himi" android: versionCode = "1" android: versionName = "1.0">

<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">

<Activity android: name = ". MainActivity" android: label = "@ string/app_name"

Android: screenOrientation = "landscape" android: configChanges = "keyboardHidden | orientation">

<Intent-filter>

<Action android: name = "android. intent. action. MAIN"/>

<Category android: name = "android. intent. category. LAUNCHER"/>

</Intent-filter>

</Activity>

</Application>

<Uses-sdk android: minSdkVersion = "4"/>

</Manifest>

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.