本文实例为大家分享了androID自定义圆形倒计时显示控件的具体代码,供大家参考,具体内容如下
先上效果图
- 倒计时结束
代码块
attr.xml 控件需要用到的属性:
<?xml version="1.0" enCoding="utf-8"?><resources> <declare-styleable name="CountDownVIEw"> <!--颜色--> <attr name="ringcolor" format="color" /> <!-- 进度文本的字体大小 --> <attr name="progresstextSize" format="dimension" /> <!-- 圆环宽度 --> <attr name="ringWIDth" format="float" /> <!--进度文本颜色--> <attr name="progresstextcolor" format="color"/> <!--倒计时--> <attr name="countdownTime" format="integer"/> </declare-styleable></resources>
CountDownVIEw.java
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; private OnCountDownFinishListener mListener; public CountDownVIEw(Context context) { this(context,null); } public CountDownVIEw(Context context,AttributeSet attrs) { this(context,attrs,0); } public CountDownVIEw(Context context,AttributeSet attrs,int defStyleAttr) { super(context,defStyleAttr); TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CountDownVIEw); mRingcolor = a.getcolor(R.styleable.CountDownVIEw_ringcolor,context.getResources().getcolor(R.color.colorAccent)); mRingWIDth = a.getfloat(R.styleable.CountDownVIEw_ringWIDth,40); mRingProgesstextSize = a.getDimensionPixelSize(R.styleable.CountDownVIEw_progresstextSize,displayUtils.sp2px(context,20)); mProgesstextcolor = a.getcolor(R.styleable.CountDownVIEw_progresstextcolor,context.getResources().getcolor(R.color.colorAccent)); mCountdownTime = a.getInteger(R.styleable.CountDownVIEw_countdownTime,60); a.recycle(); mPaint = new Paint(Paint.ANTI_AliAS_FLAG); mPaint.setAntiAlias(true); this.setwillNotDraw(false); } public voID setCountdownTime(int mCountdownTime) { this.mCountdownTime = mCountdownTime; } @OverrIDe protected voID onLayout(boolean changed,int left,int top,int right,int bottom) { super.onLayout(changed,left,top,right,bottom); mWIDth = getMeasureDWIDth(); mHeight = getMeasuredHeight(); mRectF = new RectF(0 + mRingWIDth / 2,0 + mRingWIDth / 2,mWIDth - mRingWIDth / 2,mHeight - mRingWIDth / 2); } @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); /** *圆环 */ //颜色 mPaint.setcolor(mRingcolor); //空心 mPaint.setStyle(Paint.Style.stroke); //宽度 mPaint.setstrokeWIDth(mRingWIDth); canvas.drawArc(mRectF,-90,mCurrentProgress - 360,false,mPaint); //绘制文本 Paint textPaint = new Paint(); 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(); int baseline = (int) ((mRectF.bottom + mRectF.top - FontMetrics.bottom - FontMetrics.top) / 2); canvas.drawText(text,mRectF.centerX(),baseline,textPaint); } private ValueAnimator getValA(long countdownTime) { ValueAnimator valueAnimator = ValueAnimator.offloat(0,100); valueAnimator.setDuration(countdownTime); valueAnimator.setInterpolator(new linearInterpolator()); valueAnimator.setRepeatCount(0); return valueAnimator; } /** * 开始倒计时 */ public voID startCountDown() { setClickable(false); ValueAnimator valueAnimator = getValA(mCountdownTime * 1000); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @OverrIDe public voID onAnimationUpdate(ValueAnimator animation) { float i = float.valueOf(String.valueOf(animation.getAnimatedValue())); mCurrentProgress = (int) (360 * (i / 100f)); invalIDate(); } }); valueAnimator.start(); valueAnimator.addListener(new AnimatorListenerAdapter() { @OverrIDe public voID onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); //倒计时结束回调 if (mListener != null) { mListener.countDownFinished(); } setClickable(true); } }); } public voID setAddCountDownListener(OnCountDownFinishListener mListener) { this.mListener = mListener; } public interface OnCountDownFinishListener { voID countDownFinished(); }}
MainActivity.java
package com.ouyuan.demo.myapplication;import androID.animation.ValueAnimator;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.TextVIEw;import androID.Widget.Toast;public class MainActivity extends AppCompatActivity { CountDownVIEw cdv; TextVIEw textVIEw; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); cdv = (CountDownVIEw) findVIEwByID(R.ID.countDownVIEw); cdv.setAddCountDownListener(new CountDownVIEw.OnCountDownFinishListener() { @OverrIDe public voID countDownFinished() { Toast.makeText(MainActivity.this,"倒计时结束",Toast.LENGTH_SHORT).show(); } }); cdv.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { cdv.startCountDown(); } }); }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的android自定义圆形倒计时显示控件全部内容,希望文章能够帮你解决android自定义圆形倒计时显示控件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)