利用Android画圆弧canvas.drawArc()实例详解
作者:艾阳丶 时间:2022-05-21 08:45:54
前言
在学习android中图形图像处理技术这部分内容时,对绘制圆弧函数canvas.drawArc()的用法、参数含义及画图原理很是不理解,在网上搜索了一些,加上自己的理解,在此做个小总结,下面来一起看看吧。
示例代码
public void drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter,
@NonNull Paint paint) {
drawArc(oval.left, oval.top, oval.right, oval.bottom, startAngle, sweepAngle, useCenter,
paint);
}
要实现这个方法,我们要传5个参数进去。
第一个参数:RectF oval
oval 参数的作用是:定义的圆弧的形状和大小的范围
/**
* 这是一个居中的圆
*/
float x = (getWidth() - getHeight() / 2) / 2;
float y = getHeight() / 4;
RectF oval = new RectF( x, y,
getWidth() - x, getHeight() - y);
第二个参数:float startAngle
这个参数的作用是设置圆弧是从哪个角度来顺时针绘画的
canvas.drawArc(oval,-90,120,false,mPaint);
canvas.drawArc(oval,90,110,false,mPaint);
//设置为-180的时候也是这样
canvas.drawArc(oval,180,140,false,mPaint);
//设置为360的时候也是这样
canvas.drawArc(oval,0,140,false,mPaint);
第三个参数:float sweepAngle
这个参数的作用是设置圆弧扫过的角度
我们从上面的代码就可以知道其中的作用了
第四个参数:boolean useCenter
这个参数的作用是设置我们的圆弧在绘画的时候,是否经过圆形
值得注意的是,这个参数在我们的 mPaint.setStyle(Paint.Style.STROKE);
设置为描边属性的时候,是看不出效果的。
/**
*这里我是偷懒了,建议不要在onDraw()方法里初始化对象
*/
Paint p = new Paint();//这个是画矩形的画笔,方便大家理解这个圆弧
p.setStyle(Paint.Style.STROKE);
p.setColor(Color.RED);
mPaint.setAntiAlias(true);//取消锯齿
mPaint.setStyle(Paint.Style.FILL);//设置画圆弧的画笔的属性为描边(空心),个人喜欢叫它描边,叫空心有点会引起歧义
mPaint.setStrokeWidth(mCircleWidth);
mPaint.setColor(Color.CYAN);
/**
* 这是一个居中的圆
*/
float x = (getWidth() - getHeight() / 2) / 2;
float y = getHeight() / 4;
RectF oval = new RectF( x, y,
getWidth() - x, getHeight() - y);
canvas.drawArc(oval,360,140,false,mPaint);//画圆弧,这个时候,绘制没有经过圆心
canvas.drawRect(oval, p);//画矩形
//当我们设置为true的时候,绘制的时候就经过圆心了
canvas.drawArc(oval,360,140,true,mPaint);
第五个参数:Paint paint
这个参数的作用是设置我们的画笔对象的属性
//当我们设置为true的时候,绘制的时候就经过圆心了
canvas.drawArc(oval,360,140,true,mPaint);
这里还是要强调一下,当 p.setStyle(Paint.Style.STROKE)
的时候,我们的第四个参数boolean useCenter
,是看不到效果的。
下面是代码全文
public class CustomProgress extends View{
private Paint mPaint;
/**
* 圆的宽度
*/
private int mCircleWidth = 3;
public CustomProgress(Context context) {
this(context, null);
}
public CustomProgress(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomProgress(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
mPaint.setAntiAlias(true);//取消锯齿
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStrokeWidth(mCircleWidth);
mPaint.setColor(Color.CYAN);
/**
* 这是一个居中的圆
*/
float x = (getWidth() - getHeight() / 2) / 2;
float y = getHeight() / 4;
RectF oval = new RectF( x, y,
getWidth() - x, getHeight() - y);
canvas.drawArc(oval,360,140,true,mPaint);
}
}
来源:http://blog.csdn.net/csdn_aiyang/article/details/71616979
标签:android,canvas.drawArc(),画圆弧
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ES结合java代码聚合查询详细示例
2022-08-31 01:23:29
![](https://img.aspxhome.com/file/2023/8/119168_0s.png)
Spring interceptor拦截器配置及用法解析
2023-06-26 06:08:15
Java深入浅出数组的定义与使用上篇
2022-03-10 22:32:58
![](https://img.aspxhome.com/file/2023/1/67131_0s.png)
Mybatis generator如何自动生成代码
2023-08-13 10:15:15
![](https://img.aspxhome.com/file/2023/0/58180_0s.png)
Opencv实现傅里叶变换
2023-08-24 18:53:29
![](https://img.aspxhome.com/file/2023/2/121392_0s.png)
详解Java 自动装箱与拆箱的实现原理
2022-08-16 11:35:51
对Java中JSON解析器的一些见解
2023-02-05 20:53:15
java整合SSM框架的图文教程
2023-03-11 01:06:30
![](https://img.aspxhome.com/file/2023/8/67168_0s.jpg)
java开发分布式服务框架Dubbo调用过程
2022-04-13 14:29:55
![](https://img.aspxhome.com/file/2023/2/71882_0s.png)
springboot中的静态资源加载顺序优先级
2023-08-24 11:12:31
![](https://img.aspxhome.com/file/2023/6/94206_0s.jpg)
详解OAuth2 Token 一定要放在请求头中吗
2022-05-01 09:43:15
![](https://img.aspxhome.com/file/2023/3/76543_0s.jpg)
java使用influxDB数据库的详细代码
2023-03-04 05:22:27
java实现水果超市管理系统
2022-02-03 12:52:32
浅谈Java中的重载,重写,多态,静态绑定、动态绑定
2023-10-10 02:40:05
深入了解Java对象的克隆
2021-10-29 13:59:35
使用maven命令安装jar包到本地仓库的方法步骤
2022-10-19 21:57:58
![](https://img.aspxhome.com/file/2023/8/80958_0s.png)
Android中通知栏跳动问题解决方法
2023-05-31 20:16:55
C#判等对象是否相等的方法汇总
2023-07-13 22:55:07
![](https://img.aspxhome.com/file/2023/7/108707_0s.png)
不看后悔!揭秘游戏服务器开发
2023-08-22 16:20:30
![](https://img.aspxhome.com/file/2023/6/58306_0s.png)
javaweb中Filter(过滤器)的常见应用
2023-07-10 23:57:36