更改油漆颜色时,现有的笔划也会发生变化

时间:2013-02-28 18:35:12

标签: android colors paint

我正在使用下面的代码捕捉绘图。为了更改笔触颜色或宽度,我调用setPaintAttribute()方法。但是当我改变颜色时,整个绘图都会改变,包括我之前绘制的线条。如何更改颜料的颜色并保持以前的图纸不变?我试图创建一个新路径,但之前的绘图消失了。

public void setPaintAttributes(float STROKE_WIDTH, int color)
    {
        paint=new Paint();
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(STROKE_WIDTH);
    }
 public boolean onTouchEvent(MotionEvent event) {
        scrollView.requestDisallowInterceptTouchEvent(true);
        btnSaveSignature.setEnabled(true);
        btnSaveSignature.setBackgroundResource(R.drawable.save);
        float eventX = event.getX();
        float eventY = event.getY();           

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            lastTouchX = eventX;
            lastTouchY = eventY;
            return true;

        case MotionEvent.ACTION_MOVE:

        case MotionEvent.ACTION_UP:

            resetDirtyRect(eventX, eventY);
            int historySize = event.getHistorySize();
            for (int i = 0; i < historySize; i++) {
                float historicalX = event.getHistoricalX(i);
                float historicalY = event.getHistoricalY(i);
                path.lineTo(historicalX, historicalY);
            }
            path.lineTo(eventX, eventY);
            break;
        }

        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 = eventX;
        lastTouchY = eventY;

        return true;
    }

    private void resetDirtyRect(float eventX, float eventY) {
        dirtyRect.left = Math.min(lastTouchX, eventX);
        dirtyRect.right = Math.max(lastTouchX, eventX);
        dirtyRect.top = Math.min(lastTouchY, eventY);
        dirtyRect.bottom = Math.max(lastTouchY, eventY);
    }

1 个答案:

答案 0 :(得分:2)

创建一个包含路径和绘画对的arraylist,然后将每个路径添加到arraylist,它适用于我,

ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();

使用如下:

    mCanvas = new Canvas();
    mPath = new Path();
    Paint newPaint = new Paint(mPaint);
    paths.add(new Pair<Path, Paint>(mPath, newPaint));