这几天对android中实现画圆弧及圆弧效果中所实现的效果进行了修改,改为进度圆心进度条,效果如图所示
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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。