本文参考借鉴:https://www.oudahe.com/p/26827/
先上效果图:
自定义控件:AttendanceProgressbar
代码如下:
public class AttendanceProgressbar extends VIEw { // 画圆环底部的画笔 private Paint mCirclePaint; // 画圆环的画笔 private Paint mRingPaint; // 画字体的画笔 private Paint mTextPaint; // 圆形颜色 private int mCircle@R_419_6004@; // 圆环颜色 private int mRing@R_419_6004@; // 半径 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 = 80; //字体颜色 private int mText@R_419_6004@; // 字体大小 private float mTextSize; public AttendanceProgressbar(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.AttendanceProgressbar,0); mRadius = typeArray.getDimension(R.styleable.AttendanceProgressbar_radius,80); mstrokeWIDth = typeArray.getDimension(R.styleable.AttendanceProgressbar_strokeWIDth,10); mCircle@R_419_6004@ = typeArray.get@R_419_6004@(R.styleable.AttendanceProgressbar_circle@R_419_6004@,0xFFFFFFFF); mRing@R_419_6004@ = typeArray.get@R_419_6004@(R.styleable.AttendanceProgressbar_ring@R_419_6004@,0xFFFFFFFF); mText@R_419_6004@ = typeArray.get@R_419_6004@(R.styleable.AttendanceProgressbar_text@R_419_6004@,0xFF000000); mTextSize = typeArray.getDimension(R.styleable.AttendanceProgressbar_textSize,80); mRingRadius = mRadius + mstrokeWIDth / 2; } private voID initvariable() { mCirclePaint = new Paint(); mCirclePaint.setAntiAlias(true); mCirclePaint.set@R_419_6004@(mCircle@R_419_6004@);// mCirclePaint.setStyle(Paint.Style.FILL); mCirclePaint.setStyle(Paint.Style.stroke); mCirclePaint.setstrokeWIDth(mstrokeWIDth); mRingPaint = new Paint(); mRingPaint.setAntiAlias(true); mRingPaint.set@R_419_6004@(mRing@R_419_6004@); mRingPaint.setStyle(Paint.Style.stroke); mRingPaint.setstrokeWIDth(mstrokeWIDth); mTextPaint = new Paint(); mTextPaint.setAntiAlias(true); mTextPaint.setStyle(Paint.Style.FILL); mTextPaint.set@R_419_6004@(mText@R_419_6004@);// mTextPaint.setARGB(255,255,255);// mTextPaint.setTextSize(mRadius / 2); mTextPaint.setTextSize(mTextSize); Paint.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); canvas.drawCircle(mXCenter,mRingRadius,mCirclePaint); 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,mRadius + mstrokeWIDth / 2,mRingPaint);// String txt = mProgress + "%"; String txt = "16/18"; mTxtWIDth = mTextPaint.measureText(txt,txt.length());// canvas.drawText(txt,mXCenter - mTxtWIDth / 2,mYCenter + mTxtHeight / 4,mTextPaint); canvas.drawText(txt,mYCenter + mTxtHeight / 4 - diptopx(10),mTextPaint); txt = "出勤人数"; mTxtWIDth = mTextPaint.measureText(txt,txt.length()); canvas.drawText(txt,mYCenter + mTxtHeight / 4 + diptopx(10),mTextPaint); } public voID setProgress(int progress) { mProgress = progress; postInvalIDate(); } private int diptopx(int dip) { float scale = getContext().getResources().getdisplayMetrics().density; return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1)); }}
因为是自定义控件,所以在attr.xml文件定义了一些控件属性,以便在xml文件中设置这些属性
代码如下:
<?xml version="1.0" enCoding="utf-8"?><resources> <declare-styleable name="AttendanceProgressbar"> <attr name="radius" format="dimension"/> <attr name="strokeWIDth" format="dimension"/> <attr name="circle@R_419_6004@" format="@R_419_6004@"/> <attr name="ring@R_419_6004@" format="@R_419_6004@"/> <attr name="text@R_419_6004@" format="@R_419_6004@"/> <attr name="textSize" format="dimension"/> </declare-styleable></resources>
最后,在xml文件中,可以这样使用
<com.ztd.lIEyi.Widget.AttendanceProgressbar androID:layout_gravity="center" androID:layout_wIDth="100dp" androID:layout_height="100dp" app:radius="45dp" app:strokeWIDth="5dp" app:textSize="@dimen/text_16" app:text@R_419_6004@="@@R_419_6004@/@R_419_6004@_333333" app:circle@R_419_6004@="@@R_419_6004@/@R_419_6004@_d5ebfd" app:ring@R_419_6004@="@@R_419_6004@/@R_419_6004@_2c9df7"/>
这只是初步处理,使用时可以根据需求酌情处理。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android实现环形进度条代码全部内容,希望文章能够帮你解决Android实现环形进度条代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)