The graph of the game touch locus in Android game development _android

Source: Internet
Author: User
Tags drawtext gety android games

This article is mainly to explain how to draw the game touch track curve.

In the Ontouchevent method, we can get the x and y coordinates of the finger touch point when touching the screen, and how to use these points to form an irregular trajectory and display this irregular trajectory curve on the screen is the gist of this article.

Android Path Class

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

Here's how to set path paths for the path class in the API.

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 "last point", approaching Control Point (x1,y1), and ending at (x2,y2).

Explain:

Parameter 1 Trajectory starting point x coordinates

Parameter 2 trajectory starting point y-coordinate

Parameter 3 trajectory end point x coordinates

Parameter 4 trajectory end point y-coordinate

So according to this parameter can set a line segment trajectory.

Step-by-Step explanation

In order to set a more sleek and good-looking curve we need to make some settings for the game brush. Comments have been written in the code very clearly, here I say in detail set brush style Mpaint.setstyle (Paint.Style.STROKE); It means to set the style of the brush the Android brush offers a total of three styles Paint.Style.STROKE, Paint.Style.FILL, and Paint.Style.FILL_AND_STROKE meaning are hollow, solid, solid and hollow, respectively. If not set, the default is Paint.Style.FILL, where it must be set to hollow. Because if you set the Cheng Shi heart or the solid and the hollow then the brush will wrap the path in the middle so it's not a curve segment, so let's take a look here.

Java code

/** Create a curve brush **/ 
mpaint = new Paint (); 
Mpaint.setcolor (color.black); 
/** sets the brush anti-aliasing **/ 
Mpaint.setantialias (true); 
The type of/** brush **/ 
mpaint.setstyle (Paint.Style.STROKE); 
/** set the brush to a sleek **/ 
mpaint.setstrokecap (Paint.Cap.ROUND); 
/** sets the width of the line **/ 
mpaint.setstrokewidth (5); 

Set the start point of the touch screen point as a trajectory in the touch press event, so that the trajectory starting point of the curve in the touch move event is the point at which the last touch point end point is the moveto. Use the Quadto method to record a curved segment produced each time a move is drawn and then draw all the curve segments in the screen, and the Reset () method resets the curve trajectory if the touch is lifted.

Java code

