android漂亮的进度条,Android自定义漂亮的圆形进度条

这几天对android中实现画圆弧及圆弧效果中所实现的效果进行了修改,改为进度圆心进度条,效果如图所示

411dd68ad68d5314f7037a4098667446.gif

taskscompletedview.java 代码如下

import android.content.context;

import android.content.res.typedarray;

import android.graphics.canvas;

import android.graphics.paint;

import android.graphics.rectf;

import android.graphics.paint.fontmetrics;

import android.util.attributeset;

import android.view.view;

import com.snailws.taskscompleted.r;

/**

* @author naiyu(http://snailws.com)

* @version 1.0

*/

public class taskscompletedview extends view {

// 画实心圆的画笔

private paint mcirclepaint;

// 画圆环的画笔

private paint mringpaint;

// 画字体的画笔

private paint mtextpaint;

// 圆形颜色

private int mcirclecolor;

// 圆环颜色

private int mringcolor;

// 半径

private float mradius;

// 圆环半径

private float mringradius;

// 圆环宽度

private float mstrokewidth;

// 圆心x坐标

private int mxcenter;

// 圆心y坐标

private int mycenter;

// 字的长度

private float mtxtwidth;

// 字的高度

private float mtxtheight;

// 总进度

private int mtotalprogress = 100;

// 当前进度

private int mprogress;

public taskscompletedview(context context, attributeset attrs) {

super(context, attrs);

// 获取自定义的属性

initattrs(context, attrs);

initvariable();

}

private void initattrs(context context, attributeset attrs) {

typedarray typearray = context.gettheme().obtainstyledattributes(attrs,

r.styleable.taskscompletedview, 0, 0);

mradius = typearray.getdimension(r.styleable.taskscompletedview_radius, 80);

mstrokewidth = typearray.getdimension(r.styleable.taskscompletedview_strokewidth, 10);

mcirclecolor = typearray.getcolor(r.styleable.taskscompletedview_circlecolor, 0xffffffff);

mringcolor = typearray.getcolor(r.styleable.taskscompletedview_ringcolor, 0xffffffff);

mringradius = mradius + mstrokewidth / 2;

}

private void initvariable() {

mcirclepaint = new paint();

mcirclepaint.setantialias(true);

mcirclepaint.setcolor(mcirclecolor);

mcirclepaint.setstyle(paint.style.fill);

mringpaint = new paint();

mringpaint.setantialias(true);

mringpaint.setcolor(mringcolor);

mringpaint.setstyle(paint.style.stroke);

mringpaint.setstrokewidth(mstrokewidth);

mtextpaint = new paint();

mtextpaint.setantialias(true);

mtextpaint.setstyle(paint.style.fill);

mtextpaint.setargb(255, 255, 255, 255);

mtextpaint.settextsize(mradius / 2);

fontmetrics fm = mtextpaint.getfontmetrics();

mtxtheight = (int) math.ceil(fm.descent - fm.ascent);

}

@override

protected void ondraw(canvas canvas) {

mxcenter = getwidth() / 2;

mycenter = getheight() / 2;

canvas.drawcircle(mxcenter, mycenter, mradius, mcirclepaint);

if (mprogress > 0 ) {

rectf oval = new rectf();

oval.left = (mxcenter - mringradius);

oval.top = (mycenter - mringradius);

oval.right = mringradius * 2 + (mxcenter - mringradius);

oval.bottom = mringradius * 2 + (mycenter - mringradius);

canvas.drawarc(oval, -90, ((float)mprogress / mtotalprogress) * 360, false, mringpaint); //

// canvas.drawcircle(mxcenter, mycenter, mradius + mstrokewidth / 2, mringpaint);

string txt = mprogress + "%";

mtxtwidth = mtextpaint.measuretext(txt, 0, txt.length());

canvas.drawtext(txt, mxcenter - mtxtwidth / 2, mycenter + mtxtheight / 4, mtextpaint);

}

}

public void setprogress(int progress) {

mprogress = progress;

// invalidate();

postinvalidate();

}

}

attrs.xml

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值