本文分享瞭解決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觸摸軌跡閃爍問題。