@Override Public 
Boolean ontouchevent (Motionevent event) { 
 /** get touch status **/ 
 int action = Event.getaction (); C4/>float x = Event.getx (); 
 Float y = event.gety (); 
 Switch (action) { 
 //Touch pressed event case 
 Motionevent.action_down: 
 /** Set curve trajectory start x y coordinate **/ 
 mpath.moveto (x, y); C11/>break; 
 Touch Moving Event Case 
 motionevent.action_move: 
 /** Set curve trajectory **/ 
 //Parameter 1 starting point x coordinate 
 //parameter 2 starting point y coordinate/ 
 /Parameter 3 End point x coordinate 
 //Parameter 4 end point y coordinate 
 mpath.quadto (MPOSX, Mposy, x, y); 
 break; 
 Touch raised event case 
 motionevent.action_up: 
 /** button lifted after the empty path trajectory **/ 
 mpath.reset (); 
 break; 
 Record the current touch x y coordinate 
 mposx = x; 
 Mposy = y; 
 return true; 

The DrawPath method is called in game drawing to draw the path curve of the ontouchevent record in the screen.

Java code

private void Draw () { 
 /** empty canvas **/ 
 mcanvas.drawcolor (color.white); 
 /** Draw Curves **/ 
 mcanvas.drawpath (MPath, mpaint); 
 /** records the current contact position **/ 
 mcanvas.drawtext ("Current stylus X:" + mposx, 0, 20,mtextpaint); 
 Mcanvas.drawtext ("Current stylus Y:" + mposy, 0, 40,mtextpaint); 

the overall implementation of the Code

Detailed comments have been written in the code Welcome to the reading oh wow, ka-ho ~ ~ ~ ~ ~

Java code

Import android.app.Activity; 
Import Android.content.Context; 
Import Android.graphics.Canvas; 
Import Android.graphics.Color; 
Import Android.graphics.Paint; 
Import Android.graphics.Path; 
Import Android.os.Bundle; 
Import android.view.MotionEvent; 
Import Android.view.SurfaceHolder; 
Import Android.view.SurfaceView; 
Import Android.view.Window; 
Import Android.view.WindowManager; 
Import Android.view.SurfaceHolder.Callback; 
 public class Surfaceviewacitvity extends activity {MyView Manimview = null; 
 @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 Full Screen display window requestwindowfeature (window.feature_no_title); 
 GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
 Displays a custom game view Manimview = new MyView (this); 
 Setcontentview (Manimview); public class MyView extends Surfaceview implements callback,runnable {/** every 50 frames refresh screen **/public static final int T Ime_in_FRAME = 50; 
 /** Game Brush **/Paint mpaint = null; 
 Paint mtextpaint = null; 
 Surfaceholder msurfaceholder = null; 
 /** Control Game Update Cycle **/Boolean mrunning = false; 
 /** game Canvas **/Canvas mcanvas = null; 
 /** Control Game Loop **/Boolean misrunning = false; 
 /** curve Direction **/private Path MPath; 
 Private float mposx, mposy; 
  Public MyView {Super (context); 
  /** sets the current view to have control focus **/this.setfocusable (true); 
  /** sets the current view to have the touch event **/This.setfocusableintouchmode (TRUE); 
  /** get Surfaceholder object **/Msurfaceholder = This.getholder (); 
  /** adds Msurfaceholder to the callback callback function **/Msurfaceholder.addcallback (this); 
  /** Create canvas **/Mcanvas = new Canvas (); 
  /** Create a curve brush **/mpaint = new Paint (); 
  Mpaint.setcolor (Color.Black); 
  /** sets the brush anti-aliasing **/Mpaint.setantialias (true); 
  The type of/** brush **/mpaint.setstyle (Paint.Style.STROKE); 
  /** set the brush to a sleek **/mpaint.setstrokecap (Paint.Cap.ROUND); 
  /** sets the width of the line **/Mpaint.setstrokewidth (5); 
 /** Create Path object **/MPath = new Path (); /** Create a text brush **/mtextpaint = new Paint (); 
  /** Set color **/mtextpaint.setcolor (color.black); 
 /** set Text Size **/mtextpaint.settextsize (15); 
  @Override public boolean ontouchevent (Motionevent event) {/** get touch status **/int action = Event.getaction (); 
  float x = Event.getx (); 
  Float y = event.gety (); 
  Switch (action) {//Touch pressed event case Motionevent.action_down:/** set curve trajectory start x y coordinate **/mpath.moveto (x, y); 
  Break Touch Moving Event Case motionevent.action_move:/** set curve trajectory **///parameter 1 starting point x Coordinate//parameter 2 starting point y coordinate//Parameter 3 end point x coordinate//parameter 4 end point y sit 
  Superscript mpath.quadto (Mposx, Mposy, x, y); 
  Break 
  Touch raised event case MOTIONEVENT.ACTION_UP:/** button lifted after the empty path trajectory **/mpath.reset (); 
  Break 
  ///record current touch x y coordinate mposx = x; 
  Mposy = y; 
 return true; 
  private void Draw () {/** empty canvas **/mcanvas.drawcolor (color.white); 
  /** Draw Curves **/Mcanvas.drawpath (MPath, mpaint); 
  /** records the current contact position **/Mcanvas.drawtext ("Current stylus X:" + mposx, 0, 20,mtextpaint); Mcanvas.drawtext ("Current stylus Y:" + mposy, 0, 40,mtextpaint); 
 @Override public void surfacechanged (surfaceholder holder, int format, int width, int height) {} @Override 
  The public void surfacecreated (Surfaceholder holder) {/** starts the game main loop thread **/misrunning = true; 
 New Thread (This). Start (); 
 @Override public void surfacedestroyed (Surfaceholder holder) {misrunning = false; @Override the time before the public void run () {while (misrunning) {/** Gets the update game **/long starttime = System.currenttimem 
  Illis (); 
   /** here plus the line Cheng Ann full lock **/synchronized (msurfaceholder) {/** Get the current canvas and lock **/Mcanvas = Msurfaceholder.lockcanvas (); 
   Draw (); 
  The/** is displayed on the screen after the drawing is finished **/msurfaceholder.unlockcanvasandpost (Mcanvas); 
  /** get update game end time **/long endtime = System.currenttimemillis (); 
  /** calculates the number of milliseconds the game updates **/int difftime = (int) (endtime-starttime); /** ensure that each update time is 50 frames **/while (difftime <= time_in_frame) {difftime = (int) (System.currenttimemillis ()-Starttim 
  e); /** thread waits for **/Thread.yield ();  } 
  } 
 } 
 } 
}

Read and mastered these code examples, I believe that we have a way to draw the game to touch the trajectory of the understanding of the method, I hope everyone in the development of Android games to use them freely.

The above is a small series to draw the game track curve of the data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.