Android自定義view倒計(jì)時(shí)60秒
一個(gè)簡單的自定義view。在里面封裝了時(shí)間的倒計(jì)時(shí),以及距離現(xiàn)在時(shí)間的時(shí)間計(jì)算
public class TimerTextView extends LinearLayout{ // 時(shí)間變量 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(); // 在控件被銷毀時(shí)移除消息 handler.removeMessages(0); } private boolean isRun = true; // 是否啟動(dòng)了 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; } /** * 開始計(jì)時(shí) */ public void start() { isRun = true; handler.removeMessages(0); handler.sendEmptyMessage(0); } /** * 結(jié)束計(jì)時(shí) */ 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"; //按照傳入的格式生成一個(gè)simpledateformate對(duì)象 SimpleDateFormat sd = new SimpleDateFormat(format); long nd = 1000 * 24 * 60 * 60;//一天的毫秒數(shù) long nh = 1000 * 60 * 60;//一小時(shí)的毫秒數(shù) long nm = 1000 * 60;//一分鐘的毫秒數(shù) long ns = 1000;//一秒鐘的毫秒數(shù)long diff;try { //獲得兩個(gè)時(shí)間的毫秒時(shí)間差異 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;//計(jì)算差多少天 if (day > 0) { tv_Time.setText(String.valueOf(day)); tv_Unit.setText("天"); } else { long hour = diff % nd / nh;//計(jì)算差多少小時(shí) if (hour > 0) { tv_Time.setText(String.valueOf(hour)); tv_Unit.setText("小時(shí)"); } else { long min = diff % nd % nh / nm;//計(jì)算差多少分鐘 if (min > 0) { tv_Time.setText(String.valueOf(min)); tv_Unit.setText("分鐘"); } else { second = diff%nd%nh%nm/ns;//計(jì)算差多少秒//輸出結(jié)果 // 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); } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android手勢密碼--設(shè)置和校驗(yàn)功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android手勢密碼--設(shè)置和校驗(yàn)功能的實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考校驗(yàn)價(jià)值,需要的朋友可以參考下2018-05-05完美解決關(guān)于禁止ViewPager預(yù)加載的相關(guān)問題
本篇文章主要介紹了完美解決關(guān)于禁止ViewPager預(yù)加載的相關(guān)問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09Android?Jetpack?組件LiveData源碼解析
這篇文章主要為大家介紹了Android?Jetpack?組件LiveData源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Android解析json數(shù)組對(duì)象的方法及Apply和數(shù)組的三個(gè)技巧
這篇文章主要介紹了Android解析json數(shù)組對(duì)象的方法及Apply和數(shù)組的三個(gè)技巧的相關(guān)資料,需要的朋友可以參考下2015-12-12Android使用Volley實(shí)現(xiàn)上傳文件功能
這篇文章主要介紹了Android使用Volley實(shí)現(xiàn)上傳文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android編程實(shí)現(xiàn)自定義ImageView圓圖功能的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義ImageView圓圖功能的方法,結(jié)合實(shí)例形式分析了Android自定義ImageView及實(shí)現(xiàn)圓圖效果的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08Flutter實(shí)現(xiàn)彈窗攔截器的示例代碼
彈窗的排隊(duì)執(zhí)行在App中是一個(gè)很常見的應(yīng)用場景,這篇文章為大家介紹了兩個(gè)Flutter實(shí)現(xiàn)彈窗攔截器的示例代碼,感興趣的小伙伴可以學(xué)習(xí)一下2023-09-09