更改路径颜色不改变previous路径 [英] Change path color without changing previous paths

查看:158
本文介绍了更改路径颜色不改变previous路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个小漆应用,在那里我可以用一些颜色绘制和我测试只有一种颜色变化到现在为止它不能正常工作。当我按一下按钮,并开始与新的颜色绘制,我做了所有的previous图纸也改变颜色。有人可以帮我吗?

 公共类MyTouchEventView扩展视图{    民营涂料粉刷=新的油漆();
    私人路径path =新路径();    公共按钮btnChange;
    公众的LayoutParams PARAMS;    公共MyTouchEventView(上下文的背景下){
        超级(上下文);        paint.setAntiAlias​​(真);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(1F);
        btnChange =新按钮(背景);
        btnChange.setText(Chaneg色);        PARAMS =新的LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        btnChange.setLayoutParams(PARAMS);        btnChange.setOnClickListener(新View.OnClickListener(){            公共无效的onClick(视图v){
                paint.setColor(Color.GREEN);
            }
        });
    }    @覆盖
    保护无效的onDraw(帆布油画){        canvas.drawPath(路径,油漆);    }    @覆盖
    公共布尔onTouchEvent(MotionEvent事件){        浮pointX = event.getX();
        浮尖尖= event.getY();        //检查是否有发生的事件
        开关(event.getAction()){
        案例MotionEvent.ACTION_DOWN:
            path.moveTo(pointX,尖尖的);            返回true;
        案例MotionEvent.ACTION_MOVE:
            path.lineTo(pointX,尖尖的);            打破;
        默认:
            返回false;
        }
        postInvalidate();
        返回true;
    }}


解决方案

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

您应该建立在每一个ACTION_DOWN一条新路。并为每个路径的论文,你必须存放油漆也是如此。

例如,你可以定义与两个元素作为成员的类:

 公共类中风{
    私人路径_path;
    私人涂料_paint;
} //添加构造函数(路径,油漆),并访问

和中风的上下文列表:

 列表<笔划GT; allStrokes =新的ArrayList<笔划GT;();

所以每 ACTION_DOWN ,创建一个新的Stroke(所以一个新的路径,并与您choosen颜色的新漆)。

和每 ACTION_MOVE ,你有retreive最后添加的路径,那么你就可以了lineTo 最后一个点。

然后在你的onDraw,只画都创造了行程:

 为(行程S:allStrokes){
     canvas.drawPath(s.getPath(),s.getPaint());
}

注意:在此简单的解决方案,你不能做一个多点触控图。要做到这一点,你将不得不存储和处理MotionEvent的ID为好。

编辑:下面是创建充满随机颜色招工作的多点触控漆例如:

DrawArea.java:

 进口android.content.Context;
