Android自定义view倒计时60秒
一个简单的自定义view。在里面封装了时间的倒计时,以及距离现在时间的时间计算
publicclassTimerTextViewextendsLinearLayout{ //时间变量 privatelongsecond; privateTextViewtv_Time; privateTextViewtv_Unit; RefreshCallBackrefreshCallBack; publicTimerTextView(Contextcontext,AttributeSetattrs,intdefStyleAttr){ super(context,attrs,defStyleAttr); initView(context); } publicTimerTextView(Contextcontext,AttributeSetattrs){ super(context,attrs); initView(context); } publicTimerTextView(Contextcontext){ super(context); initView(context); } privatevoidinitView(Contextcontext){ //加载布局 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 protectedvoidonDetachedFromWindow(){ super.onDetachedFromWindow(); //在控件被销毁时移除消息 handler.removeMessages(0); } privatebooleanisRun=true;//是否启动了 privateHandlerhandler=newHandler(Looper.getMainLooper()){ @Override publicvoidhandleMessage(Messagemsg){ switch(msg.what){ case0: if(isRun){ if(second>0){ second=second-1; handler.sendEmptyMessageDelayed(0,1000); }else{ if(null!=refreshCallBack){ refreshCallBack.refreshCallBack(true); isRun=false; } } } break; } } }; publicbooleanisRun(){ returnisRun; } /** *开始计时 */ publicvoidstart(){ isRun=true; handler.removeMessages(0); handler.sendEmptyMessage(0); } /** *结束计时 */ publicvoidstop(){ isRun=false; } publicvoiddiffTime(StringendTime){ SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss",Locale.CHINA); StringstartTime=sdf.format(newDate()); Stringformat="yyyy-MM-ddhh:mm:ss"; //按照传入的格式生成一个simpledateformate对象 SimpleDateFormatsd=newSimpleDateFormat(format); longnd=1000*24*60*60;//一天的毫秒数 longnh=1000*60*60;//一小时的毫秒数 longnm=1000*60;//一分钟的毫秒数 longns=1000;//一秒钟的毫秒数longdiff;try{ //获得两个时间的毫秒时间差异 longdiff=0; try{ diff=sd.parse(endTime).getTime()-sd.parse(startTime).getTime(); }catch(ParseExceptione){ e.printStackTrace(); } if(diff<0){ if(null!=refreshCallBack){ refreshCallBack.showCallBack(false); } return; }else{ if(null!=refreshCallBack){ refreshCallBack.showCallBack(true); } longday=diff/nd;//计算差多少天 if(day>0){ tv_Time.setText(String.valueOf(day)); tv_Unit.setText("天"); }else{ longhour=diff%nd/nh;//计算差多少小时 if(hour>0){ tv_Time.setText(String.valueOf(hour)); tv_Unit.setText("小时"); }else{ longmin=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); } } } } } publicvoidsetTextViewSize(intsize){ if(null!=tv_Time){ tv_Time.setTextSize(size); } if(null!=tv_Unit){ tv_Unit.setTextSize(size); } } publicvoidsetTextViewSpace(Stringtype){ if("Big".equals(type)){ LinearLayout.LayoutParamslp2=(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)); }elseif("Middle".equals(type)){ tv_Time.setPadding(12,0,12,0); LinearLayout.LayoutParamslp2=(LayoutParams)tv_Time.getLayoutParams(); lp2.setMargins(0,0,12,0); tv_Time.setLayoutParams(lp2); }else{ tv_Time.setPadding(8,0,8,0); LinearLayout.LayoutParamslp2=(LayoutParams)tv_Time.getLayoutParams(); lp2.setMargins(0,0,8,0); tv_Time.setLayoutParams(lp2); } } publicvoidsetRefreshCallBack(RefreshCallBackrefreshCallBack){ this.refreshCallBack=refreshCallBack; } publicinterfaceRefreshCallBack{ publicvoidrefreshCallBack(booleanflag); publicvoidshowCallBack(booleanflag); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。