The example of this article introduces how Android can draw a touch path, and share it for everyone's reference, the details are as follows
Effect Chart:
Implementation code:
Package com.android.gameview5;
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.SurfaceHolder.Callback;
Import Android.view.SurfaceView;
Import Android.view.Window;
Import Android.view.WindowManager;
public class SurfaceViewActivity3 extends activity {public void onCreate (Bundle s) {super.oncreate (s);
Full Screen display requestwindowfeature (window.feature_no_title);
GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Setcontentview (This) (new MyView);
The public class MyView extends Surfaceview implements callback,runnable{public static final int time_in_frame = 50;
Paint mpaint = null;
Paint mtextpaint = null;
Surfaceholder msurfaceholder = null; Boolean mrunning = false;
Canvas Mcanvas = null;
Private Path MPath;
private float Mposx,mposy;
Public MyView {Super (context); this.setfocusable (true); This.setfocusableintouchmode (true);
Msurfaceholder = This.getholder ();
Msurfaceholder.addcallback (this);
Mcanvas = new Canvas ();
Mpaint = new Paint ();
Mpaint.setcolor (Color.Black);
Mpaint.setantialias (TRUE);
Mpaint.setstyle (Paint.Style.STROKE);
Mpaint.setstrokecap (Paint.Cap.ROUND);
Mpaint.setstrokewidth (6);
MPath = new Path ();
Mtextpaint = new Paint ();
Mtextpaint.setcolor (Color.Black);
Mtextpaint.settextsize (15);
public boolean ontouchevent (Motionevent event) {int action = event.getaction ();
float x = Event.getx ();
Float y = event.gety ();
Switch (action) {case MotionEvent.ACTION_DOWN:mPath.moveTo (x, y);
Break
Case MotionEvent.ACTION_MOVE:mPath.quadTo (MPOSX, Mposy, x, y);
Break
Case MOTIONEVENT.ACTION_UP: Mpath.reset ();
Break
//record current touch point coordinates mposx = x;
Mposy = y;
return true; private void OnDraw () {mcanvas.drawcolor (color.white);//Draw Curve Mcanvas.drawpath (MPath, Mpaint); Mcanvas.drawtext ("
Current stylus x: "+mposx,0,20,mtextpaint";
Mcanvas.drawtext ("Current Stylus y:" +mposy,0,40,mtextpaint);} public void Run () {//TODO auto-generated method stub while (mrunning) {Long starttime = System.currenttimemillis (); synch Ronized (msurfaceholder) {Mcanvas = Msurfaceholder.lockcanvas (); OnDraw (); Msurfaceholder.unlockcanvasandpost (
Mcanvas);
Long endtime = System.currenttimemillis ();
int difftime = (int) (endtime-starttime);
while (difftime<=time_in_frame) {difftime = (int) (System.currenttimemillis ()-starttime);
Thread.yield (); @Override public void surfacechanged (surfaceholder holder, int format, int width, int height) {//TODO Auto-genera
Ted Method stub} @Override public void surfacecreated (Surfaceholder holder) {mrunning = true; new Thread (This). Start (); } @Override public void SURFACEdestroyed (Surfaceholder holder) {//TODO auto-generated method Stub mrunning = false;}
}
}
The above is Android easy to draw a touch track of the specific method, I hope to help you learn.