进口android.graphics *。
进口android.util.SparseArray;
进口android.view.MotionEvent;
进口android.view.View;进口的java.util.ArrayList;
进口的java.util.List;
进口了java.util.Random;公共类DrawArea扩展视图{    私人列表<笔划GT; _allStrokes; //需要所有笔划被绘制
    私人SparseArray<笔划GT; _activeStrokes; //使用检索当前笔触绘制
    私人随机_rdmColor =新的随机();    公共DrawArea(上下文的背景下){
        超级(上下文);
        _allStrokes =新的ArrayList<笔划GT;();
        _activeStrokes =新SparseArray<笔划GT;();
        setFocusable(真);
        setFocusableInTouchMode(真);
        setBackgroundColor(Color.WHITE);
    }    公共无效的onDraw(帆布油画){
        如果(_allStrokes!= NULL){
            对于(行程行程:_allStrokes){
                如果(行程!= NULL){
                    路径path = stroke.getPath();
                    漆画家= stroke.getPaint();
                    如果((路径= NULL)及!&安培;!(画家= NULL)){
                        canvas.drawPath(路径,画家);
                    }
                }
            }
        }
    }    @覆盖
    公共布尔onTouchEvent(MotionEvent事件){
        最终诠释行动= event.getActionMasked();
        最终诠释pointerCount = event.getPointerCount();        开关(动作){
            案例MotionEvent.ACTION_DOWN:{
                pointDown((int)的event.getX(),(INT)event.getY(),event.getPointerId(0));
                打破;
            }
            案例MotionEvent.ACTION_MOVE:{
                对于(INT PC = 0;电脑和LT; pointerCount; PC ++){
                    pointMove((int)的event.getX(PC),(INT)event.getY(PC),event.getPointerId(PC));
                }
                打破;
            }
            案例MotionEvent.ACTION_POINTER_DOWN:{
                对于(INT PC = 0;电脑和LT; pointerCount; PC ++){
                    pointDown((int)的event.getX(PC),(INT)event.getY(PC),event.getPointerId(PC));
                }
                打破;
            }
            案例MotionEvent.ACTION_UP:{
                打破;
            }
            案例MotionEvent.ACTION_POINTER_UP:{
                打破;
            }
        }
        无效();
        返回true;
    }    私人无效pointDown(INT X,INT Y,INT ID){
        //创建一个随机颜色油漆
        涂料粉刷=新的油漆();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        paint.setColor(_rdmColor.nextInt());        //创建行程
        点PT =新的点(X,Y);
        行程行程=新的行程(油漆);
        stroke.addPoint(PT);
        _activeStrokes.put(ID,中风);
        _allStrokes.add(中风);
    }    私人无效pointMove(INT X,INT Y,INT ID){
        //检索行程,并添加新的点其路径
        行程行程= _activeStrokes.get(ID);
        如果(行程!= NULL){
            点PT =新的点(X,Y);
            stroke.addPoint(PT);
        }
    }
}

Stroke.java:

 进口android.graphics.Paint;
进口android.graphics.Path;
进口android.graphics.Point;公共类中风{
    私人路径_path;
    私人涂料_paint;    公开行程(涂料粉刷){
        _paint =漆;
    }    公共路径的getPath(){
        返回_path;
    }    市民漆getPaint(){
        返回_paint;
    }    公共无效addPoint(点PT){
        如果(_path == NULL){
            _path =新路径();
            _path.moveTo(pt.x,pt.y);
        }其他{
            _path.lineTo(pt.x,pt.y);
        }
    }
}

MyActivity.java:

 进口android.app.Activity;
进口android.os.Bundle;公共类MyActivity延伸活动{    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        DrawArea DA =新DrawArea(本);
        的setContentView(DA);
    }
}

I wanted to create a small paint app where i can use some colors to draw and i tested only one color change till now it is not working properly. When i click the button and start drawing with the new color, all previous drawings i had made also changes colors. Can someone help me?

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;
    }

}

解决方案

The problem here is you only use one Path.

You should create a new Path on every ACTION_DOWN. And for each of theses path you have to store the Paint as well.

For example you can define a class with both elements as members:

public class Stroke {
    private Path _path;
    private Paint _paint;
}// add constructor(Path, Paint) and accessors

And a list of Stroke in your Context:

List<Stroke> allStrokes = new ArrayList<Stroke>();

So on every ACTION_DOWN, you create an new Stroke (so a new Path, and a new Paint with your choosen color).

And on every ACTION_MOVE, you have retreive the last added Path, then you can lineTo the last point.

Then on your onDraw, just draw all created Stroke:

for (Stroke s : allStrokes) {
     canvas.drawPath(s.getPath(), s.getPaint());
}

Note that with this simple solution, you can not do a multiTouch drawing. To do so you will have to store and handle MotionEvent IDs as well.

EDIT: Here is a working multitouch paint example that creates strokes filled with random colors:

DrawArea.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);
        }
    }
}

Stroke.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);
        }
    }
}

MyActivity.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);
    }
}

这篇关于更改路径颜色不改变previous路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