Implementation code of Android countdown function

I haven't blogged for a long time. At the end of the year, I summarized some technical problems encountered in the next year, as well as some custom controls, such as countdown function

First, the implementation of countdown

1. Handler 2. Timer 3. Rxjava 4. Valueanimator 5. Others

Among these methods, I chose valueanimator, mainly because its API is friendly and we don't need to encapsulate too many things. I won't write the specific use method separately. The following code has comments

Project address

Project picture

Code implementation:

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();
  }
}

summary

The above is the implementation code of Android countdown function introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support to our website! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>