时间:2021-05-20
本文实例介绍了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,0,20,mTextPaint);mCanvas.drawText("当前触笔Y:"+mPosY,0,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轻松画出触摸轨迹的具体方法,希望对大家的学习有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了android绘制触点轨迹的具体代码,供大家参考,具体内容如下重点函数是onTouchEvent(),所有的触摸事件都会在View的这个函数
本文分享了解决SurfaceView触摸轨迹闪烁问题的方法,供大家参考,具体内容如下第一种解决SurfaceView触摸轨迹闪烁问题的方法:由于SurfaceV
本篇文章主要来讲解怎样绘制游戏触摸轨迹的曲线图。我们在onTouchEvent方法中,可以获取到触摸屏幕时手指触摸点的x、y坐标,如何用这些点形成一条无规则轨迹
前言上一篇讲了Android触摸事件的传递机制,具体可以看这里初识Android触摸事件传递机制。既然知道Android中触摸事件的传递分发,那么它能解决什么样
本篇文章主要介绍了Android利用三阶贝塞尔曲线绘制运动轨迹的示例,分享给大家,具体如下:实现点赞效果,自定义起始点以及运动轨迹效果图:xml布局:MainA