java如何画百分比圆环_canvas绘制百分比圆环进度条

开发项目,PM会跟踪项目进度;完成某个事情,也可以设置一个完成的进度。

这里用canvas绘制一个简单百分比圆环进度条。

看下效果:

1. 动画方式

2. 静默方式

贴上代码,仅供参考

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

/**

* LBS drawRing

* Date: 2015-04-24

* ==================================

* opts.parent 插入到哪里 一个JS元素对象

* opts.width 宽度 = 2* (半径+弧宽)

* opts.radius 半径

* opts.arc 弧宽

* opts.perent 百分比

* opts.color 弧渲染颜色 [底色,进度色]

* opts.textColor 文字渲染颜色

* opts.textSize 文字渲染大小

* opts.animated 是否以动画的方式绘制 默认false

* opts.after 绘制完成时执行函数

* ==================================

**/

functiondrawRing(opts) {var _opts ={

parent: document.body,

width:100,

radius:45,

arc:5,

perent:100,

color: ['#ccc', '#042b61'],

textColor:'#000',

textSize:'14px',

animated:false,

after:function() {}

}, k;for (k in opts) _opts[k] =opts[k];var parent =_opts.parent,

width=_opts.width,

radius=_opts.radius,

arc=_opts.arc,

perent=parseFloat(_opts.perent),

color=_opts.color,

textSize=_opts.textSize,

textColor=_opts.textColor,

c= document.createElement('canvas'),

ctx= null,

x= 0,

animated=_opts.animated,

after=_opts.after;

parent.appendChild(c);

ctx= c.getContext("2d");

ctx.canvas.width=width;

ctx.canvas.height=width;functionclearFill() {

ctx.clearRect(0, 0, width, width);

}functionfillBG() {

ctx.beginPath();

ctx.lineWidth=arc;

ctx.strokeStyle= color[0];

ctx.arc(width/ 2, width / 2, radius, 0, 2 *Math.PI);

ctx.stroke();

}functionfillArc(x) {

ctx.beginPath();

ctx.lineWidth=arc;

ctx.strokeStyle= color[1];

ctx.arc(width/ 2, width / 2, radius, -90 * Math.PI / 180, (x * 3.6 - 90) * Math.PI / 180);

ctx.stroke();

}functionfillText(x) {

ctx.font= textSize + ' Arial';

ctx.fillStyle=textColor;

ctx.textBaseline= "middle";

ctx.textAlign= 'center';

ctx.fillText(x.toFixed(1) + '%', width / 2, width / 2);

}functionfill(x) {

fillBG();

fillArc(x);

fillText(x);

}if (!animated) returnfill(perent);

fill(x);!functionanimate() {if (++x > perent) return after &&after();

setTimeout(animate,10);

clearFill();

fill(x);

}();

}

View Code

很简单的一段代码

先创建一个canvas画布对象,设置宽高。

var c = document.createElement('canvas');

document.body.appendChild(c);var ctx = c.getContext("2d");

ctx.canvas.width=width;

ctx.canvas.height= width;

圆环由两部分组成,底部灰色完整的环,根据百分比变动的环。

先绘制底部完整的环。

//起始一条路径

ctx.beginPath();//设置当前线条的宽度

ctx.lineWidth = 10; //10px//设置笔触的颜色

ctx.strokeStyle = '#ccc';//arc() 方法创建弧/曲线(用于创建圆或部分圆)

ctx.arc(100, 100, 90, 0, 2 *Math.PI);//绘制已定义的路径

ctx.stroke();

arc方法默认是从3点钟方向(90度)开始绘制的,一般要把这个开始处设置平常习惯的0点方向。

也需要理解弧度和角度的互相转换。

degrees = radians * 180/Math.PI

角度等于弧度乘于180再除于PI

radians= degrees * Math.PI/180

弧度等于角度度乘于PI再除于180

绘制根据百分比变动的环。

ctx.beginPath();

ctx.lineWidth= 10;

ctx.strokeStyle= '#f30';//设置开始处为0点钟方向(-90 * Math.PI / 180)//x为百分比值(0-100)

ctx.arc(100, 100, 90, -90 * Math.PI / 180, (x * 3.6 - 90) * Math.PI / 180);

ctx.stroke();

绘制中间的文字

ctx.font = '40px Arial';

ctx.fillStyle= '#f30';

ctx.textBaseline= 'middle';

ctx.textAlign= 'center';

ctx.fillText('50.0%', 100, 100);

到此一个静止的百分比圆环进度条就绘制完成了。

不满足于绘制静态的,动态的更生动有趣一些。

canvas动态绘制就是在一段时间内:绘制一个区域的内容,清除这个区域内容,再重新绘制内容,重复这个过程(绘制-清除-绘制)。

有兴趣可以去研究一下,上面的代码也可以参考,如果结合动画函数和运动公式可以绘制更生动的百分比圆环进度条哦。

--------------------------------------------

参考:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值