本文分享了解决SurfaceView触摸轨迹闪烁问题的方法,供大家参考,具体内容如下
第一种解决SurfaceView触摸轨迹闪烁问题的方法:
由于SurfaceView使用双缓存机制,两张画布轮流显示到屏幕上。那么,要存储触摸轨迹并避免两张画布内容不一致造成的闪烁问题,完全可以利用保存绘制过程并不断重新绘制的方法解决闪烁,而且这样还顺带解决了多次试验中偶尔出现的因为moveTo()函数不能读取到参数执行默认设置(参数设为上次的触摸点)而出现的断线连接闪烁问题,详细代码如下:
package com.tobacco.touchdraw; import java.util.ArrayList; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.SurfaceHolder.Callback; import android.view.View.OnTouchListener; public class LSurfaceView extends SurfaceView implements Callback,OnTouchListener,Runnable{ private SurfaceHolder sfh; private Canvas canvas; private Paint paint; private Path path; private ArrayList<Path> paths; private boolean flag; public LSurfaceView(Context context) { super(context); sfh=this.getHolder(); sfh.addCallback(this); paint=new Paint(); paint.setColor(Color.RED); paint.setAntiAlias(true); paint.setStrokeWidth(4); paint.setStyle(Paint.Style.STROKE); paint.setStrokeCap(Paint.Cap.ROUND); paths=new ArrayList<Path>(); path=new Path(); } public void myDraw(MotionEvent e){ int action=e.getAction(); switch(action){ case MotionEvent.ACTION_DOWN: path.moveTo(e.getX(),e.getY()); break; case MotionEvent.ACTION_MOVE: path.lineTo(e.getX(),e.getY()); break; case MotionEvent.ACTION_UP: //path.close(); Path path1=new Path(path); paths.add(path1); path.reset(); break; } } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { } @Override public void surfaceCreated(SurfaceHolder holder) { flag=true; setOnTouchListener(this); new Thread(this).start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { flag=false; } @Override public boolean onTouch(View v, MotionEvent event) { myDraw(event); return true; } @Override public void run() { while(flag){ long start=System.currentTimeMillis(); canvas=sfh.lockCanvas(); if(canvas!=null){ canvas.drawColor(Color.BLACK); for(int i=0;i<paths.size();i++) canvas.drawPath(paths.get(i),paint); canvas.drawPath(path,paint); sfh.unlockCanvasAndPost(canvas); } long end=System.currentTimeMillis(); try{ if(end-start<30){ Thread.sleep(30-(end-start)); } } catch(Exception e){ } } } }
这里还要注意的是:ArrayList保存的是对象的引用,所以要在每次添加时都新建一个对象实体。
第二种解决SurfaceView触摸轨迹闪烁问题的方法:
处理触屏轨迹的绘制时,用到了SurfaceView,建立Path对象,在点击时开始设置Path对象,滑动过程中记录触摸点,离开后重新设置Path对象,因不能阻塞主线程,所以新建了一个子线程来不断刷新屏幕,也就是将path不断绘制。但是,接着就出现了一个问题:屏幕中每条轨迹线的终点都会有一小段直线段不断闪烁。猜测可能是lockCanvas()获取的对象区域不一样,就试着使用了lockCanvas(Rect re),但是,运行后发现还是没有解决问题;接着想到可能是因为每次lockCanvas()后获取的对象不同,就在主线程中添加了一个Canvas对象,每次都在Canvas对象中修改画面,然后提交显示,但是,程序运行后效果丝毫没有改变!程序代码如下:
package com.tobacco.touchdraw; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.SurfaceHolder.Callback; import android.view.View.OnTouchListener; public class MySurfaceView extends SurfaceView implements Callback,OnTouchListener,Runnable{ private SurfaceHolder sfh; private Canvas canvas; private Paint paint; private float lastX,lastY; private Path path; private boolean flag; public MySurfaceView(Context context) { super(context); sfh=this.getHolder(); sfh.addCallback(this); paint=new Paint(); paint.setColor(Color.RED); paint.setAntiAlias(true); paint.setStrokeWidth(5); paint.setStyle(Paint.Style.STROKE); paint.setStrokeCap(Paint.Cap.ROUND); path=new Path(); } public void myDraw(MotionEvent e){ int action=e.getAction(); switch(action){ case MotionEvent.ACTION_DOWN: path.moveTo(e.getX(),e.getY()); lastX=e.getX(); lastY=e.getY(); break; case MotionEvent.ACTION_MOVE: path.quadTo(lastX,lastY,e.getX(),e.getY()); lastX=e.getX(); lastY=e.getY(); break; case MotionEvent.ACTION_UP: //path.quadTo(lastX,lastY,e.getX(),e.getY()); path.reset(); break; } } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { } @Override public void surfaceCreated(SurfaceHolder holder) { flag=true; setOnTouchListener(this); new Thread(this).start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { flag=false; } @Override public boolean onTouch(View v, MotionEvent event) { myDraw(event); return true; } @Override public void run() { while(flag){ long start=System.currentTimeMillis(); canvas=sfh.lockCanvas(); if(canvas!=null){ canvas.drawPath(path,paint); sfh.unlockCanvasAndPost(canvas); } long end=System.currentTimeMillis(); try{ if(end-start<100){ Thread.sleep(100-(end-start)); } } catch(Exception e){ } } } }
以上就是本文的全部内容,希望能够帮助大家轻松解决SurfaceView触摸轨迹闪烁问题。