介绍
这篇文章主要讲解了Android自定义观点圆形进度条控件的方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
继续练习自定义的观点,这次带来的圆形进度条控件与之前的圆形百分比控件大同小异,这次涉及到了渐变渲染以及画布旋转等知识点,效果如下:
虽然步骤类似,但是我还是要写,毕竟基础的东西就是要多练
1,在res/值文件夹下新建attrs。xml文件,编写自定义属性:
& lt;及# 63;xml version=?.0”;编码=皍tf-8", # 63;比; & lt; resources> & lt; declare-styleable name=癈ircleProgressView"祝辞 & lt; !——弧线宽度——比; & lt; attr name=癮rcWidth"格式=癲imension"/比; & lt; !——刻度个数——比; & lt; attr name=皊caleCount"格式=癷nteger"/比; & lt; !——渐变起始颜色——比; & lt; attr name=皊tartColor"格式=癱olor"/比; & lt; !——渐变终止颜色——比; & lt; attr name=癳ndColor"格式=癱olor"/比; & lt; !——标签说明文本——比; & lt; attr name=發abelText"格式=皊tring"/比; & lt; !——文本颜色——比; & lt; attr name=皌extColor"格式=癱olor"/比; & lt; !——百分比文本字体大小——比; & lt; attr name=皃rogressTextSize"格式=癲imension"/比; & lt; !——标签说明字体大小——比; & lt; attr name=發abelTextSize"格式=癲imension"/比; & lt;/declare-styleable> & lt;/resources>
2,新建CircleProgressView继承看来,重写构造方法:
TypedArray ta=上下文。obtainStyledAttributes (attrs R.styleable。CircleProgressView defStyleAttr 0); mArcWidth=ta.getDimension (R.styleable。CircleProgressView_arcWidth DensityUtils。dp2px(上下文,8)); mScaleCount=ta.getInteger (R.styleable。CircleProgressView_scaleCount、24); mStartColor=ta.getColor (R.styleable。CircleProgressView_startColor Color.parseColor (“# 3 fc199")); mEndColor=ta.getColor (R.styleable。CircleProgressView_endColor Color.parseColor (“# 3294 c1")); mColorArray=new int [] {mStartColor, mEndColor}; mLabelText=ta.getString (R.styleable.CircleProgressView_labelText); mTextColor=ta.getColor (R.styleable。CircleProgressView_textColor Color.parseColor (“# 4 f5f6f")); mProgressTextSize=ta.getDimension (R.styleable。CircleProgressView_progressTextSize, 160); mLabelTextSize=ta.getDimension (R.styleable。CircleProgressView_labelTextSize, 64); ta.recycle ();
4,创建画图所使用的对象,如油漆、矩形,RectF:
@Override 保护空白> @Override 保护空白>公共空setProgress(浮动进展){ Log.e(“——比;“进步+““); ValueAnimator动画=ValueAnimator。ofFloat (mProgress、进步); anim.setDuration((长)(数学。abs (mProgress -进展)* 20)); 动画。addUpdateListener(新ValueAnimator.AnimatorUpdateListener () { @Override 公共空间> & lt;及# 63;xml version=?.0”;编码=皍tf-8", # 63;比; http://schemas.android.com/apk/res/android" & lt; RelativeLayout xmlns: android=?; xmlns: cpv=癶ttp://schemas.android.com/apk/res-auto" xmlns:工具=癶ttp://schemas.android.com/tools" android: layout_width=癿atch_parent" android: layout_height=癿atch_parent" android: paddingBottom=癅dimen/activity_vertical_margin" android: paddingLeft=癅dimen/activity_horizontal_margin" android: paddingRight=癅dimen/activity_horizontal_margin" android: paddingTop=癅dimen/activity_vertical_margin" 工具:上下文=?MainActivity"比; & lt; com.monkey.circleprogressview.CircleProgressView android: id=癅 + id/circle_progress_view" android: layout_width=皐rap_content" android: layout_height=皐rap_content" android: layout_centerInParent=皌rue" cpv: arcWidth=? dp" cpv: endColor=? 126 b94" null null null null null null null null null nullAndroid自定义观点圆形进度条控件的方法