DrawCircle

//

//  ZendaiProgressChartView.m

//  DrawCircle

//

//  Created by ios on 15/4/22.

//  Copyright (c) 2015 ios. All rights reserved.

//


#import "ZendaiProgressChartView.h"


@implementation ZendaiProgressChartView


-(void)setProgress:(CGFloat)progress

{

    _progress=progress;

    [self setNeedsDisplay];

}


// 覆盖drawRect方法,你可以在此自定义绘画和动画

- (void)drawRect:(CGRect)rect

{

    self.backgroundColor=COLOR_MAKE(100.0, 168.0, 72.0);

//    self.backgroundColor=[UIColor clearColor];

    CGFloat R=rect.size.width>rect.size.height?rect.size.height*0.8/2:rect.size.width*0.8/2;

    CGPoint center=CGPointMake(rect.size.width/2, rect.size.height/2);

    CGFloat singel=-M_PI_2+_progress*2*M_PI/100;

    

    

    //An opaque type that represents a Quartz 2D drawing environment.

    //一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你可以在上面任意绘画

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextRef imgCtx = UIGraphicsGetCurrentContext();

    CGContextMoveToPoint(imgCtx, center.x, center.y);

    CGContextSetFillColor(imgCtx, CGColorGetComponents([UIColor colorWithRed:117.0/255.0 green:180.0/255.0 blue:94.0/255.0 alpha:1].CGColor));

    CGContextAddArc(imgCtx, center.x, center.y, R-15,  -M_PI/2,singel, 0);

    CGContextFillPath(imgCtx);

    

    CGContextSetRGBStrokeColor(context,1,1,1,1.0);//画笔线的颜色

    CGContextSetLineWidth(context, 8.0);//线的宽度

    //void CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π ≈57.3° 度=弧度×180°/π 360°360×π/180 弧度

    // x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle 结束的弧度,clockwise 0为顺时针,1为逆时针。

    CGContextAddArc(context, center.x, center.y, R-15,0,2*M_PI, 0); //添加一个圆

    CGContextDrawPath(context, kCGPathStroke); //绘制路径

    

    

    CGContextSetRGBStrokeColor(context,1,1,1,1.0);//画笔线的颜色

    CGContextSetLineWidth(context,1.0);//线的宽度

    //void CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π ≈57.3° 度=弧度×180°/π 360°360×π/180 弧度

    // x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle 结束的弧度,clockwise 0为顺时针,1为逆时针。

    CGContextAddArc(context, center.x, center.y, R,0,2*M_PI, 0); //添加一个圆

    CGContextDrawPath(context, kCGPathStroke); //绘制路径

    //

    

    CGContextRef cirCtx = UIGraphicsGetCurrentContext();

    CGContextMoveToPoint(cirCtx, (center.x+cosf(singel) * R), (center.y+sinf(singel) * R));

    CGContextSetFillColor(cirCtx, CGColorGetComponents([UIColor colorWithRed:254.0/255.0 green:254.0/255.0  blue:254.0/255.0  alpha:1].CGColor));

    CGContextAddArc(cirCtx, (center.x+cosf(singel) * R), (center.y+sinf(singel) * R), 40, -2*M_PI, 1);

    CGContextFillPath(cirCtx);


    

}



@end








#import <UIKit/UIKit.h>


@interface ZendaiProgressCircleView : UIView


@property(nonatomic,assign)CGFloat progress;


@end




//

//  ZendaiProgressCircleView.m

//  DrawCircle

//

//  Created by ios on 15/4/22.

//  Copyright (c) 2015 ios. All rights reserved.

//


#import "ZendaiProgressCircleView.h"


@implementation ZendaiProgressCircleView



-(void)setProgress:(CGFloat)progress

{

    _progress=progress;

    [self setNeedsDisplay];

}


// 覆盖drawRect方法,你可以在此自定义绘画和动画

- (void)drawRect:(CGRect)rect

