Example code of Bezier curve drawing method in Android
Bessel curve, many people may not understand, what is called Bessel curve? Here's a brief introduction: Bezier curve can also be called Bezier curve or Bezier curve. It is composed of segments and nodes. Nodes are draggable fulcrums, and segments are like retractable rubber bands. General vector graphics software often uses Bezier curve to accurately draw the curve.
In the above introduction, "line segments are like retractable rubber bands" is very key, but it is also very easy to understand. As for the details of Bessel curve, you can refer to relevant materials.
Bezier curve drawing interface provided by Android
In Android development, it is actually very simple to implement Bezier curve, because Android has provided us with relevant interfaces, but this interface method is hidden a little deep in the path class. This method is as follows:
android.graphics.Path.quadTo(float x1,float y1,float x2,float y2)
Since: API Level 1
Parameter Description:
X1: X coordinate of operation point
Y1: Y coordinate of operation point
X2: X coordinate of the end point
Y2: Y coordinate of the end point
It can be seen from API that Bezier curve has been supported since API-1.
Drawing example of Android Bezier curve
After you are familiar with the method, you can implement it as follows:
Surfaceview framework is not much to talk about. Anyone who has read my blog should know.
Directly look at the mysurfaceview class, which inherits the surfaceview and is the main view of the game.
Here for a clearer explanation: some of the codes here will not be posted first, and finally will be posted as a whole.
First, define relevant member variables:
Java code
Constructor of this class:
Java code
Then I encapsulated the drawing of Bezier curve into a method, and the function is as follows:
Java code
Finally, the user touch screen monitoring function and logic function:
Java code
The whole code is easy, mainly the parameters of Bessel function, especially the operation points. Different operation points can achieve different effects. Here, I simply and uniformly say that the operation points are set to half of the X and y of the user's touch screen points. Hehe, I'm lazy~~
I write Bessel's operation points in the logic () function, execute them continuously, and use nextint function to get random operation points every time, mainly to make its curve change continuously, so as to form a vibrating curve trajectory.
The screenshot of operation effect is as follows:
Here, the effect may not be obvious because the picture is still. You can run the source code to observe it.
The source code of the whole mysurfaceview is posted below:
Java code
The above is the sample code for drawing Android Bezier curve. We will continue to supplement relevant knowledge in the future. Thank you for your support for this site!