Android:使用不同的笔触宽度绘制

时间:2014-10-03 01:02:53

标签: android ondraw

我有一个自定义绘图视图,我根据我的点击和移动坐标绘制线条。 此自定义视图添加到我的主要活动中,我可以在自定义视图中更改绘制对象的笔触宽度。我将当前笔划宽度存储在静态类中。在onDraw() - 方法中,我想用旧的笔触宽度重绘旧的Pathes,我可以用不同的宽度绘制,但它始终是当前设置的笔划宽度。

MainActivity:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    if (position == 0) {
        Memory.setStrokeWidth(10);   //Set stroke width in static class
    }
    if (position == 1) {
        Memory.setStrokeWidth(25);
    }
    if (position == 2) {
        Memory.setStrokeWidth(40);
    }


}

自定义绘图视图:

private Paint paint;
private Path path;
private HashMap<Path, Float> strokeWidthMemory;

@Override
protected void onDraw(Canvas canvas) {

    paint.setStrokeWidth(Memory.getStrokeWidth());
    canvas.drawPath(path, paint);                           
    strokeWidthMemory.put(path, paint.getStrokeWidth());    //Save drawn path with stroke width in HashMap

    //Redraw old lines with stored stroke width
    for (Path p : strokeWidthMemory.keySet()) {
        paint.setStrokeWidth(strokeWidthMemory.get(p));
        canvas.drawPath(p, paint);
    }

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // Get the coordinates of the touch event.
    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // Set a new starting point
        path.moveTo(eventX, eventY);

        return true;
    case MotionEvent.ACTION_MOVE:
        // Connect the points
        path.lineTo(eventX, eventY);
        break;
    default:
        return false;
    }

    // Makes our view repaint and call onDraw
    invalidate();

    return true;
}

1 个答案:

答案 0 :(得分:1)

在绘制路径时在ACTION_MOVE中添加笔触宽度。如果要在onDraw中添加笔触宽度,则每次调用invalidate时,使用当前笔触宽度值更新哈希图

   private Paint paint;
    private Path path;
    private HashMap<Path, Float> strokeWidthMemory;

    @Override
    protected void onDraw(Canvas canvas) {

        paint.setStrokeWidth(Memory.getStrokeWidth());
        canvas.drawPath(path, paint);                           


        //Redraw old lines with stored stroke width
        for (Path p : strokeWidthMemory.keySet()) {
            paint.setStrokeWidth(strokeWidthMemory.get(p));
            canvas.drawPath(p, paint);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Get the coordinates of the touch event.
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // Set a new starting point
            path.moveTo(eventX, eventY);

            return true;
        case MotionEvent.ACTION_MOVE:
            // Connect the points
            path.lineTo(eventX, eventY);
            strokeWidthMemory.put(path, paint.getStrokeWidth());    //Save drawn path with stroke width in HashMap
            break;
        default:
            return false;
        }

        // Makes our view repaint and call onDraw
        invalidate();

        return true;
    }

请查看this answer以供参考。