如何在视图上模拟橡皮擦?

时间:2012-06-17 07:41:53

标签: android graphics drawing

我一直在开发绘图应用程序,并且有代码:

public class PainterView extends View implements DrawingListener {

    private GestureDetector detector;
    private Context context;
    private Painter painter;
    private Bitmap background;
    private Bitmap bitmap;
    private Paint bitmapPaint;
    private Path path;
    private Paint paint;

    private float x;
    private float y;

    public PainterView(Context context, Painter painter) {

        super(context);
        this.context = context;
        this.painter = painter;
        detector = new GestureDetector(context, new GestureListener());

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (bitmap != null) {
            canvas.drawBitmap(background, 0, 0, bitmapPaint);
            canvas.drawBitmap(bitmap, 0, 0, bitmapPaint);
            canvas.drawPath(path, paint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        detector.onTouchEvent(event);
        x = event.getX();
        y = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            painter.touchStart(x, y);
            break;
        case MotionEvent.ACTION_MOVE:
            painter.touchMove(x, y);
            break;
        case MotionEvent.ACTION_UP:
            painter.touchUp();
            break;
        }
        return true;
    }

    @Override
    public void onPictureUpdate(Bitmap background, Bitmap bitmap, Paint bitmapPaint, Path path,
            Paint paint) {
        this.background=background;
        this.bitmap = bitmap;
        this.bitmapPaint = bitmapPaint;
        this.path = path;
        this.paint = paint;
        invalidate();
    }

    public void setPainter(Painter painter) {
        this.painter = painter;
    }

所以,背景位图是图像(统治纸),我需要知道如何模拟橡皮擦以便用户擦除他的笔记?

1 个答案:

答案 0 :(得分:0)

你可以这样做:

public class Eraser
{
    public void erase(int locX, int locY, int brushsize)
    {
        for(int x = locX - brushsize;x < locX + brushsize;x++)
        {
            for(int y = locY - brushsize;y < locY + brushsize;y++)
            {
                map.setPixle(x,y,Color.white);
            }
        }
    }

    private Bitmap map;
}

这是一个方形橡皮擦,我敢肯定你可以找到一种方法,如果你真的想要它成为一个圆圈。我希望他的帮助!