解决Android SurfaceView绘制触摸轨迹闪烁问题的方法
本文分享了解决SurfaceView触摸轨迹闪烁问题的方法,供大家参考,具体内容如下
第一种解决SurfaceView触摸轨迹闪烁问题的方法:
由于SurfaceView使用双缓存机制,两张画布轮流显示到屏幕上。那么,要存储触摸轨迹并避免两张画布内容不一致造成的闪烁问题,完全可以利用保存绘制过程并不断重新绘制的方法解决闪烁,而且这样还顺带解决了多次试验中偶尔出现的因为moveTo()函数不能读取到参数执行默认设置(参数设为上次的触摸点)而出现的断线连接闪烁问题,详细代码如下:
packagecom.tobacco.touchdraw; importjava.util.ArrayList; importandroid.content.Context; importandroid.graphics.Canvas; importandroid.graphics.Color; importandroid.graphics.Paint; importandroid.graphics.Path; importandroid.view.MotionEvent; importandroid.view.SurfaceHolder; importandroid.view.SurfaceView; importandroid.view.View; importandroid.view.SurfaceHolder.Callback; importandroid.view.View.OnTouchListener; publicclassLSurfaceViewextendsSurfaceViewimplementsCallback,OnTouchListener,Runnable{ privateSurfaceHoldersfh; privateCanvascanvas; privatePaintpaint; privatePathpath; privateArrayList<Path>paths; privatebooleanflag; publicLSurfaceView(Contextcontext){ super(context); sfh=this.getHolder(); sfh.addCallback(this); paint=newPaint(); paint.setColor(Color.RED); paint.setAntiAlias(true); paint.setStrokeWidth(4); paint.setStyle(Paint.Style.STROKE); paint.setStrokeCap(Paint.Cap.ROUND); paths=newArrayList<Path>(); path=newPath(); } publicvoidmyDraw(MotionEvente){ intaction=e.getAction(); switch(action){ caseMotionEvent.ACTION_DOWN: path.moveTo(e.getX(),e.getY()); break; caseMotionEvent.ACTION_MOVE: path.lineTo(e.getX(),e.getY()); break; caseMotionEvent.ACTION_UP: //path.close(); Pathpath1=newPath(path); paths.add(path1); path.reset(); break; } } @Override publicvoidsurfaceChanged(SurfaceHolderarg0,intarg1,intarg2,intarg3){ } @Override publicvoidsurfaceCreated(SurfaceHolderholder){ flag=true; setOnTouchListener(this); newThread(this).start(); } @Override publicvoidsurfaceDestroyed(SurfaceHolderholder){ flag=false; } @Override publicbooleanonTouch(Viewv,MotionEventevent){ myDraw(event); returntrue; } @Override publicvoidrun(){ while(flag){ longstart=System.currentTimeMillis(); canvas=sfh.lockCanvas(); if(canvas!=null){ canvas.drawColor(Color.BLACK); for(inti=0;i<paths.size();i++) canvas.drawPath(paths.get(i),paint); canvas.drawPath(path,paint); sfh.unlockCanvasAndPost(canvas); } longend=System.currentTimeMillis(); try{ if(end-start<30){ Thread.sleep(30-(end-start)); } } catch(Exceptione){ } } } }
这里还要注意的是:ArrayList保存的是对象的引用,所以要在每次添加时都新建一个对象实体。
第二种解决SurfaceView触摸轨迹闪烁问题的方法:
处理触屏轨迹的绘制时,用到了SurfaceView,建立Path对象,在点击时开始设置Path对象,滑动过程中记录触摸点,离开后重新设置Path对象,因不能阻塞主线程,所以新建了一个子线程来不断刷新屏幕,也就是将path不断绘制。但是,接着就出现了一个问题:屏幕中每条轨迹线的终点都会有一小段直线段不断闪烁。猜测可能是lockCanvas()获取的对象区域不一样,就试着使用了lockCanvas(Rectre),但是,运行后发现还是没有解决问题;接着想到可能是因为每次lockCanvas()后获取的对象不同,就在主线程中添加了一个Canvas对象,每次都在Canvas对象中修改画面,然后提交显示,但是,程序运行后效果丝毫没有改变!程序代码如下:
packagecom.tobacco.touchdraw; importandroid.content.Context; importandroid.graphics.Canvas; importandroid.graphics.Color; importandroid.graphics.Paint; importandroid.graphics.Path; importandroid.view.MotionEvent; importandroid.view.SurfaceHolder; importandroid.view.SurfaceView; importandroid.view.View; importandroid.view.SurfaceHolder.Callback; importandroid.view.View.OnTouchListener; publicclassMySurfaceViewextendsSurfaceViewimplementsCallback,OnTouchListener,Runnable{ privateSurfaceHoldersfh; privateCanvascanvas; privatePaintpaint; privatefloatlastX,lastY; privatePathpath; privatebooleanflag; publicMySurfaceView(Contextcontext){ super(context); sfh=this.getHolder(); sfh.addCallback(this); paint=newPaint(); paint.setColor(Color.RED); paint.setAntiAlias(true); paint.setStrokeWidth(5); paint.setStyle(Paint.Style.STROKE); paint.setStrokeCap(Paint.Cap.ROUND); path=newPath(); } publicvoidmyDraw(MotionEvente){ intaction=e.getAction(); switch(action){ caseMotionEvent.ACTION_DOWN: path.moveTo(e.getX(),e.getY()); lastX=e.getX(); lastY=e.getY(); break; caseMotionEvent.ACTION_MOVE: path.quadTo(lastX,lastY,e.getX(),e.getY()); lastX=e.getX(); lastY=e.getY(); break; caseMotionEvent.ACTION_UP: //path.quadTo(lastX,lastY,e.getX(),e.getY()); path.reset(); break; } } @Override publicvoidsurfaceChanged(SurfaceHolderarg0,intarg1,intarg2,intarg3){ } @Override publicvoidsurfaceCreated(SurfaceHolderholder){ flag=true; setOnTouchListener(this); newThread(this).start(); } @Override publicvoidsurfaceDestroyed(SurfaceHolderholder){ flag=false; } @Override publicbooleanonTouch(Viewv,MotionEventevent){ myDraw(event); returntrue; } @Override publicvoidrun(){ while(flag){ longstart=System.currentTimeMillis(); canvas=sfh.lockCanvas(); if(canvas!=null){ canvas.drawPath(path,paint); sfh.unlockCanvasAndPost(canvas); } longend=System.currentTimeMillis(); try{ if(end-start<100){ Thread.sleep(100-(end-start)); } } catch(Exceptione){ } } } }
以上就是本文的全部内容,希望能够帮助大家轻松解决SurfaceView触摸轨迹闪烁问题。