2013-03-27 58 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

相关问题