介绍
这期内容当中小编将会给大家带来有关怎么在android中实现一个圆环倒计时控件,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
1。自定义属性
& lt; ? xml version=?.0“,编码=皍tf-8" ?比; & lt; resources> ,& lt; !——,倒计时控件属性,——比; ,& lt; declare-styleable name=癈ountDownView"比; ,& lt; !——颜色——比; ,& lt; attr name=皉ingColor",格式=癱olor",/比; ,& lt; !——,进度文本的字体大小,——比; ,& lt; attr name=皃rogressTextSize",格式=癲imension",/比; ,& lt; !——,圆环宽度,——比; ,& lt; attr name=皉ingWidth",格式=癴loat",/比; ,& lt; !——进度文本颜色——比; ,& lt; attr name=皃rogressTextColor",格式=癱olor"/比; ,& lt; !——倒计时——比; ,& lt; attr name=癱ountdownTime",格式=癷nteger"/比; ,& lt;/declare-styleable> & lt;/resources>
2。自定义观点
public class CountDownView extends View { ,//圆环颜色 ,private int  mRingColor; ,//圆环宽度 ,private float  mRingWidth; ,//圆环进度值文本大小 ,private int  mRingProgessTextSize; ,//宽度 ,private int  mWidth; ,//高度 ,private int  mHeight; ,private Paint  mPaint; ,//圆环的矩形区域 ,private RectF  mRectF; ,//,private int  mProgessTextColor; ,private int  mCountdownTime; ,private float  mCurrentProgress; ,ValueAnimator valueAnimator;/* * ,*监听事件 ,*/,private OnCountDownListener  mListener; ,public CountDownView (Context 上下文),{ ,这(上下文,null); ,} ,public CountDownView (Context 上下文,@Nullable AttributeSet attrs), { 此(上下文,attrs,,, 0); ,} ,public CountDownView (Context 上下文,@Nullable AttributeSet attrs,, int defStyleAttr), { ,超级(上下文,attrs, defStyleAttr); ,//init ();/* * *,才能获取相关属性值 ,*/,TypedArray TypedArray =, context.obtainStyledAttributes (R.styleable.CountDownView attrs也);=,,mRingColor  typedArray.getColor (R.styleable.CountDownView_ringColor, context.getResources () .getColor (R.color.colorAccent));=,,mRingWidth  typedArray.getFloat (R.styleable.CountDownView_ringWidth, 40);=,,mRingProgessTextSize  typedArray.getDimensionPixelSize (R.styleable.CountDownView_progressTextSize, 20);=,,mProgessTextColor  typedArray.getColor (R.styleable.CountDownView_progressTextColor, context.getResources () .getColor (R.color.colorAccent));=,,mCountdownTime  typedArray.getInteger (R.styleable.CountDownView_countdownTime,, 60); ,typedArray.recycle ();=,,mPaint  new 油漆(Paint.ANTI_ALIAS_FLAG); ,mPaint.setAntiAlias(真正的); ,this.setWillNotDraw(假); ,} ,@Override ,protected void  onLayout (boolean 改变,,int 左,int ,, int ,, int 底部),{ ,super.onLayout(改变,,,,,,对,,底部);=,,mWidth  getMeasuredWidth ();=,,mHeight  getMeasuredHeight ();=,,mRectF  new RectF (0, +, mRingWidth /, 2,, 0, +, mRingWidth /, 2, mWidth 才能成功,mRingWidth /, 2, mHeight 作用;mRingWidth /, 2); ,}/* * ,*设置倒计时间,单位秒 ,* @param mCountdownTime ,*/,public void  setCountdownTime (int mCountdownTime), {=,,this.mCountdownTime  mCountdownTime; ,无效(); ,}//,public CountDownView (Context 上下文,@Nullable AttributeSet attrs,, int defStyleAttr,, int defStyleRes), {//,超级(attrs,上下文,还以为,defStyleAttr, defStyleRes);//,}/* * ,*动画 ,* @param countdownTime ,* @return ,*/,private ValueAnimator  getValueAnimator (long countdownTime), { ,ValueAnimator ValueAnimator =, ValueAnimator.ofFloat (0, 100); ,valueAnimator.setDuration (countdownTime); ,valueAnimator.setInterpolator (new LinearInterpolator ()); ,valueAnimator.setRepeatCount (0); ,return valueAnimator; ,} ,@Override ,protected void  onDraw (Canvas 画布),{ ,super.onDraw(画布);/* * *才能圆环 ,*/,//颜色 ,mPaint.setColor (mRingColor); ,//空心 ,mPaint.setStyle (Paint.Style.STROKE); ,//宽度 ,mPaint.setStrokeWidth (mRingWidth); null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null怎么在android中实现一个圆环倒计时控件