2012-07-22 5 views
0

캔버스를 사용하여 표면 뷰에 텍스트를 그리려합니다. 문제는 각 호출 후에 표면보기가 재설정되지 않는 것입니다. 이전에 그려진 모든 것은 다시 그려 질 것입니다. 그래서, 제가 텍스트를 그릴 때마다 그 위치가 바뀌면 캔버스가 지워지지 않기 때문에 그 텍스트의 긴 흔적이 생깁니다.표면보기 문제 드로잉

내가 뭘 잘못하고 있니?

public class Test1Activity extends Activity { 

private Draw drawText; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    drawText = new Draw(this); 
    setContentView(drawText); 
} 

public class Draw extends SurfaceView implements Runnable { 

    Thread thread = null; 
    SurfaceHolder surfaceHolder; 
    volatile boolean running = false; 
    private int i; 

    public Draw(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     surfaceHolder = getHolder(); 
     running = true; 
     thread = new Thread(this); 
     thread.start(); 
    } 


    public void run() { 
     // TODO Auto-generated method stub 
     while (running) { 
      if (surfaceHolder.getSurface().isValid()) { 
       Canvas canvas = surfaceHolder.lockCanvas(); 

       Paint paint = new Paint(); 
       paint.setColor(Color.RED); 
       i++; 
       if(i > 240) 
        i = 1; 

       canvas.drawText("Hello",i , 60, paint); 

       surfaceHolder.unlockCanvasAndPost(canvas); 
      } 
     } 
    } 

} 
}  

public class Test1Activity extends Activity { 


private Draw drawText; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    drawText = new Draw(this); 
    setContentView(drawText); 
} 



public class Draw extends SurfaceView implements Runnable { 

    Thread thread = null; 
    SurfaceHolder surfaceHolder; 
    volatile boolean running = false; 
    private int i; 

    public Draw(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     surfaceHolder = getHolder(); 
     running = true; 
     thread = new Thread(this); 
     thread.start(); 
    } 


    public void run() { 
     // TODO Auto-generated method stub 
     while (running) { 
      if (surfaceHolder.getSurface().isValid()) { 
       Canvas canvas = surfaceHolder.lockCanvas(); 

       Paint paint = new Paint(); 
       paint.setColor(Color.RED); 
       i++; 
       if(i > 240) 
        i = 1; 

       canvas.drawText("Hello",i , 60, paint); 

       surfaceHolder.unlockCanvasAndPost(canvas); 
      } 
     } 
    } 

} 

감사합니다!

답변

1

예를 들어 그림을 그리기 전에 캔바스를 가져와야합니다. canvas.drawColor (Color.BLACK);

  • ereas (블랙 그릴)
  • 그리기 텍스트

NB : 당신의 업데이트 및 렌더링 스레드에 페인트 객체를 생성하지 마십시오, 그것은 미친 GC를 운전합니다!

0

다른 PorterDuff 모드에서 오래된 캔버스를 다시 그려야 할 수도 있습니다. this link이 유용합니다.