Android game development custom gesture — input method gesture Technology

In software development, we usually like to use newer versions of tools, but why do I use lower versions of SDK to develop Android games? Here are the reasons:

1. Android SDK is backward compatible! So the low version can run, and the high version is basically no problem! (of course, every SDK update will bring new functions, or modify some original bugs, etc. in fact, for game development, if you don't need the support of a higher SDK version in your game, you don't have to pursue the latest SDK at all!)

2. Use the lower version for game development, which can take into account more models and obtain more users!

3. As we all know, each time the Android SDK version is updated, the underlying code will be more robust and optimized! For example, the Android version of our company's online game runs slightly on G2 (sdk1.5), while it runs smoothly on my mobile phone (sdk2.2) ~ all kinds of comfort ~ ~ but this will also bring some disadvantages. For example, if our own games are developed with a higher version of SDK, how about the performance and memory, We can't easily see the effect. If we use a lower version of the SDK, we will obviously feel how the performance is ~ think about it. If your game runs smoothly on 1.5 and 1.6, it's even better to put it on a higher version of the SDK machine~

Summary: in game development, if your game does not need higher API support, it is recommended to develop based on SDK 1.5 and 1.6!

In the last article, I introduced the touch screen gesture operation, but this kind of touch screen gesture operation has limitations; For example, we all know that Android can use gestures to unlock, similar to the form of Jiugong grid, through @ R_ 419_ A single stroke gesture of 2410 @ can unlock the screen and @ R_ 419_ 2410 @ stroke gesture to start an application, so this so-called stroke gesture is actually the input method gesture recognition technology I want to explain to you today! This gesture is that we can @ r by ourselves_ 419_ 2410 @, unlike the previous touch-screen gesture operation, it just uses Android to encapsulate some touch-screen actions. Here are some mobile phones @ r_ 419_ 2410 @ screenshot of stroke gesture unlocking:

OK, since we can use gestures to unlock and other operations, we can add this highlight in game development. For example, in the game, I draw a circle to change the background, draw an X to exit the game, etc. haha, does it feel very interesting? OK, let's start the explanation!

First of all, this chapter mainly studies two points:

1. How to create input method gestures, delete input method gestures, and read gesture files from SD card!

2. How to match our @ r after the input method gesture is created_ 419_ 2410 @ gesture!

Let's familiarize ourselves with two classes and several concepts:

1. What is gestureoverlayview? Simply put, it is actually a handwritten drawing area;

2. What is gesturelibrary? This class is used to save and delete gestures. It is a small warehouse for storing gestures!

3. What are strokes, fonts, strokes? Yes, it's actually a concept with the strokes we write!

4. What is stroke type? There are two types of strokes in gesture operation of input method; One is single stroke, the other is multi stroke

The so-called single stroke is to draw a gesture with one stroke. From the moment your finger touches the screen to when you leave the screen, the stroke will immediately form a gesture! make smooth reading!

Multiple strokes can be any number of strokes within a certain compact time, and then a gesture will be formed after this compact time!

Take out the screenshot of the project first, and briefly describe its functions and operations:

Figure 1 the interface is divided into three parts, from top to bottom: textview, EditText and surfaceview; Then there is a gestureoverlay view covering the full screen behind the surfaceview!

The interface in Figure 2 is the interface for matching gestures in the created gestures. It can be seen clearly here. It's right to find ~ hehe~

Take a look at main xml:

XML / HTML code

We @ r are registered in XML_ 419_ 2410 @ surfaceview. I'm not familiar with it. You can take a look at Android game development 6. I don't have much explanation. For gestureoverlayview, the width and height are simply defined here. Some important properties are set in the code. Of course, XML can also be set.

Let's look at mainactivity java:

Java code

This is the main code of mainactivity, in which I have written functions to add gestures, match gestures, traverse gestures and turn gestures into pictures, so as to make your children's shoes clearer.

From the above code, we can see that before creating gestures, the gestureoverlay view must be created first, and then we can carry out Stroke Painting gestures in its area. Of course, before painting gestures, we also need to set the stroke type, which I introduced to you at the beginning, Then, the most important thing is to bind the gesture listener in the handwriting drawing area. Add ongesturelistener. This listener rewrites four functions. Here are the most important two functions:

Ongesturestarted and ongestureended: monitoring functions for gesture start and gesture end.

In particular, the function of gesture end monitoring is particularly important. I set several conditional statements. On the one hand, these conditions let you know some important and commonly used methods in gesture. On the other hand, I would like to remind you of children's shoes:

If you set the stroke type as multi stroke type, ideally, the system should create the hand gesture in a compact period of time, no matter how many strokes you use to draw the gesture, and the system should respond to this function only when it determines that you have no more strokes in a certain short period of time;

In fact, it's wrong. I thought so at the beginning, but I found that no matter whether the stroke type you set is single or multi stroke, when your finger leaves the screen, Android will respond to this completion function no matter what stroke you currently have. So~ I call the getstrokescount() function in the gesture gesture class here, This function will record the number of strokes you draw a gesture in a compact time, so according to this function, we can solve the problem that the finger is always responded when leaving the screen, because this value will never be greater than 1 for a single stroke type!

If (event. Getaction() = = motionevent. Action_up) {} This is just to show you how to use the second parameter key action.

So let's see how to create a gesture:

Java code

It is also well understood here. The routine is similar to the routine stored in the file file before. First judge whether the SD exists, and then whether the file exists:

If the file does not exist, first add it directly to the gesture warehouse, and then the gesture warehouse calls gesturelib Save() saves the gesture to the gesture file on the SD card.

If the file exists, it is also necessary to determine whether the file contains gestures with the same name; Of course, it is not necessary to determine whether the same gesture name exists here, and then delete it! In fact, you can directly add the current new gesture without deleting it; The reason is clear after reading the notes below;

Note 1: because the gesture saved by gesturelib is a HashMap, key = gesture name and value = gesture, gesturelib removeGesture(name,gesture); This deletion method only deletes the gesture. The name of the gesture is still saved in HashMap. The HashMap will directly overwrite this entry when the same name gesture is saved next time. Therefore, according to the characteristics of HashMap, we can directly extract lib without deleting addGesture(name,gesture); Because if a gesture with the same gesture name appears, the HashMap will directly overwrite the value drop of its entry according to the key (gesture name)~

Note 2: This is also a way to delete gestures, but this method is different from note 1. Here, the entries in the HashMap are deleted, that is, the key and value are deleted!

Here's how to turn a gesture into a bitmap:

Java code

Here is how to traverse gestures:

Java code

Finally, let's look at the matching of gestures:

Java code

Finally, let's talk to children's shoes. In fact, the input method gesture operation is very suitable for use in the game. Whether it's the touch screen gesture operation or the input method gesture operation mentioned today, it's quite good if added to the game!

The above is for Android @ R_ 419_ 2410 @ gesture implementation example, continue to sort out relevant materials in the future, thank you for your support to 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
分享
二维码
< <上一篇
下一篇>>