2011-12-07 48 views
0

這是我的程序代碼:如何根據下面的代碼清除我的畫布?

SingleTouchEvent是我的課

的名稱中使用canvas.onDraw但它只能更改背景我累了。我想要的是,清除畫布上的另一張圖紙。

public class SingleTouchEventView extends View { 
private Paint paint = new Paint(); 
private Path path = new Path(); 


public SingleTouchEventView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    paint.setAntiAlias(true); 
    paint.setStrokeWidth(6f); 
    paint.setColor(Color.GREEN); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeJoin(Paint.Join.ROUND); 


} 

它只改變屏幕的顏色。它不會刪除畫布的前一個內容。

@Override 
protected void onDraw(Canvas canvas) { 


    canvas.drawColor(Color.BLACK); 
    canvas.drawPath(path, paint); 

} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    float eventX = event.getX(); 
    float eventY = event.getY(); 

    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
     path.moveTo(eventX, eventY); 

     return true; 
    case MotionEvent.ACTION_MOVE: 
     path.lineTo(eventX, eventY); 
     break; 
    case MotionEvent.ACTION_UP: 
     // nothing to do 
     break; 
    default: 
     return false; 
    } 


    // Schedules a repaint. 
invalidate(); 


return true; 
} 



} 
+0

關閉一個副本http://stackoverflow.com/questions/5729377/android-canvas-how-do-i-clear-delete-contents-of-a-canvas-bitmaps-livin –

+0

哪裏會iplace的canvas.onDraw? – user1081908

回答

0

call invalidate();在OnDraw函數結束時,它會不停的叫喚的onDraw,在此之後作出這樣的檢查:

if(isClear) 
{ 
    invalidate();//It'll draw nothing. 
} 
else 
{ 
    do your drawing.. 
} 

或點擊此鏈接: How to clear finger paint?

+0

謝謝!但我會在哪裏放置上面給出的檢查? – user1081908

+0

onDraw函數.. –

+0

我得到錯誤if(isClear).... – user1081908

0

試試:

canvas.drawColor(0, Mode.CLEAR);