Android 秒杀倒计时

时间:2022-05-30 17:05:07

public class TearDownView extends LinearLayout {

   public long endTime = 0L;
   private boolean mIsAttachedToWindow = false;

   private static final int MESSAGE_WHAT = 0;

   private TearDownHandler mHandler = new TearDownHandler(this);

   private int textSize = 0;
   private TextView dayTextView,hourTextView,minutsTextView,secondTextView;

   public TearDownView(Context context) {
      super(context);
      init(context,null);

   }

   public TearDownView(Context context, AttributeSet attributeSet) {
      super(context, attributeSet);
      init(context,attributeSet);

   }

   public TearDownView(Context context,
                  AttributeSet attributeSet, int paramInt) {
      super(context, attributeSet, paramInt);
      init(context,attributeSet);
   }

   public void startTearDown(long endTime) {
      this.endTime = endTime;
      if(mIsAttachedToWindow) {
         mHandler.removeMessages(MESSAGE_WHAT);
         mHandler.sendEmptyMessageDelayed(MESSAGE_WHAT, 1000);
      }
   }

   private void init(Context context, AttributeSet attributeSet){

      LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      inflater.inflate(R.layout.tear_down_layout,this);

      dayTextView = (TextView) findViewById(R.id.day_time);
      hourTextView = (TextView) findViewById(R.id.hour_time);
      minutsTextView = (TextView) findViewById(R.id.minuts_time);
      secondTextView = (TextView) findViewById(R.id.second_time);



   }


   @Override
   protected void onAttachedToWindow() {
      // TODO Auto-generated method stub  mHandler.removeMessages(MESSAGE_WHAT);
      mHandler.sendEmptyMessageDelayed(MESSAGE_WHAT, 1000);
      mIsAttachedToWindow = true;
      super.onAttachedToWindow();
   }

   @Override
   protected void onDetachedFromWindow() {
      // TODO Auto-generated method stub  mHandler.removeMessages(MESSAGE_WHAT);
      mIsAttachedToWindow = false;
      super.onDetachedFromWindow();
   }

   /*public final Handler mHandler = new Handler() {
      public void handleMessage(Message msg) {
         Log.e(tag, "handleMessage, mIsAttachedToWindow = " + mIsAttachedToWindow);
         if (msg.what == 0 && mIsAttachedToWindow) {
            if (leftTime > 0L) {
               leftTime--;

               String str = TimeUtil.formatDiffDate(leftTime);
               setText("剩余 " + str);
               
               mHandler.sendEmptyMessageDelayed(0, 1000);
            } else {
               setText("抢购结束");
            }
         }
      };
   };*/

   public void tearDown() {
      if(mIsAttachedToWindow) {
         long remainTime = (endTime - System.currentTimeMillis()) / 1000;
         if (remainTime > 0) {
            String str = TimeUtil.formatDiffDate2(remainTime);
            String[] splitStr = str.split(":");
            if(splitStr.length == 4){

               dayTextView.setText(splitStr[1]);
               hourTextView.setText(splitStr[2]);
               minutsTextView.setText(splitStr[3]);
//             secondTextView.setText(splitStr[3]);
            }

            if(splitStr.length == 3){
               dayTextView.setText(splitStr[1]);
               hourTextView.setText(splitStr[2]);
               minutsTextView.setText(splitStr[3]);
//             secondTextView.setText("0");
            }

            mHandler.sendEmptyMessageDelayed(MESSAGE_WHAT, 1000);

            new CountDownTimer(1100,100){

               @Override
               public void onTick(long millisUntilFinished) {
                  secondTextView.setText(millisUntilFinished/100+"");
               }

               @Override
               public void onFinish() {

               }
            }.start();
         } else {
//          setText("抢购结束");
         }
      }
   }




   public static class TearDownHandler extends Handler {
      private WeakReference<TearDownView> mTextViewRef;

      public TearDownHandler(TearDownView view) {
         mTextViewRef = new WeakReference<TearDownView>(view);
      }

      @Override
      public void handleMessage(Message msg) {
         TearDownView textView = mTextViewRef.get();
         if(null != textView) {
            removeMessages(MESSAGE_WHAT);
            textView.tearDown();
         }
      }
   }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <TextView
        android:text="12"
        android:textColor="@color/white"
        android:id="@+id/day_time"
        style="@style/text_round_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/text_round" />

    <TextView
        style="@style/text_round_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/y25"
        android:text=":"/>

    <TextView
        android:id="@+id/hour_time"
        style="@style/text_round_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:background="@drawable/text_round" />

    <TextView
        style="@style/text_round_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="@dimen/y25"
        android:text=":" />


    <TextView
        android:id="@+id/minuts_time"
        style="@style/text_round_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:background="@drawable/text_round"/>

    <TextView
        style="@style/text_round_style"
        android:textSize="@dimen/y25"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=":"/>


    <TextView
        android:id="@+id/second_time"
        style="@style/text_round_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:background="@drawable/text_round" />


</LinearLayout>

调用 

tearDown.startTearDown(1499792292894L);