Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 解決Android SurfaceView繪制觸摸軌跡閃爍問題的方法

解決Android SurfaceView繪制觸摸軌跡閃爍問題的方法

編輯:關於Android編程

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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved