欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android倒计时功能的实现代码

程序员文章站 2022-03-31 14:08:08
好久没有写博客了,趁着年末,总结了下最近一年所遇到的一些技术问题,还有一些自定义控件,比如倒计时功能 首先倒计时的实现方式 1.handler 2.timer 3.rxjava...

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

首先倒计时的实现方式

1.handler
2.timer
3.rxjava
4.valueanimator
5.其他

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

项目地址

项目图片

Android倒计时功能的实现代码

代码实现:

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

总结

以上所述是小编给大家介绍的android倒计时功能的实现代码,希望对大家有所帮助