2.Timer
3.RxJava
4.ValueAnimator
5.其他@H_301_0@这些方式中,我选择了ValueAnimator,主要是它的API比较友好,不需要我们去封装太多东西,具体的使用方式我就不单独写了,下面的代码都有备注@H_301_0@项目地址@H_301_0@项目图片@H_301_0@@H_301_0@代码实现:
package com.example.countdownvIEw;import androID.animation.Animator;import androID.animation.AnimatorListenerAdapter;import androID.animation.ValueAnimator;import androID.content.Context;import androID.content.res.TypedArray;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Paint;import androID.graphics.RectF;import androID.util.AttributeSet;import androID.vIEw.VIEw;import androID.vIEw.animation.linearInterpolator;public class CountDownVIEw extends VIEw { //圆轮颜色 private int mRingcolor; //圆轮宽度 private float mRingWIDth; //宽度 private int mWIDth; //高度 private int mHeight; private Paint mPaint; //圆环的矩形区域 private RectF mRectF; // private int mCountdownTime; private float mCurrentProgress; private OnCountDownFinishListener mListener; ValueAnimator valueAnimator; 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,color.RED); mCountdownTime = a.getInteger(R.styleable.CountDownVIEw_countdownTime,10); mRingWIDth=a.getDimension(R.styleable.CountDownVIEw_ringWIDth,2); a.recycle(); mPaint = new Paint(Paint.ANTI_AliAS_FLAG); /** *圆环 */ //颜色 mPaint.setcolor(mRingcolor); //空心 mPaint.setStyle(Paint.Style.stroke); mPaint.setAntiAlias(true); // 消除锯齿 //宽度 mPaint.setstrokeWIDth(mRingWIDth); } 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); canvas.drawArc(mRectF,-90,mCurrentProgress,false,mPaint); } 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 = 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 resumeCountDown(){ if (valueAnimator!=null){ valueAnimator.resume(); } } /** * 暂停 */ public voID pauseCountDown(){ if (valueAnimator!=null){ valueAnimator.pause(); } } /** * 停止倒计时 */ public voID stopCountDown(){ if (valueAnimator!=null){ valueAnimator.cancel(); } } public voID setCountDownFinishListener(OnCountDownFinishListener mListener) { this.mListener = mListener; } public interface OnCountDownFinishListener { voID countDownFinished(); }}@H_301_0@总结@H_301_0@以上所述是小编给大家介绍的AndroID倒计时功能的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢! 总结
以上是内存溢出为你收集整理的Android倒计时功能的实现代码全部内容,希望文章能够帮你解决Android倒计时功能的实现代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)