2013-03-27 52 views
0

我想擦除我的位圖,但我不想擦除背景圖像。當我嘗試擦除是白色的,並且在畫面中非常困難。如何在沒有背景圖像的情況下擦除我的物體?

這是CanvasView

erasePaint.setColor(Color.WHITE); 
//erasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 

erasePaint.setAntiAlias(true); 
erasePaint.setStyle(Paint.Style.STROKE); 
erasePaint.setStrokeJoin(Paint.Join.ROUND); 
erasePaint.setStrokeWidth(12); 

//.... 

protected void onDraw(Canvas canvas) { 
    paint.setPathEffect(null); 


    if(bitmap!=null){ 

     for(MyEraser e:eraserList){ 
      canvas.drawPath(e.p,erasePaint); 
      invalidate(); 
     } 

     final OnTouchListener eraseListener = new OnTouchListener() { 

      public boolean onTouch(View v, MotionEvent event) { 

       // erasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
       //FirstActivity.ll.setVisibility(LinearLayout.GONE); 

       switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
         myEraser = new MyEraser(); 
         lastTouchX = event.getX(); 
         lastTouchY = event.getY(); 
         myEraser.mouseDown(event.getX(), event.getY()); 
         return true; 

        case MotionEvent.ACTION_MOVE: 
        case MotionEvent.ACTION_UP: 
         resetDirtyRect(event.getX(),event.getY()); 
         int historySize = event.getHistorySize(); 
         for(int i=0;i<historySize;i++){ 
          float historicalX = event.getHistoricalX(i); 
          float historicalY = event.getHistoricalY(i); 
          expandDirtyRect(historicalX, historicalY); 

          myEraser.mouseUp(historicalX, historicalY); 
         } 
         myEraser.mouseUp(event.getX(), event.getY()); 
         eraserList.add(myEraser); 
         break; 

        default: 
         Log.d("mock it up", "Unknown touch event " + event.toString()); 
         return false; 
       } 

       invalidate(
        (int) (dirtyRect.left - HALF_STROKE_WIDTH), 
        (int) (dirtyRect.top - HALF_STROKE_WIDTH), 
        (int) (dirtyRect.right + HALF_STROKE_WIDTH), 
        (int) (dirtyRect.bottom + HALF_STROKE_WIDTH)); 

       lastTouchX = event.getX(); 
       lastTouchY = event.getY(); 
       return true; 
      } 
     }; 
    } 
} 

MyEraser

public class MyEraser { 

    Paint paint = new Paint(); 
    Path p = new Path(); 

    public MyEraser(){ 
     paint.setColor(Color.WHITE); 
     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setAntiAlias(true); 
     paint.setStrokeJoin(Paint.Join.ROUND); 
     paint.setStrokeWidth(5); 
    } 

    public void mouseDown(float x, float y) { 
     //path.addCircle(x,y,5,Path.Direction.CW); 
     p.moveTo(x, y); 
     // path.lineTo(x, y); 
    } 

    public void mouseMove(Path path, float x, float y) { 
     // path.addCircle(x,y,5,Path.Direction.CW); 
    } 

    public void mouseUp(float x, float y){ 
     //path.addCircle(x,y,5,Path.Direction.CW); 
     p.lineTo(x, y); 
    } 

    public void draw(Canvas c,Path path){ 
     //paint.setColor(Color.WHITE); 
     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setAntiAlias(true); 
     paint.setStrokeJoin(Paint.Join.ROUND); 
     paint.setStrokeWidth(7); 
     c.drawPath(p, paint); 
    } 
} 

回答

0

編輯:開始了更好的解釋:)

我不熟悉的Android ,其實我從來沒有用過它。將此代碼用作如何組織代碼的示例,而不是功能性代碼。

class MyView extends View { 
    private Eraser myEraser; 
    private Bitmap myBackgroundImage; 
    private Canvas myForegroundCanvas; 

    public MyView(Context context, Attributes, attrs) { 
     myEraser = new Eraser() 
     myBackgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.your_background_name); 
     Bitmap image = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); // the width and height of the view 
     myForegroundCanvas = new Canvas(image); 
    } 

    public boolean onTouchEvent(MotionEvent event) { 
     // update your eraser path 
     return true; 
    } 

    protected void onDraw(Canvas canvas) { 
     canvas.drawBitmap(myBackgroundImage, 0, 0, null); 

     //for (Eraser item : eraserList) { 
      // if you have multiple eraser, add them to a list 
      myEraser.draw(myForegroundCanvas); 
     //} 

     canvas.drawCanvas(myForegroundCanvas, 0, 0, null); 
    } 
} 

主要想法是保持背景和前景圖像之間的分離。這樣,您可以輕鬆更改背景並更新。您也可以在前臺重置以擦除所有內容等。

我希望這可以幫助您。

+0

我不明白:( – 2013-03-27 21:18:20

+0

我在初學的Android和Java – 2013-03-27 21:18:54

+0

我會嘗試尋找解釋我的想法的鏈接。:) – jpmorin 2013-03-27 21:22:42

相關問題