{

    self.backgroundColor=[UIColor clearColor];

    CGFloat R=rect.size.width>rect.size.height?rect.size.height*0.9/2:rect.size.width*0.9/2;

    CGPoint center=CGPointMake(rect.size.width/2, rect.size.height/2);

    CGFloat singel=_progress*2*M_PI/100-M_PI/2;

    

    

    //An opaque type that represents a Quartz 2D drawing environment.

    //一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你可以在上面任意绘画

    CGContextRef context = UIGraphicsGetCurrentContext();

    //边框圆

    CGContextSetRGBStrokeColor(context,1,1,1,1.0);//画笔线的颜色

    CGContextSetLineWidth(context, 2.0);//线的宽度

    //void CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π ≈57.3° 度=弧度×180°/π 360°360×π/180 弧度

    // x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle 结束的弧度,clockwise 0为顺时针,1为逆时针。

    CGContextAddArc(context, center.x, center.y, R-3.5,0,2*M_PI, 0); //添加一个圆

    CGContextDrawPath(context, kCGPathStroke); //绘制路径

    

    CGContextSetRGBStrokeColor(context,1,1,1,1.0);//画笔线的颜色

    CGContextSetLineWidth(context, 6.0);//线的宽度

    //void CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π ≈57.3° 度=弧度×180°/π 360°360×π/180 弧度

    // x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle 结束的弧度,clockwise 0为顺时针,1为逆时针。

    CGContextAddArc(context, center.x, center.y, R,-M_PI_2,singel, 0); //添加一个圆

    CGContextDrawPath(context, kCGPathStroke); //绘制路径

    

}


@end


### 如何在 Android 中使用 Canvas 绘制圆圈 `Canvas.drawCircle()` 是一种用于绘制圆形的方法,在 Android 平台上广泛应用于图形处理和自定义视图开发中[^1]。 #### 使用 `drawCircle` 的方法签名如下: ```java public void drawCircle(float cx, float cy, float radius, Paint paint) ``` 参数说明: - `cx`: 圆心的 X 坐标位置。 - `cy`: 圆心的 Y 坐标位置。 - `radius`: 圆形半径大小。 - `paint`: 描述画笔的颜色和其他样式属性的对象。 为了更好地理解如何应用此函数,下面提供了一个完整的例子来展示怎样创建一个简单的应用程序并利用 `Canvas` 对象调用 `drawCircle` 来绘制一个带有边框的圆圈[^4]。 #### 示例代码:绘制带边框的圆圈 ```java import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View; public class CircleView extends View { private int width; // 宽度 private int height; // 高度 private int radius; // 半径 private int borderWidth = 8; // 边界宽度 public CircleView(Context context) { super(context); setLayerType(View.LAYER_TYPE_SOFTWARE, null); Paint paint = new Paint(); paint.setColor(Color.BLUE); // 设置填充颜色为蓝色 paint.setStyle(Paint.Style.FILL); // 设定绘画风格为实心 this.radius = Math.min(width, height)/2 - borderWidth/2; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh){ super.onSizeChanged(w,h,oldw,oldh); width=w; height=h; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint fillPaint = new Paint(); // 创建一个新的画笔对象 fillPaint.setAntiAlias(true); // 抗锯齿效果开启 fillPaint.setColor(Color.RED); // 设置内部区域颜色为红色 fillPaint.setStyle(Paint.Style.FILL); // 设置绘图模式为填充 Paint borderPaint = new Paint(fillPaint); // 复制上面设置好的画笔配置给borderPaint borderPaint.setStrokeWidth(borderWidth); // 设置线条粗细等于边界宽度 borderPaint.setStyle(Paint.Style.STROKE);// 更改绘图模式为描边(即只画轮廓) // 计算中心坐标以及调整后的实际半径 final float centerX = (width / 2f); final float centerY = (height / 2f); final float adjustedRadius = radius-borderWidth/2f; // 先绘制内层填充分割线再覆盖外层边缘分割线 canvas.drawCircle(centerX, centerY,adjustedRadius ,fillPaint ); canvas.drawCircle(centerX,centerY,adjustedRadius,borderPaint ); } } ``` 上述代码展示了如何在一个自定义视图组件 (`CircleView`) 内部重写 `onDraw` 方法,并通过传入合适的参数到 `canvas.drawCircle(...)` 函数从而完成对指定区域内绘制具有特定样式的圆形图案的操作。这里还包含了对于抗锯齿的支持、不同部分(比如内外两层)采用不同颜色区分等细节上的优化措施。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值