Android中使用Handler及Countdowntimer實(shí)現(xiàn)包含倒計(jì)時(shí)的閃屏頁(yè)面
上一篇博文《Android中Handler使用淺析》通過(guò)實(shí)現(xiàn)倒計(jì)時(shí)閃屏頁(yè)面的制作引出了Handler的使用方法以及實(shí)現(xiàn)原理,博文末尾也提到了實(shí)現(xiàn)過(guò)程中的Bug,有興趣的朋友可以點(diǎn)擊鏈接回去看看。今天通過(guò)使用Handler以及CountDownTimer來(lái)實(shí)現(xiàn)完整版的倒計(jì)時(shí)閃屏(不會(huì)出現(xiàn)在退出閃屏頁(yè)后,依然會(huì)跳轉(zhuǎn)頁(yè)面的現(xiàn)象)。
1. 實(shí)現(xiàn)效果如下:
1.1 正常進(jìn)入跳轉(zhuǎn)的效果以及l(fā)og顯示


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


對(duì)比上篇博文的實(shí)現(xiàn),退出后計(jì)時(shí)停止且不會(huì)再跳到新的界面
2. 實(shí)現(xiàn)方法
2.1 去除actionBar
閃屏頁(yè)面一般都為全屏顯示,這里我們首先需要去除actionBar,在res/values/styles.xml中設(shè)置:

這里也建議大家在后期開(kāi)發(fā)中盡量不要用死板的actionBar,可以根據(jù)項(xiàng)目需求使用ToolBar或者自定義TitleBar組件來(lái)替代actionBar,這樣的話界面設(shè)計(jì)會(huì)更加靈活。
2.2 layout布局
這里僅僅設(shè)置布局背景圖片,以及在右上角添加TextView用于顯示倒計(jì)時(shí),做的有點(diǎn)糙,見(jiàn)諒,代碼如下:
<?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實(shí)現(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í)行頁(yè)面跳轉(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);
}
}
//頁(yè)面跳轉(zhuǎn)的方法
private void startMainActivity(){
Intent intent = new Intent(this,Main3Activity.class);
startActivity(intent);
finish();//完成跳轉(zhuǎn)后銷毀閃屏頁(yè)(從棧內(nèi)移除)
}
class MyCountDownTimer extends CountDownTimer {
/**
* @param millisInFuture
* 表示以毫秒為單位 倒計(jì)時(shí)的總數(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("倒計(jì)時(shí)(" + millisUntilFinished / 1000 + ")");
Log.i("tag","倒計(jì)時(shí)"+millisUntilFinished / 1000);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//閃屏頁(yè)銷毀時(shí)將消息對(duì)象從消息隊(duì)列移除并結(jié)束倒計(jì)時(shí)
myHandler.removeCallbacksAndMessages(null);
mc.cancel();
Log.i("tag","destory");
}
}
以上所述是小編給大家介紹的Android中使用Handler及Countdowntimer實(shí)現(xiàn)包含倒計(jì)時(shí)的閃屏頁(yè)面,希望對(duì)大家有所幫助!
相關(guān)文章
Andriod事件分發(fā)事件由來(lái)初識(shí)
這篇文章主要為大家講解了Andriod事件分發(fā)事件由來(lái)的初步認(rèn)識(shí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
RecyclerView嵌套R(shí)ecyclerView滑動(dòng)卡頓的解決方法
這篇文章主要為大家詳細(xì)介紹了RecyclerView嵌套R(shí)ecyclerView滑動(dòng)卡頓的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android實(shí)現(xiàn)爆炸式菜單按鈕彈出效果
這篇文章主要介紹了Android實(shí)現(xiàn)爆炸式菜單按鈕彈出效果,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Flutter最小刷新范圍探索ValueListenableBuilder使用詳解
這篇文章主要為大家介紹了Flutter最小刷新范圍探索ValueListenableBuilder使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android ListView自動(dòng)顯示隱藏布局的實(shí)現(xiàn)方法
這篇文章主要介紹了Android ListView自動(dòng)顯示隱藏布局的實(shí)現(xiàn)方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
android 仿微信demo——注冊(cè)功能實(shí)現(xiàn)(移動(dòng)端)
本篇文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望能給你們提供幫助2021-06-06
Android開(kāi)發(fā)人臉識(shí)別統(tǒng)計(jì)人臉數(shù)
這篇文章主要介紹了Android開(kāi)發(fā)人臉識(shí)別統(tǒng)計(jì)人臉數(shù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
Android自定義Animation實(shí)現(xiàn)View搖擺效果
這篇文章主要為大家詳細(xì)介紹了Android自定義Animation實(shí)現(xiàn)View搖擺效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

