안 드 로 이 드 모방 경 동 초살 카운트다운 코드

효과 도 는 다음 과 같다.
这里写图片描述  
내 가 모방 한 경 동 은 분 모듈 이기 때문에 이번 에는 주로 초살 모듈 을 묘사 합 니 다!
우선 시간의 배경 을 설정 해 주세요.
drawable 파일 아래 shape 만 들 기miaosha_time.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
 <solid android:color="#000"></solid>
 <corners android:radius="2.5dp"></corners>
</shape>
그리고 주요 구 조 는 단독으로 쓴 다음 에 인용 할 수 있다

**count_down.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="#FFFFFF"
 android:orientation="vertical">
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:gravity="center_vertical">
  <TextView
   android:id="@+id/tv_miaosha"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginLeft="8dp"
   android:text="    "
   android:textColor="#f00"
   android:textSize="20sp" />
  <TextView
   android:id="@+id/tv_miaosha_time"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="5dp"
   android:text="12  "
   android:textSize="20sp" />
  <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   <TextView
    android:id="@+id/tv_miaosha_shi"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:background="@drawable/shape_miaosha_time"
    android:gravity="center"
    android:text="1"
    android:textColor="#fff"
    android:textSize="15sp" />
   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="3dp"
    android:text=":" />
   <TextView
    android:id="@+id/tv_miaosha_minter"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:background="@drawable/shape_miaosha_time"
    android:gravity="center"
    android:text="1"
    android:textColor="#fff"
    android:textSize="15sp" />
   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="3dp"
    android:text=":" />
   <TextView
    android:id="@+id/tv_miaosha_second"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:background="@drawable/shape_miaosha_time"
    android:gravity="center"
    android:text="1"
    android:textColor="#fff"
    android:textSize="15sp" />
  </LinearLayout>
 </LinearLayout>
</LinearLayout>
这里写图片描述  
논리 코드 여기 써 있어 요.

//  handler    UI
private Handler handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   countDown();
   sendEmptyMessageDelayed(0, 1000);
  }
 };
 /**
  *   
  */
 private void countDown() {
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  Date curDate = new Date(System.currentTimeMillis());
  String format = df.format(curDate);
  StringBuffer buffer = new StringBuffer();
  String substring = format.substring(0, 11);
  buffer.append(substring);
  Log.d("ccc", substring);
  Calendar calendar = Calendar.getInstance();
  int hour = calendar.get(Calendar.HOUR_OF_DAY);
  if (hour % 2 == 0) {
   mMiaoshaTimeTv.setText(hour + "  ");
   buffer.append((hour + 2));
   buffer.append(":00:00");
  } else {
   mMiaoshaTimeTv.setText((hour - 1) + "  ");
   buffer.append((hour + 1));
   buffer.append(":00:00");
  }
  String totime = buffer.toString();
  try {
   java.util.Date date = df.parse(totime);
   java.util.Date date1 = df.parse(format);
   long defferenttime = date.getTime() - date1.getTime();
   long days = defferenttime / (1000 * 60 * 60 * 24);
   long hours = (defferenttime - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
   long minute = (defferenttime - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
   long seconds = defferenttime % 60000;
   long second = Math.round((float) seconds / 1000);
   mMiaoshaShiTv.setText("0" + hours + "");
   if (minute >= 10) {
    mMiaoshaMinterTv.setText(minute + "");
   } else {
    mMiaoshaMinterTv.setText("0" + minute + "");
   }
   if (second >= 10) {
    mMiaoshaSecondTv.setText(second + "");
   } else {
    mMiaoshaSecondTv.setText("0" + second + "");
   }
  } catch (ParseException e) {
   e.printStackTrace();
  }
 }
여기 가 오픈 코드 입 니 다.

private void startCountDown() {
  handler.sendEmptyMessage(0);
 }
총결산
위 에서 말 한 것 은 편집장 이 여러분 에 게 소개 한 안 드 로 이 드 모방 경 동 초읽기 코드 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 면 메 시 지 를 남 겨 주세요.편집장 은 제때에 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기