How to draw Arc between two points on the Canvas?

后端 未结 7 1199
渐次进展
渐次进展 2020-11-28 04:27

I have two points in the canvas, now I\'m able to draw a line between those points like this below image by using

This code canvas.drawLine(p1.x, p1.y, p2.x,

相关标签:
7条回答
  • 2020-11-28 05:01

    I may be late to answer but I got more information.

    After Android Lollipop there are two ways to address this problem

    public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

    public void drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

    Usage:

       RectF rectF = new RectF(left, top, right, bottom);
    
        // method 1
        canvas.drawArc (rectF, 90, 45, true,  paints[0]);
    
        // method 2
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.drawArc (left, top, right, bottom, 0, 45, true, paints[1]);
        }
    

    Sweep angle is nothing more than angle of Sector which is drawn clockwise eg. for below code

    private void drawArcs(Canvas canvas) {
        RectF rectF = new RectF(left, top, right, bottom);
    
        // white arc
        canvas.drawArc (rectF, 90, 45, true,  paints[0]);
    
        // Green arc
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.drawArc (left, top, right, bottom, 0, 45, true, paints[1]);
        }
    
        // Red stroked arc
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.drawArc (left, top, right, bottom, 180, 45, true,  paints[2]);
        }
    }
    

    Result will look like this

    Same can be achieved with the help of defining Paths and then iterating over them in onDraw method as illustrated in this snippet:

     public class ArcDrawable extends Drawable {
    
        private int left, right, top, bottom;
        private  Paint[] paints = new Paint[3];
        private HashMap<Path, Paint> pathMap = new HashMap();
    
    
        public ArcDrawable() {
    
            // white paint
            Paint whitePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            whitePaint.setColor(Color.WHITE);
            paints[0]= whitePaint;
    
            // green paint
            Paint greenPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            greenPaint.setColor(Color.GREEN);
            paints[1]= greenPaint;
    
            // red paint
            Paint redPaint =new Paint(Paint.ANTI_ALIAS_FLAG);
            redPaint.setColor(Color.RED);
            redPaint.setStyle(Paint.Style.STROKE);
            paints[2]= redPaint;
        }
    
        @Override
        public void draw(Canvas canvas) {
    
            //----------USE PATHS----------
            // Define and use custom  Path
            for (Map.Entry<Path, Paint> entry : pathMap.entrySet()) {
                // Draw Path on respective Paint style
                canvas.drawPath(entry.getKey(),  entry.getValue());
    
            }
    
            // -------OR use conventional Style---------
            //drawArcs(canvas);
    
        }
    
    
        //Same result
        private void drawArcs(Canvas canvas) {
            RectF rectF = new RectF(left, top, right, bottom);
    
            // method 1
            canvas.drawArc (rectF, 90, 45, true,  paints[0]);
    
            // method 2
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                canvas.drawArc (left, top, right, bottom, 0, 45, true, paints[1]);
            }
    
            // method two with stroke
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                canvas.drawArc (left, top, right, bottom, 180, 45, true,  paints[2]);
            }
        }
    
    
        @Override
        protected void onBoundsChange(Rect bounds) {
            super.onBoundsChange(bounds);
    
            int width = bounds.width();
            int height = bounds.height();
    
            left = bounds.left;
            right = bounds.right;
            top = bounds.top;
            bottom = bounds.bottom;
    
            final int size = Math.min(width, height);
            final int centerX = bounds.left + (width / 2);
            final int centerY = bounds.top + (height / 2);
    
            pathMap.clear();
            //update pathmap using new bounds
            recreatePathMap(size, centerX, centerY);
            invalidateSelf();
        }
    
    
        private Path recreatePathMap(int size, int centerX, int centerY) {
    
            RectF rectF = new RectF(left, top, right, bottom);
    
            // first arc
            Path arcPath = new Path();
            arcPath.moveTo(centerX,centerY);
            arcPath.arcTo (rectF, 90, 45);
            arcPath.close();
            // add to draw Map
            pathMap.put(arcPath, paints[0]);
    
            //second arc
            arcPath = new Path();
            arcPath.moveTo(centerX,centerY);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
              arcPath.arcTo (rectF, 0, 45);
            }
            arcPath.close();
            // add to draw Map
            pathMap.put(arcPath, paints[1]);
    
            // third arc
            arcPath = new Path();
            arcPath.moveTo(centerX,centerY);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                arcPath.arcTo (rectF, 180, 45);
    
            }
            arcPath.close();
            // add to draw Map
            pathMap.put(arcPath, paints[2]);
    
            return arcPath;
    
        }
    
        @Override
        public void setAlpha(int alpha) {
    
        }
    
        @Override
        public void setColorFilter(@Nullable ColorFilter colorFilter) {
    
        }
    
        @Override
        public int getOpacity() {
            return 0;
        }
    
    
    }
    

    Complete source code:

    https://github.com/hiteshsahu/Arc-Drawable

    0 讨论(0)
提交回复
热议问题