0

这个问题已经被问过很多次,比如Android 绘图、擦除和撤消操作, 但没有人给出正确的解决方案。我正在制作一个绘图应用程序,一切正常,直到我添加了撤消功能。添加撤消功能橡皮擦后无法正常工作。它使以前的绘图变黑并且也不会擦除。这是我的 DrawingView 类的构造函数:

public DrawingView(Context context){
    super(context);
    // if(!isInEditMode())
    setLayerType(View.LAYER_TYPE_SOFTWARE, drawPaint);

            drawPath = new Path();
    drawPaint = new Paint();
    drawPaint.setColor(paintColor);
    drawPaint.setAntiAlias(true);
    drawPaint.setStrokeWidth(35);
    drawPaint.setStyle(Paint.Style.STROKE);
    drawPaint.setStrokeJoin(Paint.Join.ROUND);
    drawPaint.setStrokeCap(Paint.Cap.ROUND);
    canvasPaint = new Paint(Paint.DITHER_FLAG);

}

这是 onDraw 方法

@Override
protected void onDraw(Canvas canvas) {
//draw view

//  canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);

     for (Path p : paths)
        {
           drawPaint.setColor(colorsMap.get(p));
           drawPaint.setStrokeWidth(widthMap.get(p));
           canvas.drawPath(p, drawPaint);          
        } 
        drawPaint.setColor(paintColor);
        drawPaint.setStrokeWidth(30);

    if(erase){
        return;
    }
    canvas.drawPath(drawPath, drawPaint);
}

这是touchEvent Handler

@Override
public boolean onTouchEvent(MotionEvent event) {
//detect user touch     
    float touchX = event.getX();
    float touchY = event.getY();
    if(erase){
    //  drawPaint.setColor(paintColor);
        drawCanvas.drawPath(drawPath, drawPaint);
        invalidate();
    }

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
         undonePaths.clear();
           drawPath.reset();

        drawPath.moveTo(touchX, touchY);

        break;
    case MotionEvent.ACTION_MOVE:
         drawPath.lineTo(touchX, touchY);

        break;
    case MotionEvent.ACTION_UP:
        drawCanvas.drawPath(drawPath, drawPaint);
       if(!erase){
        paths.add(drawPath);
        colorsMap.put(drawPath,getDrawingColor());
        widthMap.put(drawPath,30);
       }
        drawPath = new Path();


        break;
    default:
        return false;
    }
    invalidate();
    return true;
}

这是我的 SetErase 方法

public void setErase(boolean isErase){
    //set erase true or false    
    erase=isErase;
    if(erase){
        drawPaint.setMaskFilter(null);
        drawPaint.setAlpha(0xFF);
        drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        } 
    else{ drawPaint.setXfermode(null);  
        }
    }

最后我的撤消方法

public void onClickUndo() 
{ 
   if (paths.size()>0) 
    { 
       undonePaths.add(paths.remove(paths.size()-1));
       invalidate();
    }      
   else Toast.makeText(getContext(), "nothing more to undo", Toast.LENGTH_SHORT).show();
}

有没有办法在绘图应用程序中同时实现撤消和擦除功能?如果是?那么请提供一些帮助。谢谢

4

1 回答 1

1

这是我的代码。我可以擦除和撤消。我唯一的问题是当我点击画笔时,它会连续擦除。要重新绘制必须选择颜色(不使用最后一种颜色绘制)。如果它有效并且你可以解决这个问题,请告诉我,我希望我能帮上忙。

public class DrawingView extends View {
private Context context;
private Path drawPath;
private Paint drawPaint;
private Paint canvasPaint;
private Canvas drawCanvas;
private Bitmap canvasBitmap;
private int previousPaintColor;
private int paintColor=0xFF000000;
private float brushSize;
private float eraserSize;
private float lastBrushSize;
private boolean isErasing = false;
private boolean isImageLoaded = false;
private List<PaintPathPair> undoList = null;
private List<PaintPathPair> currentMoveList = null;
private List<PaintPathPair> moveList = null;

public DrawingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    this.moveList = new ArrayList<PaintPathPair>();
    this.undoList = new ArrayList<PaintPathPair>();
    this.currentMoveList = new ArrayList<PaintPathPair>();
    this.canvasPaint = new Paint(Paint.DITHER_FLAG);
    setupDrawing();
}

private void clearBrushes() {
    moveList.clear();
    undoList.clear();
    currentMoveList.clear();
}
private 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);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    drawCanvas = new Canvas(canvasBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
    if (isImageLoaded) {
        canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint); 
    }
    drawPaint.setColor(paintColor);
    for (PaintPathPair pair : currentMoveList) {
        canvas.drawPath(pair.getPath(), pair.getPaint());
    }
    for (PaintPathPair pair : moveList) {
        canvas.drawPath(pair.getPath(), pair.getPaint());   
    }
}
public void startNewDrawing() {
    setBackgroundColor(Color.WHITE);
    drawCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
    clearBrushes();
    isImageLoaded = false;
    invalidate();
}
public void undo() {
    if (moveList.size() > 0) {
        undoList.add(moveList.remove(moveList.size() - 1));
        invalidate();   
    }
}
public void redo() {
    if (undoList.size() > 0) {
        moveList.add(undoList.remove(undoList.size() - 1));
        invalidate();
    }
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    float touchX = event.getX();
    float touchY = event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            drawPath.moveTo(touchX, touchY);
            break;
        case MotionEvent.ACTION_MOVE:
            drawPath.lineTo(touchX, touchY);
            currentMoveList.add(new PaintPathPair(drawPaint, drawPath));
            break;
        case MotionEvent.ACTION_UP:
            drawPath.lineTo(touchX, touchY);
            drawCanvas.drawPath(drawPath, drawPaint);
            moveList.add(new PaintPathPair(new Paint(drawPaint), drawPath));
            drawPath = new Path();
            currentMoveList.clear();
            break;
        default:
            return false;
    }
    invalidate();
    return true;
}

void setErasing(boolean erasing) {
    this.isErasing = erasing;
    int colorToSet = 0;
    previousPaintColor = drawPaint.getColor();

    if(isErasing) {
        //drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        this.setColor("#FFFFFFFF");
    }
    else {
        drawPaint.setXfermode(null);
    }
}
public void setColor(String newColor) {
    this.previousPaintColor = drawPaint.getColor();
    paintColor = Color.parseColor(newColor);
    drawPaint.setColor(paintColor);
    invalidate();
}
public float getBrushSize() {
    return brushSize;
}
public void setBrushSize(float newSize) {
    brushSize = newSize;
    drawPaint.setStrokeWidth(brushSize);
    setErasing(false);
}
public float getEraserSize() {
    return eraserSize;
}
public void setEraserSize(float newSize) {
    eraserSize = newSize;
    drawPaint.setStrokeWidth(eraserSize);
    setErasing(true);
}
public void setLastBrushSize(float lastBrushSize) {
    this.lastBrushSize = lastBrushSize;
}
public void setBackgroundImage(Bitmap image) {
    isImageLoaded = true;
    clearBrushes();
    canvasBitmap = image;
    drawCanvas.drawBitmap(image, new Matrix(), null);
    invalidate();
}
public float getLastBrushSize() {
    return lastBrushSize;
}   
}
于 2015-04-06T21:32:21.017 回答