更改路径颜色而不更改以前的路径

Kat*_*aty 7 android colors touch

我想创建一个小型油漆应用程序,我可以在其中使用一些颜色进行绘制,但我只测试了一种颜色变化,直到现在它无法正常工作。当我单击按钮并开始使用新颜色绘图时,我之前所做的所有绘图也会更改颜色。有人能帮我吗?

public class MyTouchEventView extends View {

    private Paint paint = new Paint();
    private Path path = new Path();



    public Button btnChange;
    public LayoutParams params;

    public MyTouchEventView(Context context) {
        super(context);

        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(1f);


        btnChange = new Button(context);
        btnChange.setText("Chaneg color");

        params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        btnChange.setLayoutParams(params);

        btnChange.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {


                paint.setColor(Color.GREEN);
            }
        });


    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawPath(path, paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float pointX = event.getX();
        float pointY = event.getY();

        // Checks for the event that occurs
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(pointX, pointY);

            return true;
        case MotionEvent.ACTION_MOVE:
            path.lineTo(pointX, pointY);

            break;
        default:
            return false;
        }


        postInvalidate();
        return true;
    }

}
Run Code Online (Sandbox Code Playgroud)

lea*_*ien 4

这里的问题是你只使用一个路径。

您应该在每个 ACTION_DOWN 上创建一个新路径。对于每条路径,您还必须存储 Paint。

例如,您可以定义一个包含两个元素作为成员的类:

public class Stroke {
    private Path _path;
    private Paint _paint;
}// add constructor(Path, Paint) and accessors
Run Code Online (Sandbox Code Playgroud)

以及您上下文中的中风列表:

List<Stroke> allStrokes = new ArrayList<Stroke>();
Run Code Online (Sandbox Code Playgroud)

因此,在每个 上ACTION_DOWN,您都会创建一个新的 Stroke(因此是一个新的 Path,以及一个具有您选择的颜色的新 Paint)。

在每个 上ACTION_MOVE,您都检索了最后添加的路径,然后您可以找到lineTo最后一个点。

然后在 onDraw 上,绘制所有创建的 Stroke:

for (Stroke s : allStrokes) {
     canvas.drawPath(s.getPath(), s.getPaint());
}
Run Code Online (Sandbox Code Playgroud)

请注意,使用这个简单的解决方案,您无法进行多点触控绘图。为此,您还必须存储和处理 MotionEvent ID。

编辑:这是一个有效的多点触控绘画示例,它创建填充随机颜色的笔画:

绘制区域.java:

import android.content.Context;
import android.graphics.*;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DrawArea extends View {

    private List<Stroke> _allStrokes; //all strokes that need to be drawn
    private SparseArray<Stroke> _activeStrokes; //use to retrieve the currently drawn strokes
    private Random _rdmColor = new Random();

    public DrawArea(Context context) {
        super(context);
        _allStrokes = new ArrayList<Stroke>();
        _activeStrokes = new SparseArray<Stroke>();
        setFocusable(true);
        setFocusableInTouchMode(true);
        setBackgroundColor(Color.WHITE);
    }

    public void onDraw(Canvas canvas) {
        if (_allStrokes != null) {
            for (Stroke stroke: _allStrokes) {
                if (stroke != null) {
                    Path path = stroke.getPath();
                    Paint painter = stroke.getPaint();
                    if ((path != null) && (painter != null)) {
                        canvas.drawPath(path, painter);
                    }
                }
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int action = event.getActionMasked();
        final int pointerCount = event.getPointerCount();

        switch (action) {
            case MotionEvent.ACTION_DOWN: {
                pointDown((int)event.getX(), (int)event.getY(), event.getPointerId(0));
                break;
            }
            case MotionEvent.ACTION_MOVE: {
                for (int pc = 0; pc < pointerCount; pc++) {
                    pointMove((int) event.getX(pc), (int) event.getY(pc), event.getPointerId(pc));
                }
                break;
            }
            case MotionEvent.ACTION_POINTER_DOWN: {
                for (int pc = 0; pc < pointerCount; pc++) {
                    pointDown((int)event.getX(pc), (int)event.getY(pc), event.getPointerId(pc));
                }
                break;
            }
            case MotionEvent.ACTION_UP: {
                break;
            }
            case MotionEvent.ACTION_POINTER_UP: {
                break;
            }
        }
        invalidate();
        return true;
    }

    private void pointDown(int x, int y, int id) {
        //create a paint with random color
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        paint.setColor(_rdmColor.nextInt());

        //create the Stroke
        Point pt = new Point(x, y);
        Stroke stroke = new Stroke(paint);
        stroke.addPoint(pt);
        _activeStrokes.put(id, stroke);
        _allStrokes.add(stroke);
    }

    private void pointMove(int x, int y, int id) {
        //retrieve the stroke and add new point to its path
        Stroke stroke = _activeStrokes.get(id);
        if (stroke != null) {
            Point pt = new Point(x, y);
            stroke.addPoint(pt);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

中风.java:

import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;

public class Stroke {
    private Path _path;
    private Paint _paint;

    public Stroke (Paint paint) {
        _paint = paint;
    }

    public Path getPath() {
        return _path;
    }

    public Paint getPaint() {
        return _paint;
    }

    public void addPoint(Point pt) {
        if (_path == null) {
            _path = new Path();
            _path.moveTo(pt.x, pt.y);
        } else {
            _path.lineTo(pt.x, pt.y);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的活动.java:

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DrawArea da = new DrawArea(this);
        setContentView(da);
    }
}
Run Code Online (Sandbox Code Playgroud)