Android - 保存路径并稍后绘制

时间:2014-01-27 00:15:56

标签: android path

我正在使用以下代码来获取触摸事件,绘制路径并将其存储在ArrayList上并且它正在工作。

@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);
        drawCanvas.drawPath(drawPath, drawPaint);
        paths.add(drawPath);
        drawPath.reset();
        drawPath.moveTo(touchX, touchY);
        break;
    case MotionEvent.ACTION_UP:
        drawCanvas.drawPath(drawPath, drawPaint);
        paths.add(drawPath);
        drawPath.reset();
        break;
    default:
        return false;
    }

    invalidate();
    return true;
}

我想再画一次但是颜色不同而且不起作用。如果我创建路径,如注释,它的工作原理:s

public void printPath(){

    Path testePath = new Path();
    //testePath.moveTo(0, 0);
    //testePath.lineTo(300, 300);

    Paint testePaint = new Paint();
    testePaint.setColor(0xFF00FF00);
    testePaint.setAntiAlias(true);
    testePaint.setStrokeWidth(brushSize);
    testePaint.setStyle(Paint.Style.STROKE);
    testePaint.setStrokeJoin(Paint.Join.ROUND);
    testePaint.setStrokeCap(Paint.Cap.ROUND);

    testePath = paths.remove(0);
    drawCanvas.drawPath(testePath, testePaint);
}

我如何创建数学:

public class DrawingView extends View {

    //drawing path
    private Path drawPath;
    //drawing and canvas paint
    private Paint drawPaint, canvasPaint;
    //initial color
    private int paintColor = 0x00660000;
    //canvas
    private Canvas drawCanvas;
    //canvas bitmap
    private Bitmap canvasBitmap;

    private float brushSize, lastBrushSize;

    private ArrayList<Path> paths;

    private boolean erase=false;

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

    private void setupDrawing(){
        brushSize = getResources().getInteger(R.integer.medium_size);
        lastBrushSize = brushSize;

        //get drawing area setup for interaction
        drawPath = new Path();
        drawPaint = new Paint();

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

        canvasPaint = new Paint(Paint.DITHER_FLAG);

        paths = new ArrayList<Path>();
    }
    ...

1 个答案:

答案 0 :(得分:4)

它不起作用,因为您不断重置路径。 删除drawPath.reset();否则当你试图绘制它时,路径将是空的。