如何在Android中创建Arc

时间:2018-04-05 15:50:16

标签: android android-canvas

我想创建一个类似于照片的小尺寸像这样的照片:仔细观察照片,你会发现边框是黑色的,有白色的颜色,如何设计:

这是我的尝试,但没有得到这样的观点:

     protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
       int mX=200,mY=200;

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(50);
        // Setting the color of the circle
        mPaint.setColor(Color.BLUE);

        // Draw the circle at (x,y) with radius 250
        int radius = 150;
        canvas.drawCircle(mX, mY, radius, mPaint);

        mPaint.setColor(Color.WHITE);
        mPaint.setDither(true);                    // set the dither to true
        mPaint.setStyle(Paint.Style.STROKE);       // set to STOKE
        mPaint.setStrokeJoin(Paint.Join.ROUND);    // set the join to round you want
        mPaint.setStrokeCap(Paint.Cap.ROUND);      // set the paint cap to round too
        mPaint.setPathEffect(new CornerPathEffect(50) );   // set the path effect when they join.
        mPaint.setAntiAlias(true);

        RectF oval = new RectF(mX - radius, mY - radius, mX + radius, mY + radius);
//        canvas.drawArc(oval, 180, 0, false, mPaint);
//        mPaint.setColor(Color.RED);
        canvas.drawArc(oval, -180, 90, false, mPaint);
        // Redraw the canvas
        invalidate();
    }

这是我得到的当前图片: My o/p image with this code

1 个答案:

答案 0 :(得分:0)

我重复使用了您之前的代码版本,并通过在第一个版本中绘制更薄的笔划来实现此目的 https://i.imgur.com/2EwelfV.png

private void init()
{
    mStrokePaintOuter = new Paint(Paint.ANTI_ALIAS_FLAG);
    mStrokePaintOuter.setStyle(Paint.Style.STROKE);
    mStrokePaintOuter.setStrokeWidth(STROKE_WIDTH);
    mStrokePaintOuter.setColor(ContextCompat.getColor(getContext(), R.color.colorAccent));

    mStrokePaintInner = new Paint(Paint.ANTI_ALIAS_FLAG);
    mStrokePaintInner.setStyle(Paint.Style.STROKE);
    mStrokePaintInner.setStrokeWidth(STROKE_WIDTH - 10);
    mStrokePaintInner.setColor(ContextCompat.getColor(getContext(), android.R.color.white));

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int centerX = 0;
    int centerY = 0;
    int radius = 0;
    if (mRect == null)
    {
        centerX = getMeasuredWidth()/ 2;
        centerY = getMeasuredHeight()/ 2;
        radius = Math.min(centerX,centerY);

        int startTop = (int) (STROKE_WIDTH / 2);
        int startLeft = startTop;

        int endBottom = 2 * radius - startTop;
        int endRight = endBottom;

        mRect = new RectF(startTop, startLeft, endRight, endBottom);
    }
     canvas.drawArc(mRect, 192, 258, false, mStrokePaintOuter);
     canvas.drawArc(mRect, 193, 256, false, mStrokePaintInner);
}