Android轻松画出触摸轨迹

Android轻松画出触摸轨迹,第1张

概述本文实例介绍了Android如何画出触摸轨迹的方法,分享给大家供大家参考,具体内容如下

本文实例介绍了AndroID如何画出触摸轨迹的方法,分享给大家供大家参考,具体内容如下

效果图:

实现代码:

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);  //全屏显示  requestwindowFeature(Window.FEATURE_NO_Title);  getwindow().setFlags(WindowManager.LayoutParams.FLAG_FulLSCREEN,WindowManager.LayoutParams.FLAG_FulLSCREEN);  setContentVIEw(new MyVIEw(this));  }  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(Context context){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;  }  //记录当前触摸点得当前得坐标  mPosX = x;  mPosY = y;return true;}private voID onDraw(){mCanvas.drawcolor(color.WHITE);//绘制曲线mCanvas.drawPath(mPath,mPaint);mCanvas.drawText("当前触笔X:"+mPosX,20,mTextPaint);mCanvas.drawText("当前触笔Y:"+mPosY,40,mTextPaint);}public voID run() {// Todo auto-generated method stubwhile(mRunning){long startTime = System.currentTimeMillis();synchronized(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();}}}@OverrIDepublic voID surfaceChanged(SurfaceHolder holder,int format,int wIDth,int height) {// Todo auto-generated method stub}@OverrIDepublic voID surfaceCreated(SurfaceHolder holder) {mRunning = true;new Thread(this).start();}@OverrIDepublic voID surfaceDestroyed(SurfaceHolder holder) {// Todo auto-generated method stubmRunning = false;}    }}

以上就是AndroID轻松画出触摸轨迹的具体方法,希望对大家的学习有所帮助。

总结

以上是内存溢出为你收集整理的Android轻松画出触摸轨迹全部内容,希望文章能够帮你解决Android轻松画出触摸轨迹所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149612.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存