亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android中使用Handler及Countdowntimer實現(xiàn)包含倒計時的閃屏頁面

 更新時間:2017年03月05日 11:15:52   作者:潘侯爺  
這篇文章主要介紹了Android中使用Handler及Countdowntimer實現(xiàn)包含倒計時的閃屏頁面,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

上一篇博文《Android中Handler使用淺析》通過實現(xiàn)倒計時閃屏頁面的制作引出了Handler的使用方法以及實現(xiàn)原理,博文末尾也提到了實現(xiàn)過程中的Bug,有興趣的朋友可以點擊鏈接回去看看。今天通過使用Handler以及CountDownTimer來實現(xiàn)完整版的倒計時閃屏(不會出現(xiàn)在退出閃屏頁后,依然會跳轉(zhuǎn)頁面的現(xiàn)象)。

1. 實現(xiàn)效果如下:

1.1  正常進(jìn)入跳轉(zhuǎn)的效果以及l(fā)og顯示

1.2  倒計時未結(jié)束時退出以及l(fā)og顯示

對比上篇博文的實現(xiàn),退出后計時停止且不會再跳到新的界面

2. 實現(xiàn)方法

2.1 去除actionBar

閃屏頁面一般都為全屏顯示,這里我們首先需要去除actionBar,在res/values/styles.xml中設(shè)置:

這里也建議大家在后期開發(fā)中盡量不要用死板的actionBar,可以根據(jù)項目需求使用ToolBar或者自定義TitleBar組件來替代actionBar,這樣的話界面設(shè)計會更加靈活。

2.2 layout布局

這里僅僅設(shè)置布局背景圖片,以及在右上角添加TextView用于顯示倒計時,做的有點糙,見諒,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_splash"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@mipmap/background_login"
 tools:context="com.mly.panhouye.handlerdemo.SplashActivity">
 <TextView
 android:gravity="right"
 android:id="@+id/tv_time"
 android:textColor="@color/colorAccent"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="SS"
 android:textSize="30sp"/>
</RelativeLayout>

2.3 java實現(xiàn)代碼

2.1中只是去除了app的ActionBar,要做的全屏顯示,仍需要在activity中使用代碼設(shè)置。

package com.mly.panhouye.handlerdemo;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
public class SplashActivity extends AppCompatActivity {
 private MyHandler myHandler = new MyHandler();
 private TextView tv_time;
 private MyCountDownTimer mc;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 //設(shè)置Activity為全屏
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
 WindowManager.LayoutParams.FLAG_FULLSCREEN);
 //去標(biāo)題狀態(tài)欄
 requestWindowFeature(Window.FEATURE_NO_TITLE);
 setContentView(R.layout.activity_splash);
 tv_time = (TextView) findViewById(R.id.tv_time);
 mc = new MyCountDownTimer(5000, 1000);
 mc.start();
 /**
 * 使用handler的postDelayed延遲5秒執(zhí)行頁面跳轉(zhuǎn)
 * (與CountDownTimer的millisInFuture一致)
 */
 myHandler.postDelayed(new Runnable() {
 @Override
 public void run() {
 startMainActivity();
 }
 },5000);
 }
 //將Handler聲明為靜態(tài)內(nèi)部類
 private static class MyHandler extends Handler {
 @Override
 public void handleMessage(Message msg) {
 super.handleMessage(msg);
 }
 }
 //頁面跳轉(zhuǎn)的方法
 private void startMainActivity(){
 Intent intent = new Intent(this,Main3Activity.class);
 startActivity(intent);
 finish();//完成跳轉(zhuǎn)后銷毀閃屏頁(從棧內(nèi)移除)
 }
 class MyCountDownTimer extends CountDownTimer {
 /**
 * @param millisInFuture
 * 表示以毫秒為單位 倒計時的總數(shù)
 * 例如 millisInFuture=1000 表示1秒
 * @param countDownInterval
 * 表示 間隔 多少微秒 調(diào)用一次 onTick 方法
 * 例如: countDownInterval =1000 ; 表示每1000毫秒調(diào)用一次onTick()
 */
 public MyCountDownTimer(long millisInFuture, long countDownInterval) {
 super(millisInFuture, countDownInterval);
 }
 public void onFinish() {
 tv_time.setText("正在跳轉(zhuǎn)");
 }
 public void onTick(long millisUntilFinished) {
 tv_time.setText("倒計時(" + millisUntilFinished / 1000 + ")");
 Log.i("tag","倒計時"+millisUntilFinished / 1000);
 }
 }
 @Override
 protected void onDestroy() {
 super.onDestroy();
 //閃屏頁銷毀時將消息對象從消息隊列移除并結(jié)束倒計時
 myHandler.removeCallbacksAndMessages(null);
 mc.cancel();
 Log.i("tag","destory");
 }
}

以上所述是小編給大家介紹的Android中使用Handler及Countdowntimer實現(xiàn)包含倒計時的閃屏頁面,希望對大家有所幫助!

相關(guān)文章

最新評論