Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 更改路径颜色而不更改以前的路径_Android_Colors_Touch - Fatal编程技术网

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

Android 更改路径颜色而不更改以前的路径,android,colors,touch,Android,Colors,Touch,我想创建一个小的绘画应用程序,我可以使用一些颜色来绘制,我只测试了一个颜色变化,直到现在它还不能正常工作。当我单击按钮并开始使用新颜色绘制时,我之前绘制的所有图形也会更改颜色。有人能帮我吗 public class MyTouchEventView extends View { private Paint paint = new Paint(); private Path path = new Path(); public Button btnChange;

我想创建一个小的绘画应用程序,我可以使用一些颜色来绘制,我只测试了一个颜色变化,直到现在它还不能正常工作。当我单击按钮并开始使用新颜色绘制时,我之前绘制的所有图形也会更改颜色。有人能帮我吗

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

}

这里的问题是您只使用一条路径

你应该在每一个动作上创建一个新的路径。对于这些路径中的每一条,你也必须存储油漆

例如,可以将两个元素都定义为成员的类:

public class Stroke {
    private Path _path;
    private Paint _paint;
}// add constructor(Path, Paint) and accessors
以及您上下文中的笔划列表:

List<Stroke> allStrokes = new ArrayList<Stroke>();
请注意,使用这个简单的解决方案,您无法进行多点触控绘图。为此,您还必须存储和处理MotionEvent ID

编辑:以下是一个工作的多点触控绘制示例,该示例创建了使用随机颜色填充的笔划:

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);
        }
    }
}
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);
        }
    }
}
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);
    }
}
MyActivity.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);
        }
    }
}
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);
        }
    }
}
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);
    }
}
我的解决方案:

public class PaintView extends ImageView {
    private class Holder {
        Path path;
        Paint paint;

        Holder(int color) {
            path = new Path();

            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setStrokeWidth(4f);
            paint.setColor(color);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeCap(Paint.Cap.ROUND);
        }
    }

    private int color = Color.WHITE;
    private List<Holder> holderList = new ArrayList<Holder>();

    public PaintView(Context context) {
        super(context);
        init();
    }

    public PaintView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PaintView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        holderList.add(new Holder(color));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (Holder holder : holderList) {
            canvas.drawPath(holder.path, holder.paint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                holderList.add(new Holder(color));
                holderList.get(holderList.size() - 1).path.moveTo(eventX, eventY);
                return true;
            case MotionEvent.ACTION_MOVE:
                holderList.get(holderList.size() - 1).path.lineTo(eventX, eventY);
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                return false;
        }

        invalidate();
        return true;
    }

    public void resetPaths() {
        for (Holder holder : holderList) {
            holder.path.reset();
        }
        invalidate();
    }

    public void setBrushColor(int color) {
        this.color = color;
    }
}
public类PaintView扩展了ImageView{
私人阶级持有者{
路径;
油漆;
支架(内部颜色){
路径=新路径();
油漆=新油漆();
paint.setAntiAlias(真);
油漆。设置行程宽度(4f);
油漆。设置颜色(颜色);
绘制.设置样式(绘制.样式.笔划);
绘制.设置行程连接(绘制.连接.圆形);
油漆固定行程盖(油漆固定行程盖圆形);
}
}
私有int color=color.WHITE;
私有列表持有者列表=新的ArrayList();
公共PaintView(上下文){
超级(上下文);
init();
}
公共画图视图(上下文、属性集属性){
超级(上下文,attrs);
init();
}
公共画图视图(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
init();
}
私有void init(){
holderList.add(新的Holder(颜色));
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
用于(持有者:持有者列表){
画布.drawPath(holder.path,holder.paint);
}
}
@凌驾
公共布尔onTouchEvent(运动事件){
float eventX=event.getX();
float eventY=event.getY();
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
holderList.add(新的Holder(颜色));
get(holderList.size()-1.path.moveTo(eventX,eventY);
返回true;
case MotionEvent.ACTION\u移动:
get(holderList.size()-1.path.lineTo(eventX,eventY);
打破
case MotionEvent.ACTION\u UP:
打破
违约:
返回false;
}
使无效();
返回true;
}
公共无效重置路径(){
用于(持有者:持有者列表){
holder.path.reset();
}
使无效();
}
公共颜色(内部颜色){
这个颜色=颜色;
}
}

切换到新颜色时,应该使用Path.reset()来解决问题

。检查此编辑部分,如果它有帮助谢谢,这似乎是我正在寻找的,我将如何使用您的方法来解决我的问题?如何检索动作中最后添加的路径?请检查我刚才添加的示例