Drawing the curve of game touch trajectory in Android game development

This article mainly explains how to draw the curve of game touch trajectory.

In the ontouchevent method, we can obtain the X and Y coordinates of the finger touch points when touching the screen. How to use these points to form an irregular track and display the irregular track curve on the screen is the main content of this article.

Android path class

Android provides a path class. As the name suggests, this class can set curve path trajectory. Any irregular curve is actually composed of several line segments, and the definition of the line segment is the shortest line between two points. Path class can record the trajectory between these two points, so several paths are the irregular curves we need to draw.

The following describes how the path class in the API sets the track path.

public class

Path extends Object java.lang.Object android.graphics.Path

quadTo(float x1,float y1,float x2,float y2) Add a quadratic bezier from the last point,approaching control point (x1,y1),and ending at (x2,y2).

Explanation:

Valuepoint 1 x coordinate of track starting point

Parameter 2 y coordinate of track starting point

Valuepoint 3 x coordinate of track end point

Valuepoint 4 y coordinate of track end point

Therefore, a line segment track can be set according to this parameter.

Step by step explanation

In order to set a smooth and beautiful curve, we need to set some settings for the game brush. The comments have been clearly written in the code. Here I'll talk about setting the brush style mpaint. Setstyle (paint. Style. Stroke); It means to set the style of the brush. Android brush provides three styles: paint.style.stroke, paint.style.fill and paint.style.fill_ AND_ Stroke means hollow, solid, solid and hollow. If it is not set, it defaults to paint.style.fill, which must be set to hollow here. Once it is set to solid or solid and hollow, the brush will wrap the path in the middle, so it is not a curve segment, so please pay attention here.

Java code

In the touch press event, set the touch screen point as the starting point of the track through the moveto() method. In this way, in the touch move event, set the track starting point of the curve as the last touch point and the end point as the current touch point. Use the quadto method to record a curve segment generated by each movement, and then draw all curve segments on the screen. If you touch the lift, you will call the reset () method to reset the curve track.

Java code

In the game drawing, we call the drawPath method to draw the path curve recorded in onTouchEvent in the screen.

Java code

Overall implementation of code

Detailed comments have been written in the code. Welcome to read wow Kaka~~~~

Java code

After understanding and mastering these code examples, I believe you have a methodological understanding of how to draw the curve of game touch trajectory. I hope you can freely use them in Android game development.

The above is Xiaobian's data sorting for drawing the curve of the game touch track. We will continue to supplement relevant data 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
分享
二维码
< <上一篇
下一篇>>