android 圆环温度控件,android 圆环倒计时控件
public classCountDownView extendsView {
//圓環(huán)顏色private intmRingColor;//圓環(huán)寬度private floatmRingWidth;//圓環(huán)進(jìn)度值文本大小private intmRingProgessTextSize;//寬度private intmWidth;//高度private intmHeight;privatePaint mPaint;//圓環(huán)的矩形區(qū)域privateRectF mRectF;//private intmProgessTextColor;private intmCountdownTime;private floatmCurrentProgress;ValueAnimator valueAnimator;/*** 監(jiān)聽事件*/privateOnCountDownListener mListener;publicCountDownView(Context context) {
this(context, null);}
publicCountDownView(Context context,@NullableAttributeSet attrs) {
this(context,attrs,0);}
publicCountDownView(Context context,@NullableAttributeSet attrs, intdefStyleAttr) {
super(context,attrs,defStyleAttr);// init();/*** 獲取相關(guān)屬性值*/TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.CountDownView);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= newPaint(Paint.ANTI_ALIAS_FLAG);mPaint.setAntiAlias(true);this.setWillNotDraw(false);}
@Overrideprotected voidonLayout(booleanchanged, intleft, inttop, intright, intbottom) {
super.onLayout(changed,left,top,right,bottom);mWidth= getMeasuredWidth();mHeight= getMeasuredHeight();mRectF= newRectF(0+ mRingWidth/ 2,0+ mRingWidth/ 2,mWidth- mRingWidth/ 2,mHeight- mRingWidth/ 2);}
/*** 設(shè)置倒計(jì)時(shí)間 單位秒*@parammCountdownTime*/public voidsetCountdownTime(intmCountdownTime) {
this.mCountdownTime= mCountdownTime;invalidate();}
// public CountDownView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {// super(context, attrs, defStyleAttr, defStyleRes);// }/*** 動(dòng)畫*@paramcountdownTime*@return*/privateValueAnimator getValueAnimator(longcountdownTime) {
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,100);valueAnimator.setDuration(countdownTime);valueAnimator.setInterpolator(newLinearInterpolator());valueAnimator.setRepeatCount(0);returnvalueAnimator;}
@Overrideprotected voidonDraw(Canvas canvas) {
super.onDraw(canvas);/***圓環(huán)*///顏色mPaint.setColor(mRingColor);//空心mPaint.setStyle(Paint.Style.STROKE);//寬度mPaint.setStrokeWidth(mRingWidth);canvas.drawArc(mRectF,-90,mCurrentProgress- 360, false,mPaint);//繪制文本Paint textPaint = newPaint();textPaint.setAntiAlias(true);textPaint.setTextAlign(Paint.Align.CENTER);String text = mCountdownTime- (int) (mCurrentProgress/ 360f* mCountdownTime) + "";textPaint.setTextSize(mRingProgessTextSize);textPaint.setColor(mProgessTextColor);//文字居中顯示Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();intbaseline = (int) ((mRectF.bottom+ mRectF.top- fontMetrics.bottom- fontMetrics.top) / 2);canvas.drawText(text,mRectF.centerX(),baseline,textPaint);}
/*** 開始倒計(jì)時(shí)*/public voidstartCountDown() {
valueAnimator= getValueAnimator(mCountdownTime* 1000);valueAnimator.addUpdateListener(newValueAnimator.AnimatorUpdateListener() {
@Overridepublic voidonAnimationUpdate(ValueAnimator animation) {
floati = Float.valueOf(String.valueOf(animation.getAnimatedValue()));mCurrentProgress= (int) (360* (i / 100f));invalidate();}
});valueAnimator.start();valueAnimator.addListener(newAnimatorListenerAdapter() {
@Overridepublic voidonAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);//倒計(jì)時(shí)結(jié)束回調(diào)if(mListener!= null) {
mListener.countDownFinished();}
}
});}
/*** 停止倒計(jì)時(shí)*/public voidstopCountDdwn(){
valueAnimator.cancel();}
public voidsetOnCountDownListener(OnCountDownListener mListener) {
this.mListener= mListener;}
}
總結(jié)
以上是生活随笔為你收集整理的android 圆环温度控件,android 圆环倒计时控件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美国397米地标要倒了?纽约93层摩天大
- 下一篇: html 行内超出隐藏,css如何设置文