2016-10-14 60 views
0

我正在android上繪製應用程序,它能夠將圖像加載到畫布上並對其進行繪製,但我不知道如何僅擦除繪製路徑,現在,如果要刪除繪製路徑,它將也刪除我放在畫布上的圖像,有沒有辦法只擦除drawpath?如何僅刪除繪製路徑而不是Android上畫布上的圖像?

這裏是我的代碼:

public class DrawingView extends View { 

    private Path drawPath; 
    private Paint drawPaint, canvasPaint; 
    private Canvas drawCanvas; 
    private Bitmap canvasBitmap,image=null; 

    private Rect clipBounds; 
    private Rect destImageRect; 

    private int paintColor = 0xFF660000; 
    private float mScaleFactor = 1.f,pivotPointX = 0.f,pivotPointY=0.f,offSetX=1.00f,offSetY=1.00f; 
    private float brushSize, lastBrushSize; 
    private boolean isGesture = false; 
    private boolean brushScalling = true,erase = false; 

    private ScaleGestureDetector SGD; 
    private GestureDetector GD; 




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

     SGD = new ScaleGestureDetector(context,new ScaleGestureListener()); 
     GD = new GestureDetector(context, new GestureListener()); 

     setupDrawing(); 
    } 

    protected void setupDrawing(){ 
     drawPath = new Path(); 
     drawPaint = new Paint(); 

     brushSize = getResources().getInteger(R.integer.medium_size); 
     lastBrushSize = brushSize; 

     drawPaint.setColor(paintColor); 
     drawPaint.setAntiAlias(true); 
     drawPaint.setStrokeWidth(brushSize); 
     drawPaint.setStyle(Paint.Style.STROKE); 
     drawPaint.setStrokeJoin(Paint.Join.ROUND); 
     drawPaint.setStrokeCap(Paint.Cap.ROUND); 

     canvasPaint = new Paint(Paint.DITHER_FLAG); 

     destImageRect = new Rect(0,0,getWidth(),getHeight()); 
    } 

    public void setBrushSize(float newSize){ 
     float pixelAmount = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, newSize, getResources().getDisplayMetrics()); 
     brushSize = pixelAmount; 
     drawPaint.setStrokeWidth(brushSize); 
    } 

    public void setLastBrushSize(float lastSize){ 
     lastBrushSize = lastSize; 
    } 

    public float getLastBrushSize(){ 
     return lastBrushSize; 
    } 

    public void setErase(boolean isErase){ 
     erase = isErase; 

     if(erase) { 
      drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
     }else{ 
      drawPaint.setXfermode(null); 
     } 
    } 

    public void changeBackground(Bitmap bitmap){ 
     //store image to local var 
     image = bitmap; 
     //get offset of resolution between image and canvas 
     offSetY = (float)image.getHeight()/getHeight(); 
     offSetX = (float)image.getWidth()/getWidth(); 
     //creating new canvas 
     canvasBitmap = image.copy(Bitmap.Config.ARGB_8888, true); 
     drawCanvas = new Canvas(canvasBitmap); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
     //when size changed, store the new size 
     destImageRect = new Rect(0,0,w,h); 
     //check whether to use image as background or not 
     if(image == null) { 
      canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
      drawCanvas = new Canvas(canvasBitmap); 
     }else{ 
      canvasBitmap = image.copy(Bitmap.Config.ARGB_8888, true); 
      drawCanvas = new Canvas(canvasBitmap); 
     } 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     canvas.save(); 
     canvas.scale(mScaleFactor, mScaleFactor, pivotPointX, pivotPointY); 
     clipBounds = canvas.getClipBounds(); 
     canvas.drawPath(drawPath, drawPaint); 
     canvas.drawBitmap(canvasBitmap,null, destImageRect,canvasPaint); 
     canvas.restore(); 


    } 


    public class GestureListener extends GestureDetector.SimpleOnGestureListener{ 
     @Override 
     public boolean onDoubleTap(MotionEvent e) { 
      //zoom out with zoom point of (0,0) top,left 
      mScaleFactor=1.f; 
      pivotPointX=pivotPointY=0; 

      return super.onDoubleTap(e); 
     } 

     @Override 
     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
      //need two finger to be on the screen to be register as scrolling 
      if(e1.getPointerCount()==2 || e2.getPointerCount()==2){ 
       pivotPointX += distanceX/mScaleFactor; 
       pivotPointY += distanceY/mScaleFactor; 
      } 

      return super.onScroll(e1, e2, distanceX, distanceY); 
     } 
    } 


    public class ScaleGestureListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { 

     @Override 
     public boolean onScaleBegin(ScaleGestureDetector detector) { 
      //get the zoom point or center point of the zoom 
      pivotPointX = (detector.getFocusX()/mScaleFactor)+clipBounds.left; 
      pivotPointY = (detector.getFocusY()/mScaleFactor)+clipBounds.top; 

      return super.onScaleBegin(detector); 
     } 

     @Override 
     public boolean onScale(ScaleGestureDetector detector) { 
      //get scale factor 
      mScaleFactor*=detector.getScaleFactor(); 
      //set the limit of zoom 
      mScaleFactor = Math.max(1f, Math.min(mScaleFactor, 20.0f)); 

      return true; 
     } 

     @Override 
     public void onScaleEnd(ScaleGestureDetector detector) { 
      super.onScaleEnd(detector); 

      drawPaint.setStrokeWidth(brushSize/mScaleFactor); 
     } 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     float touchX = (event.getX()*offSetX/mScaleFactor) + clipBounds.left; 
     float touchY = (event.getY()*offSetY/mScaleFactor) + clipBounds.top ; 
     //pass the motion event to the gesture detector first 
     SGD.onTouchEvent(event); 
     GD.onTouchEvent(event); 
     //preventing the used of two finger to trigger drawing event 
     if(event.getPointerCount() == 1) { 
      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN: 
        if (!isGesture) { 
         drawPath.moveTo(touchX, touchY); 
         drawCanvas.drawPath(drawPath, drawPaint); 
        } 
        break; 
       case MotionEvent.ACTION_MOVE: 
        if (!isGesture) { 
         drawPath.lineTo(touchX, touchY); 
         drawCanvas.drawPath(drawPath, drawPaint); 
        } 
        break; 
       case MotionEvent.ACTION_UP: 
        isGesture = false; 
        drawPath.reset(); 
        break; 
       default: 
        return true; 
      } 
     }else if(event.getPointerCount() > 1){ 
      //if two finger are registered at the screen then it is flag as gesture 
      //until no finger are on the screen 
      isGesture = true; 
     } 
     //trigger the onDraw() function 
     invalidate(); 

     return true; 

    } 

    public void setColor(String newColor){ 
     invalidate(); 
     paintColor = Color.parseColor(newColor); 
     drawPaint.setColor(paintColor); 
    } 
} 

