2013-06-13 79 views
3
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 

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

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

    paint.setAntiAlias(true); 
    paint.setStrokeWidth(18f); 
    paint.setColor(Color.LTGRAY); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeJoin(Paint.Join.ROUND); 
    paint.setStrokeCap(Paint.Cap.ROUND); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    if(cc) 
    { 
     Bitmap back = BitmapFactory.decodeResource(getResources(), R.drawable.black_square); 
     Bitmap cb = Bitmap.createScaledBitmap(back, 0, 0, false); 
     canvas.drawBitmap(cb,0,0,null); 
     cc = false; 

    } 
    canvas.drawPath(path, paint); 
} 

public void clearCanvas() 
{ 
    cc =true; 
    invalidate(); 
} 

@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; 
    } 
} 

上述文件是我SingleTouchEventView.Java 這是我MainActivity.java如何清除在畫布上的Android按鈕點擊

public class MainActivity extends Activity { 

    Button reset;; 
LinearLayout canvasAlphabets; 
    SingleTouchEventView myView; 


    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_activity); 


    reset = (Button)findViewById(R.id.reset_canvas); 

    myView = new SingleTouchEventView(this, null); 
    canvasAlphabets = (LinearLayout)findViewById(R.id.canvas_Alphabets); 
    canvasAlphabets.addView(myView); 


      reset.setOnClickListener(new View.OnClickListener() { 


     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 


     } 
     }); 

     } 

    } 

我的問題是什麼樣的代碼,我應該在復位按鈕用於刪除畫布的所有內容。 請幫我 我試過執行myView.clearCanvas()但這沒有幫助。如果我添加此代碼重置點擊buutons它引起FC

感謝

+1

我修好了。 path = new Path(); \t \t Paint clearPaint = new Paint(); \t \t clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); \t \t canvas.drawRect(0,0,0,0,clearPaint); \t \t cc = false; 我用這段代碼 – Damandeep

回答

5
path = new Path(); 
Paint clearPaint = new Paint(); 
clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
canvas.drawRect(0, 0, 0, 0, clearPaint); 
cc = false; 

我用上面的代碼修復了它

0

您可以在panit一切與透明色,清除一切。

2

也許我不明白你想畫什麼,但你嘗試過這樣的:

protected void onDraw(Canvas canvas) 
{ 
    if (cc) 
    { 
     Bitmap back = BitmapFactory.decodeResource(getResources(), R.drawable.black_square); 
     Bitmap cb = Bitmap.createScaledBitmap(back, 0, 0, false); 
     canvas.drawBitmap(cb,0,0,null); 
     cc = false; 
    } 
    else 
     canvas.drawPath(path, paint); 
    } 
} 

否則,如果你想刪除所有,你可以使用這個新的油漆:

Paint transparent = new Paint(); 
transparent.setAlpha(0); 
+1

裏面的代碼if(cc) {...}是我試圖用來清除畫布的那個。 我應該在哪裏使用你寫的代碼? 實際的問題是,如果我嘗試使用myView.canvasClear(),它會導致FC – Damandeep

+2

嘗試此操作: if(cc) Paint transparent = new Paint(); transparent.setAlpha(0); canvas.drawPath(path,transparent); cc = false; } else canvas.drawPath(path,paint); –

+0

保護無效的onDraw(帆布帆布){ \t如果(CC) \t { \t \t Log.d( 「清除-Canavs」, 「清除Canavs開始」); \t \t Paint transparent = new Paint(); transparent.setAlpha(0); canvas.drawPath(path,transparent); \t \t cc = false; \t} \t else \t { canvas.drawPath(path,paint); \t} } 現在我正在使用您所說的代碼。它按下重置按鈕清除畫布,但是我所畫的所有內容都會在觸摸畫布時再次出現。在cavas上運行並不會被刪除。 – Damandeep