On Android, how does one use the besell curve?

Source: Internet
Author: User
Tags gety

First of all, for children's shoes that are not very familiar with the bell curve, Please consciously white encyclopedia, google.

To facilitate the lazy kids shoes, here is an encyclopedia address of "besell curve" and a brief description of "besell curve":

Encyclopedia of the whiteness of the betel curve fast address: http://baike.baidu.com/view/4019466.htm

The beiser curve, also known as the beiz curve or the bezié curve, is usually used by vector graphics software to accurately draw the curve. The Bez curve consists of line segments and nodes, and nodes are the pivot points that can be dragged, a line segment is a scalable rubber band.

In fact, the above paragraph is quite important and easy to understand as the line segment is like a scalable rubber band.

As for the implementation of the besell curve, it is extremely simple in Android, because it is a method encapsulated by Android, can this be simple ...... But it is hidden in depth, and it is hidden in the Path class. The method is as follows:

Android. graphics. Path. quadTo (float x1, float y1, float x2, float y2)

Since: API Level 1

Description of this parameter:

First parameter: x coordinate of the Operation Point

Second parameter: y coordinate of the Operation Point

Third parameter: x coordinate of the end point

Fourth parameter: y coordinate of the end point

From the API, we can see that the bell curve is supported from the API-1;

After you are familiar with the method, we will implement the following:

There are not many SurfaceView frameworks to talk about. I should know all those who have read my blog;

Directly look at the MySurfaceView class. This class inherits SurfaceView, which is the main view of the game.

Here for a clearer explanation: Some of the Code will not be posted first, but will be posted at last. Of course, the source code is also provided at the end for free ~

First, define the relevant member variables:

// Member variable of the besell curve (starting point, control (Operation Point), end point, and coordinate of three points)
Private int startX, startY, controlX, controlY, endX, endY;
// Path
Private Path path;
// In order not to affect the main paint brush, use a new paint brush to draw the besell curve.
Private Paint paintQ;
// Random library (making the besell curve more obvious)
Private Random random;

Constructor of this class:

/**
* SurfaceView initialization Function
*/
Public MySurfaceView (Context context ){
Super (context );
...
// Initialize the besell Curve
Path = new Path ();
PaintQ = new Paint ();
PaintQ. setAntiAlias (true );
PaintQ. setStyle (Style. STROKE );
PaintQ. setStrokeWidth (5 );
PaintQ. setColor (Color. WHITE );
Random = new Random ();
...
}

Then I encapsulate a method for drawing the besell curve. The function is as follows:

/**
* Draw the besell Curve
*
* @ Param canvas main canvas
*/
Public void drawQpath (Canvas canvas ){
Path. reset (); // reset path
// Start point of the besell Curve
Path. moveTo (startX, startY );
// Set the operation and termination points of the besell Curve
Path. quadTo (controlX, controlY, endX, endY );
// Draw the besell curve (Path)
Canvas. drawPath (path, paintQ );
}

Finally, the user's touch screen listening functions and logical functions are as follows:

/**
* Touch screen event monitoring
*/
@ Override
Public boolean onTouchEvent (MotionEvent event ){
EndX = (int) event. getX ();
EndY = (int) event. getY ();
Return true;
}
/**
* Game logic
*/
Private void logic (){
If (endX! = 0 & endY! = 0 ){
// Set the operation point to half of line x/y.
ControlX = random. nextInt (endX-startX)/2 );
ControlY = random. nextInt (endY-startY)/2 );
}
}

The entire code is very easy ~ It is mainly the parameters of the besell function, especially the operation points. Different operation points can achieve different effects. Here I will set the operation points to the x of the user's touch screen point in a simple and unified manner, half of y ~ Xi ~

I wrote the operation points of besell in the logic () function and continued to execute it. Each time I used the nextInt function to get a random operation point, the main purpose is to keep the curve changing to form a vibrating curve trajectory;

OK. The effect is shown in the following figure:

 

 

 

 

This may be because the picture is static and does not seem very obvious. You can run the source code to observe it ~ This is the case in this section. The source code of the entire MySurfaceView is shown below: (the source code of this project is finally provided)

