0

我正在使用这段代码让用户用手指画线:

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() 我正在改变油漆的颜色。但是当我改变颜色时,整个绘图都会改变(甚至是我之前画的线条)。如何更改油漆的颜色并使以前的图纸保持不变?我尝试创建新路径,但之前的绘图消失了。

4

3 回答 3

2

您需要为此创建一个小型数据结构,它将存储绘图的颜色和路径。这是一个示例:

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 对象的数组列表。

在 onDraw 方法中这样实现

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

注意:在每个新绘图上,在 arraylist 中添加新创建的 PaintClass 对象...

于 2012-10-09T07:36:45.643 回答
2

您需要使用此代码,它将帮助您画一条线

 public class MainActivity extends Activity implements OnTouchListener  {
private float x;
private float y;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    MyCustomPanel view = new MyCustomPanel(this);

    ViewGroup.LayoutParams params = 
                        new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,
                                                   LayoutParams.FILL_PARENT);
    addContentView(view, params);
    view.setOnTouchListener(this);

}
private class MyCustomPanel extends View {

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

    }
    @Override
    public void draw(Canvas canvas) {

        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(6);

        canvas.drawLine(10,10,50,50,paint);
        paint.setColor(Color.RED);

        canvas.drawLine(50, 50, 90, 10, paint);
        canvas.drawCircle(50, 50, 3, paint);

        canvas.drawCircle(x,y,3,paint);

    }
}
public boolean onTouch(View v, MotionEvent event) {
    x = event.getX();
    y = event.getY();

    v.invalidate();
    return true;
}

}

于 2012-11-26T12:45:26.910 回答
1

您需要使用您的逻辑来保存颜色并将它们加载到 onDraw() 方法中,因为有时我创建了具有 positionX、positionY、color 属性的对象 Point ..:)

于 2012-10-09T07:17:06.207 回答