触摸画的Andr​​oid油漆颜色

触摸画的Andr​​oid油漆颜色

本文介绍了触摸画的Andr​​oid油漆颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的这块code,为用户绘制用手指O线:

I am using this piece of code for user to draw o line with a finger:

public class DrawingView extends View {

private Paint paint;
private Path path;

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

    this.paint = new Paint();
    this.paint.setAntiAlias(true);
    this.paint.setColor(Color.BLACK);
    this.paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(5f);

    this.path = new Path();
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);
}

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

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            return true;
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
            path.lineTo(eventX, eventY);
            break;
        default:
            return false;
    }

    // Schedules a repaint.
    invalidate();
    return true;
}

public void clear() {
    path.reset();
    invalidate();
}

public void setPaintColor(int color) {
    paint.setColor(color);
}

public int getCurrentPaintColor() {
    return paint.getColor();
}
}

使用方法setPaintColor(),我改变油漆的颜色。但是,当我改变颜色,整个绘图得到改变(即使是我以前提请线)。如何改变涂料的颜色和离开previous附图untached?我试图创建新的路径,但随后的previous绘图消失。

With method setPaintColor() I am changing the color of the paint. But when I change the color, the whole drawing gets changed (even the lines that I drew before). How to change the color of the paint and leave previous drawings untached? I tried to create new Path, but then the previous drawing disappears.

推荐答案

您需要为这个小的数据结构,将存储图形的颜色和路径。这里有一个例子:

You need to make a small datastructure for this, which will store both color and path of the drawing.. here is an example:

class PaintClass
{
    Path path;
    int Color;
    public int getColor() { return color; }
    public void setColor(int color){this.Color = color;}
    public int getPath() { return path; }
    public void setPath(Path path){this.path = path;}

    ...
    ...
    ...
}

现在有维持的PaintClass对象的ArrayList。

Now maintain an arraylist having objects of PaintClass.

实施像这样的方法的onDraw

Implement it like this in onDraw method

{
    for(PaintClass item : yourArrayListOfPaintClassObjects)
    {
        //set Paint color like this
        setPaintColor(item.getPaintColor());
        canvas.drawPath(.....,paint);
    }
}

注意:在每个新图形数组列表中添加newely取得PaintClass对象...

这篇关于触摸画的Andr​​oid油漆颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:11