2011-11-18 36 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使用双缓冲。通常 - 在每个循环/动作中,您必须画出想要在画布上可见的每个像素。 希望有所帮助。