回答

0

只要改變你的onDraw碼分兩個階段進行繪製。

首先,您繪製圖像。由於它是分開繪製的,因此不會將圖像放在畫布位圖中。

然後您繪製您的路徑呈現的畫布位圖。 ARGB8888位圖應該是透明的,所以這樣做應該看起來不一樣。

public void changeBackground(Bitmap bitmap){ 
     //store image to local var 
     image = bitmap; 
     //get offset of resolution between image and canvas 
     offSetY = (float)image.getHeight()/getHeight(); 
     offSetX = (float)image.getWidth()/getWidth(); 
     //creating new canvas 
     // DON'T COPY IMAGE canvasBitmap = image.copy(Bitmap.Config.ARGB_8888, true); 
     // just make a blank bitmap 
     canvasBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); 
     drawCanvas = new Canvas(canvasBitmap); 
    } 

    @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
     //when size changed, store the new size 
     destImageRect = new Rect(0,0,w,h); 

     // DON'T COPY IMAGE - just make a blank bitmap 
     canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
     drawCanvas = new Canvas(canvasBitmap); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     canvas.save(); 
     canvas.scale(mScaleFactor, mScaleFactor, pivotPointX, pivotPointY); 
     clipBounds = canvas.getClipBounds(); 
     if (image != null) { 
      canvas.drawBitmap(image, 0, 0, canvasPaint); 
     } 
     // first update the canvas bitmap 
     drawCanvas.drawPath(drawPath, drawPaint); 
     // then draw it on top of the image 
     canvas.drawBitmap(canvasBitmap,null, destImageRect,canvasPaint); 
     canvas.restore(); 
    } 
+0

It Work,Thanks @KrisLarson –