Android倒计时功能的实现代码

前端之家收集整理的这篇文章主要介绍了Android倒计时功能的实现代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

好久没有写博客了,趁着年末,总结了下最近一年所遇到的一些技术问题,还有一些自定义控件,比如倒计时功能

首先倒计时的实现方式

1.Handler
2.Timer
3.RxJava
4.ValueAnimator
5.其他

这些方式中,我选择了ValueAnimator,主要是它的API比较友好,不需要我们去封装太多东西,具体的使用方式我就不单独写了,下面的代码都有备注

项目地址

项目图片

Android倒计时功能的实现代码

代码实现:

  1. package com.example.countdownview;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorListenerAdapter;
  4. import android.animation.ValueAnimator;
  5. import android.content.Context;
  6. import android.content.res.TypedArray;
  7. import android.graphics.Canvas;
  8. import android.graphics.Color;
  9. import android.graphics.Paint;
  10. import android.graphics.RectF;
  11. import android.util.AttributeSet;
  12. import android.view.View;
  13. import android.view.animation.LinearInterpolator;
  14. public class CountDownView extends View {
  15. //圆轮颜色
  16. private int mRingColor;
  17. //圆轮宽度
  18. private float mRingWidth;
  19. //宽度
  20. private int mWidth;
  21. //高度
  22. private int mHeight;
  23. private Paint mPaint;
  24. //圆环的矩形区域
  25. private RectF mRectF;
  26. //
  27. private int mCountdownTime;
  28. private float mCurrentProgress;
  29. private OnCountDownFinishListener mListener;
  30. ValueAnimator valueAnimator;
  31. public CountDownView(Context context) {
  32. this(context,null);
  33. }
  34. public CountDownView(Context context,AttributeSet attrs) {
  35. this(context,attrs,0);
  36. }
  37. public CountDownView(Context context,AttributeSet attrs,int defStyleAttr) {
  38. super(context,defStyleAttr);
  39. TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CountDownView);
  40. mRingColor = a.getColor(R.styleable.CountDownView_ringColor,Color.RED);
  41. mCountdownTime = a.getInteger(R.styleable.CountDownView_countdownTime,10);
  42. mRingWidth=a.getDimension(R.styleable.CountDownView_ringWidth,2);
  43. a.recycle();
  44. mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  45. /**
  46. *圆环
  47. */
  48. //颜色
  49. mPaint.setColor(mRingColor);
  50. //空心
  51. mPaint.setStyle(Paint.Style.STROKE);
  52. mPaint.setAntiAlias(true); // 消除锯齿
  53. //宽度
  54. mPaint.setStrokeWidth(mRingWidth);
  55. }
  56. public void setCountdownTime(int mCountdownTime) {
  57. this.mCountdownTime = mCountdownTime;
  58. }
  59. @Override
  60. protected void onLayout(boolean changed,int left,int top,int right,int bottom) {
  61. super.onLayout(changed,left,top,right,bottom);
  62. mWidth = getMeasuredWidth();
  63. mHeight = getMeasuredHeight();
  64. mRectF = new RectF(0 + mRingWidth / 2,0 + mRingWidth / 2,mWidth - mRingWidth / 2,mHeight - mRingWidth / 2);
  65. }
  66. @Override
  67. protected void onDraw(Canvas canvas) {
  68. super.onDraw(canvas);
  69. canvas.drawArc(mRectF,-90,mCurrentProgress,false,mPaint);
  70. }
  71. private ValueAnimator getValA(long countdownTime) {
  72. ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,100);
  73. valueAnimator.setDuration(countdownTime);
  74. valueAnimator.setInterpolator(new LinearInterpolator());
  75. valueAnimator.setRepeatCount(0);
  76. return valueAnimator;
  77. }
  78. /**
  79. * 开始倒计时
  80. */
  81. public void startCountDown() {
  82. setClickable(false);
  83. valueAnimator = getValA(mCountdownTime * 1000);
  84. //状态更新监听
  85. valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  86. @Override
  87. public void onAnimationUpdate(ValueAnimator animation) {
  88. float i = Float.valueOf(String.valueOf(animation.getAnimatedValue()));
  89. mCurrentProgress = (int) (360 * (i / 100f));
  90. invalidate();
  91. }
  92. });
  93. valueAnimator.start();
  94. //状态变化结束监听
  95. valueAnimator.addListener(new AnimatorListenerAdapter() {
  96. @Override
  97. public void onAnimationEnd(Animator animation) {
  98. super.onAnimationEnd(animation);
  99. //倒计时结束回调
  100. if (mListener != null) {
  101. mListener.countDownFinished();
  102. }
  103. setClickable(true);
  104. }
  105. });
  106. }
  107. /**
  108. * 恢复
  109. */
  110. public void resumeCountDown(){
  111. if (valueAnimator!=null){
  112. valueAnimator.resume();
  113. }
  114. }
  115. /**
  116. * 暂停
  117. */
  118. public void pauseCountDown(){
  119. if (valueAnimator!=null){
  120. valueAnimator.pause();
  121. }
  122. }
  123. /**
  124. * 停止倒计时
  125. */
  126. public void stopCountDown(){
  127. if (valueAnimator!=null){
  128. valueAnimator.cancel();
  129. }
  130. }
  131. public void setCountDownFinishListener(OnCountDownFinishListener mListener) {
  132. this.mListener = mListener;
  133. }
  134. public interface OnCountDownFinishListener {
  135. void countDownFinished();
  136. }
  137. }

总结

以上所述是小编给大家介绍的Android倒计时功能的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

猜你在找的Android相关文章