重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Android自定义view倒计时60秒

一个简单的自定义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);
  }
 
}

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


网页标题:Android自定义view倒计时60秒
文章地址:http://cqcxhl.cn/article/jjhihg.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP