我怎么能绕圈画笔画?

时间:2015-07-14 13:16:32

标签: android android-canvas

我正在以编程方式创建一个圆圈。我如何在圈子周围画出stroke

例如我需要这样:

enter image description here

贝娄是我的代码:

public class Draw extends View {
    boolean flag = false;
    private static class CircleArea {
        int radius;
        int centerX;
        int centerY;

        CircleArea(int centerX, int centerY, int radius) {
            this.radius = radius;
            this.centerX = centerX;
            this.centerY = centerY;
        }

        @Override
        public String toString() {
            return "Circle[" + centerX + ", " + centerY + ", " + radius + "]";
        }
    }

    private Paint mCirclePaint;
    private static final int CIRCLES_LIMIT = 1;
    private HashSet<CircleArea> mCircles = new HashSet<CircleArea>(CIRCLES_LIMIT);
    private SparseArray<CircleArea> mCirclePointer = new SparseArray<CircleArea>(CIRCLES_LIMIT);


    public Draw(final Context ct) {
        super(ct);
        init(ct);
    }


    private void init(final Context ct) {

        mCirclePaint = new Paint();

        mCirclePaint.setColor(Color.BLUE);
        mCirclePaint.setStrokeWidth(40);
        mCirclePaint.setStyle(Paint.Style.FILL);
    }

    @Override
    public void onDraw(final Canvas canv) {

        for (CircleArea circle : mCircles) {
            canv.drawCircle(circle.centerX, circle.centerY, circle.radius, mCirclePaint);
        }
    }

    @Override
    public boolean onTouchEvent(final MotionEvent event) {
        boolean handled = false;
        CircleArea touchedCircle;
        int xTouch;
        int yTouch;
        int pointerId;
        int actionIndex = event.getActionIndex();

        // get touch event coordinates and make transparent circle from it
        switch (event.getActionMasked()) {
           case MotionEvent.ACTION_DOWN:

                clearCirclePointer();

                xTouch = (int) event.getX(0);
                yTouch = (int) event.getY(0);

                touchedCircle = obtainTouchedCircle(xTouch, yTouch);
                touchedCircle.centerX = xTouch;
                touchedCircle.centerY = yTouch;
                mCirclePointer.put(event.getPointerId(0), touchedCircle);

                invalidate();
                handled = true;
                break;
            case MotionEvent.ACTION_MOVE:
                final int pointerCount = event.getPointerCount();

                for (actionIndex = 0; actionIndex < pointerCount; actionIndex++) {
                    pointerId = event.getPointerId(actionIndex);
                    xTouch = (int) event.getX(actionIndex);
                    yTouch = (int) event.getY(actionIndex);
                    touchedCircle = mCirclePointer.get(pointerId);

                    if (null != touchedCircle) {
                        touchedCircle.centerX = xTouch;
                        touchedCircle.centerY = yTouch;
                    }
                }
                invalidate();
                handled = true;
                break;

            case MotionEvent.ACTION_UP:
                clearCirclePointer();
                invalidate();
                handled = true;
                break;

            case MotionEvent.ACTION_POINTER_UP:
                pointerId = event.getPointerId(actionIndex);

                mCirclePointer.remove(pointerId);
                invalidate();
                handled = true;
                break;

            case MotionEvent.ACTION_CANCEL:
                handled = true;
                break;

            default:
                // do nothing
                break;
        }

        return super.onTouchEvent(event) || handled;
    }

    private void clearCirclePointer() {
        mCirclePointer.clear();
    }
    private CircleArea obtainTouchedCircle(final int xTouch, final int yTouch) {
        CircleArea touchedCircle = getTouchedCircle(xTouch, yTouch);
        if (null == touchedCircle) {
            touchedCircle = new CircleArea(xTouch, yTouch, 50);

            if (mCircles.size() == CIRCLES_LIMIT) {
                // remove first circle
                //mCircles.clear();
            }
            if (flag == false) {
                mCircles.add(touchedCircle);
                flag = true;
            }

        }

        return touchedCircle;
    }
    private CircleArea getTouchedCircle(final int xTouch, final int yTouch) {
        CircleArea touched = null;

        for (CircleArea circle : mCircles) {
            if ((circle.centerX - xTouch) * (circle.centerX - xTouch) + (circle.centerY - yTouch) * (circle.centerY - yTouch) <= circle.radius * circle.radius) {
                touched = circle;
                break;
            }
        }

        return touched;
    }
}

2 个答案:

答案 0 :(得分:1)

您需要使用不同风格的单独涂料。

Paint fillPaint;
Paint strokePaint;
fillPaint.setStyle(Paint.Style.FILL);
strokePaint.setStyle(Paint.Style.STROKE);

canvas.drawCircle(circle.centerX, circle.centerY, circle.radius, fillPaint);
canvas.drawCircle(circle.centerX, circle.centerY, circle.radius, strokePaint);

答案 1 :(得分:0)

**You can also use same paint object** 
public class Activity extends Activity 
{
Paint paint;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new MyView(this));
}

public class MyView extends View
{
    public MyView(Context context) 
    {
         super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) 
    {
       super.onDraw(canvas);
       int x = getWidth();
       int y = getHeight();
       int radius;
       radius = 100;

       paint = new Paint();
       paint.setStyle(Paint.Style.FILL);
       paint.setColor(Color.BLUE);
       canvas.drawCircle(x/2, y/2, radius, paint);

       paint.setStyle(Paint.Style.STROKE);
       paint.setColor(Color.GREEN);
       paint.setStrokeWidth(10);
       canvas.drawCircle(x/2, y/2, radius, paint);
   }
}
}