2011-11-18 55 views
0

我在Android上使用簡單的繪圖應用程序時出現問題。使用SurfaceView繪圖應用程序

下面是其延伸SurfaceView我的類:

public class SomeView extends SurfaceView implements SurfaceHolder.Callback { 

private static SurfaceHolder surfaceHolder; 
static Canvas canvas=null; 
static Paint paint=new Paint(); 
float X,Y,X1,Y1; 
static int mode=1; 

public static void ClearAll(){ 
    canvas=surfaceHolder.lockCanvas(null); 
    canvas.drawColor(Color.BLACK); 
    surfaceHolder.unlockCanvasAndPost(canvas); 
} 

public SomeView(Context context,AttributeSet attrs) { 
    super(context); 
    getHolder().addCallback(this); 
    paint.setColor(Color.WHITE); 
    paint.setStyle(Style.FILL); 
} 


@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    this.surfaceHolder=holder; 
} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
} 

public boolean onTouchEvent(MotionEvent event) 
{ 
    canvas=surfaceHolder.lockCanvas(null); 
    if (mode==1){  
     canvas.drawCircle(event.getX(), event.getY(),10, paint); 
    } 
    if (mode==2){ 
     paint.setStrokeWidth(10); 
     if (event.getAction()==MotionEvent.ACTION_DOWN){ 
      X=event.getX(); 
      Y=event.getY(); 
     } 
     if (event.getAction()==MotionEvent.ACTION_UP){ 
      X1=event.getX(); 
      Y1=event.getY(); 
      canvas.drawLine(X, Y, X1, Y1, paint); 
     } 
    } 
    surfaceHolder.unlockCanvasAndPost(canvas); 
    return true; 
} 

} 

模式= 1 - 是簡單的用戶的觸摸追蹤,

模式= 2 - 是直線繪圖

然而,當我在繪製模式= 2時,圖像變得奇怪: 某些線條消失,並且在再繪製幾條線條之後再次出現。當我仍然觸摸屏幕時,畫布閃爍,顯示自上次調用ClearAll()以來所有線條。如果我停止觸摸,只有幾行仍然可見。

問題是什麼?

回答

1

SurfaceView使用雙緩衝。通常 - 在每個循環/動作中,您必須畫出想要在畫布上可見的每個像素。 希望有所幫助。