Android game development: an example of switching pictures by gesture operation

Gestures for Android are often used not only in software, such as turning pages and scrolling pages in browsers; Of course, when we develop Android games, adding Android gesture operation will add a bright spot to the game. For example, for general CAG, PUZ and other types of games, you can use gestures to operate level selection, simple background movement, etc. similar to the popular angry birds some time ago, the game of birds is really good, The only highlight I see is the creativity of this game! To tell you the truth, there are no games that can't be made, only good ideas that can't be thought of. Back to the topic, let's learn what Android gestures are!

Overview of gesture recognition

The so-called gesture operation is similar to the dance machine and ezdancer, which use different actions and notes to make people dance. The gesture here on Android only gives us more tricks and playing methods in the operation of games and software. It is packaged according to the length of time the player touches the screen, the sliding distance on the screen, the pressing and lifting time, etc, In fact, Android has packaged and processed the touch screen processing.

There are actually two gesture recognition technologies in Android. One is touch screen gesture recognition, the other is input method gesture recognition. Compared with the two, the second is more flexible. You can customize gestures, which is high! In this section, we first introduce the first gesture recognition: touch screen gesture recognition. In the next blog post, I will explain input method gesture recognition to children's shoes!

Gesture recognition example

Let's put up two screenshots first:

OK, code first:

MySurfaceView.java

Java code

Add: when the code initializes the gesture, there is such a sentence: GD. Setislongpressenabled (true); This function indicates that if you set true, the long key is turned on. When you don't touch the screen for a long time, you can get the onlongpress gesture. If you set false, you can't get the support of this gesture if you don't touch the screen for a long time. This function is not set and defaults to true.

Note 1:

Here I just give a brief introduction to some children's shoes that are not familiar with this way of defining vector: generally, when we define a container, we directly define vector VC = new vector();, Well, yes, but the definition of vector < string > is a generic definition. Let's simply say the difference if vector VC = new vector(); After the object is loaded in this way, do you want to forcibly convert the extracted type when fetching?! Hehe, the definition of vector < string > indicates that the container only contains elements of string type, so~ there is no need to force it when taking it out.

Note 2:

Through the test, it is found that the touch screen event is still responded here. Even if you set the touch screen focus to setfocusableintouchmode (false), it will be called! The reason is that our view of this class is bound to a touch-screen event listener, so we will respond to note 3 first, and then note 4. Instead of returning true, we directly return it to the gesture listener to listen. Let the listener find an appropriate function to process the user's gesture, that is, there is no sign that the processing is completed, so our rewritten ontouchevent () will continue to process it!

Note 5:

The code annotated here is to test which two actions are, because the online Posts introducing Android gestures are crazy:

The first is motionevent.action_ Down, the second is motionevent.action_ MOVE。 Then the first action is to press to understand. It is the action that the player just touched the screen. The second is move! Are the moving points recorded??

In fact, the test results show that:

The first is motionevent.action_ Down, the second is motionevent.action_ UP!

Alas ~ now the posts on the Internet are really all kinds of plagiarism ~ can't you test?? Depressed! Since one of these two actions is pressing and the other is lifting, its meaning is very clear. We can know the user's sliding distance and so on according to these two actions. The distance E2. Getx() - E1. Getx().

summary

1. After touching the screen, the screen will not move all the time, and the evolution sequence: ondown - > onshowpress - > onlongpress;

2. After touching the screen, always touch the screen, slow movement is onscroll / fast movement is onfling, and fingers leave the screen;

Note: after touching the screen, always touch the screen. If your fingers don't leave the screen, it will always be onscroll. No matter how fast you move, it will never be onfling!

OK, although the gesture is very simple, it will certainly add a lot to your game if you use it skillfully and join the game.

In this example, I only did one gesture, because other actions are very simple, so I won't say more.

Supplementary contents:

Many articles on gestures on the Internet say that Android's support for gestures began with SDK 1.6 (that is, API 4), but I can recognize it with sdk1.5 simulator! (I wanted to test the support effect of a lower SDK, but I didn't have an SDK lower than version 1.5), so I checked the API and found:

android.view.GestureDetector.OnGestureListener; since api-1,

android.view.GestureDetector; since api-1,

From the perspective of API, gestures and gesture listeners have been supported since API-1, so many people are right to say that api-4 supports gestures! This is because: android.gesture has been supported since api-4. This class will be used in gesture recognition.

Conclusion: touch screen gesture recognition has been supported since API-1. The input method gesture recognition is only supported by api-4! We need to find out here!

The above is the data sorting of Android gesture operation switching pictures. We will continue to supplement relevant data in the future. Thank you for your support for this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>