Package com. qpath;
 
Import java. util. Random;
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. Paint. Style;
Import android. graphics. Path;
Import android. view. KeyEvent;
Import android. view. MotionEvent;
Import android. view. SurfaceHolder;
Import android. view. SurfaceHolder. Callback;
Import android. view. SurfaceView;
 
/**
* SABEL Curve
*
* @ Author Himi
*
*/
Public class MySurfaceView extends SurfaceView implements Callback, Runnable {
Private SurfaceHolder sfh;
Private Paint paint;
Private Thread th;
Private boolean flag;
Private Canvas canvas;
Public static int screenW, screenH;
// ----------- The above is the SurfaceView game framework
// Member variable of the besell curve (starting point, control (Operation Point), end point, and coordinate of three points)
Private int startX, startY, controlX, controlY, endX, endY;
// Path
Private Path path;
// In order not to affect the main paint brush, use a new paint brush to draw the besell curve.
Private Paint paintQ;
// Random library (making the besell curve more obvious)
Private Random random;
 
/**
* SurfaceView initialization Function
*/
Public MySurfaceView (Context context ){
Super (context );
Sfh = this. getHolder ();
Sfh. addCallback (this );
Paint = new Paint ();
Paint. setColor (Color. WHITE );
Paint. setAntiAlias (true );
SetFocusable (true );
// ----------- The above is the SurfaceView game framework
// Initialize the besell Curve
Path = new Path ();
PaintQ = new Paint ();
PaintQ. setAntiAlias (true );
PaintQ. setStyle (Style. STROKE );
PaintQ. setStrokeWidth (5 );
PaintQ. setColor (Color. WHITE );
Random = new Random ();
}
 
/**
* SurfaceView is created in response to this function
*/
Public void surfaceCreated (SurfaceHolder holder ){
ScreenW = this. getWidth ();
ScreenH = this. getHeight ();
Flag = true;
// Instance thread
Th = new Thread (this );
// Start the thread
Th. start ();
// ----------- The above is the SurfaceView game framework
}
 
/**
* Game plotting
*/
Public void myDraw (){
Try {
Canvas = sfh. lockCanvas ();
If (canvas! = Null ){
Canvas. drawColor (Color. BLACK );
// ----------- The above is the SurfaceView game framework
DrawQpath (canvas );
}
} Catch (Exception e ){
// TODO: handle exception
} Finally {
If (canvas! = Null)
Sfh. unlockCanvasAndPost (canvas );
}
}
 
/**
* Draw the besell Curve
*
* @ Param canvas
* Main canvas
*/
Public void drawQpath (Canvas canvas ){
Path. reset (); // reset path
// Start point of the besell Curve
Path. moveTo (startX, startY );
// Set the operation and termination points of the besell Curve
Path. quadTo (controlX, controlY, endX, endY );
// Draw the besell curve (Path)
Canvas. drawPath (path, paintQ );
}
 
/**
* Touch screen event monitoring
*/
@ Override
Public boolean onTouchEvent (MotionEvent event ){
EndX = (int) event. getX ();
EndY = (int) event. getY ();
Return true;
}
 
/**
* Game logic
*/
Private void logic (){
If (endX! = 0 & endY! = 0 ){
// Set the operation point to half of line x/y.
ControlX = random. nextInt (endX-startX)/2 );
ControlY = random. nextInt (endY-startY)/2 );
}
}
 
/**
* Button event monitoring
*/
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
Return super. onKeyDown (keyCode, event );
}
 
Public void run (){
While (flag ){
Long start = System. currentTimeMillis ();
MyDraw ();
Logic ();
Long end = System. currentTimeMillis ();
Try {
If (end-start <50 ){
Thread. sleep (50-(end-start ));
}
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
}
 
/**
* The SurfaceView status changes and responds to this function.
*/
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height ){
}
 
/**
* Response to this function when SurfaceView disappears
*/
Public void surfaceDestroyed (SurfaceHolder holder ){
Flag = false;
}
}

Related Article

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.