Android自定义view倒计时60秒

发布时间:2020-09-25 03:46:11 作者:MatrixMind
来源:脚本之家 阅读:219

一个简单的自定义view。在里面封装了时间的倒计时,以及距离现在时间的时间计算

public class TimerTextView extends LinearLayout{
  // 时间变量
  private long second;
  private TextView tv_Time;
  private TextView tv_Unit;
  RefreshCallBack refreshCallBack;
 
  public TimerTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initView(context);
  }
 
  public TimerTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView(context);
  }
 
  public TimerTextView(Context context) {
    super(context);
    initView(context);
  }
 
  private void initView(Context context) {
    // 加载布局
    LayoutInflater.from(context).inflate(R.layout.timer_text_view, this);
    tv_Time = (TextView) findViewById(R.id.countdown_time);
    tv_Unit = (TextView) findViewById(R.id.countdown_unit);
  }
 
  @Override
  protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    // 在控件被销毁时移除消息
    handler.removeMessages(0);
  }
 
  private boolean isRun = true; // 是否启动了
  private Handler handler = new Handler(Looper.getMainLooper()) {
    @Override
    public void handleMessage(Message msg) {
      switch (msg.what) {
        case 0:
          if (isRun) {
            if (second > 0) {
              second = second - 1;
              handler.sendEmptyMessageDelayed(0, 1000);
            }else{
              if(null != refreshCallBack){
                refreshCallBack.refreshCallBack(true);
                isRun = false;
              }
            }
          }
          break;
      }
    }
  };
 
 
  public boolean isRun() {
    return isRun;
  }
 
  /**
   * 开始计时
   */
  public void start() {
    isRun = true;
    handler.removeMessages(0);
    handler.sendEmptyMessage(0);
  }
 
  /**
   * 结束计时
   */
  public void stop() {
    isRun = false;
  }
 
  public void diffTime(String endTime) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    String startTime = sdf.format(new Date());
    String format = "yyyy-MM-dd hh:mm:ss";
    //按照传入的格式生成一个simpledateformate对象
    SimpleDateFormat sd = new SimpleDateFormat(format);
 
    long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
    long nh = 1000 * 60 * 60;//一小时的毫秒数
    long nm = 1000 * 60;//一分钟的毫秒数
    long ns = 1000;//一秒钟的毫秒数long diff;try {
    //获得两个时间的毫秒时间差异
    long diff = 0;
    try {
      diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
    } catch (ParseException e) {
      e.printStackTrace();
    }
    if (diff < 0) {
      if(null != refreshCallBack){
        refreshCallBack.showCallBack(false);
      }
      return ;
    } else {
      if(null != refreshCallBack){
        refreshCallBack.showCallBack(true);
      }
      long day = diff / nd;//计算差多少天
      if (day > 0) {
        tv_Time.setText(String.valueOf(day));
        tv_Unit.setText("天");
      } else {
        long hour = diff % nd / nh;//计算差多少小时
        if (hour > 0) {
          tv_Time.setText(String.valueOf(hour));
          tv_Unit.setText("小时");
        } else {
          long min = diff % nd % nh / nm;//计算差多少分钟
          if (min > 0) {
            tv_Time.setText(String.valueOf(min));
            tv_Unit.setText("分钟");
          } else {
            second = diff%nd%nh%nm/ns;//计算差多少秒//输出结果
//            if(min > 0){
//              stringBuffer.append(sec+"秒");
//            }
            handler.removeMessages(0);
            handler.sendEmptyMessage(0);
 
            tv_Unit.setText("即将开始");
            tv_Time.setVisibility(GONE);
          }
        }
      }
    }
  }
 
  public void setTextViewSize(int size){
    if(null != tv_Time){
      tv_Time.setTextSize(size);
    }
    if(null != tv_Unit){
      tv_Unit.setTextSize(size);
    }
  }
 
  public void setTextViewSpace(String type){
    if("Big".equals(type)){
      LinearLayout.LayoutParams lp2 = (LayoutParams) tv_Time.getLayoutParams();
      lp2.setMargins(0, 0, DensityUtil.dip2px(tv_Time.getContext(), 12), 0);
      tv_Time.setLayoutParams(lp2);     
    tv_Time.setBackground(getResources().getDrawable(R.drawable.bg_video_count_down));
    }else if("Middle".equals(type)){
      tv_Time.setPadding(12, 0, 12, 0);
      LinearLayout.LayoutParams lp2 = (LayoutParams) tv_Time.getLayoutParams();
      lp2.setMargins(0, 0,12, 0);
      tv_Time.setLayoutParams(lp2);
    }else {
      tv_Time.setPadding(8, 0, 8, 0);
      LinearLayout.LayoutParams lp2 = (LayoutParams) tv_Time.getLayoutParams();
      lp2.setMargins(0, 0, 8, 0);
      tv_Time.setLayoutParams(lp2);
    }
  }
 
  public void setRefreshCallBack(RefreshCallBack refreshCallBack){
    this.refreshCallBack = refreshCallBack;
  }
 
  public interface RefreshCallBack {
    public void refreshCallBack(boolean flag);
    public void showCallBack(boolean flag);
  }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。 

推荐阅读:
  1. android 倒计时实现
  2. Android怎么自定义view组件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android view 倒计时

上一篇:C#复制和深度复制的实现方法

下一篇:一个string类的简单实现案例

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》