Android 實(shí)現(xiàn)閃屏頁和右上角的倒計(jì)時(shí)跳轉(zhuǎn)實(shí)例代碼
以前編程的時(shí)候,遇到倒計(jì)時(shí)的功能時(shí),經(jīng)常自己去寫,但其實(shí)Android已經(jīng)幫封裝好了一個(gè)倒計(jì)時(shí)類CountDownTimer,其實(shí)是將后臺(tái)線程的創(chuàng)建和Handler隊(duì)列封裝成為了一個(gè)方便的類調(diào)用。
閃屏頁用到了handler和CountDownTimer類,還需配置一下Activity的主題,這里是:android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 全屏主題的意思。
給大家展示下效果圖:

代碼如下所示:
package com.example.shanping;
import java.lang.ref.WeakReference;
import com.example.shanping.MyActivity.MyCountDownTimer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private MyCountDownTimer mc;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
mc = new MyCountDownTimer(3000, 1000);
mc.start();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent=new Intent(MainActivity.this,MyActivity.class);
startActivity(intent);
}
}, 3000);
}
private Handler handler=new Handler();
/**
* 繼承 CountDownTimer 防范
*
* 重寫 父類的方法 onTick() 、 onFinish()
*/
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.setText("正在跳轉(zhuǎn)");
}
public void onTick(long millisUntilFinished) {
tv.setText("倒計(jì)時(shí)(" + millisUntilFinished / 1000 + ")");
}
}
}
下面給大家分享一段代碼關(guān)于Android實(shí)現(xiàn)啟動(dòng)閃屏界面效果
閃屏,就是SplashScreen,也可以說是啟動(dòng)畫面,就是啟動(dòng)的時(shí)候,閃(展示)一下,持續(xù)數(shù)秒后,自動(dòng)關(guān)閉。
android的實(shí)現(xiàn)非常簡(jiǎn)單,使用Handler對(duì)象的postDelayed方法就可以實(shí)現(xiàn)。在這個(gè)方法里傳遞一個(gè)Runnable對(duì)象和一個(gè)延遲的時(shí)間。該方法實(shí)現(xiàn)了一個(gè)延遲執(zhí)行的效果,延遲的時(shí)間由第2個(gè)參數(shù)指定,單位是毫秒。第一個(gè)參數(shù)是Runnable對(duì)象,里面包含了延遲后需要執(zhí)行的操作。demo代碼如下:
java code:
package com.mstar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class ActSplashScreen extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shan);
// 閃屏的核心代碼
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(ActSplashScreen.this,DialogTest.class); //從啟動(dòng)動(dòng)畫ui跳轉(zhuǎn)到主ui
startActivity(intent);
ActSplashScreen.this.finish(); // 結(jié)束啟動(dòng)動(dòng)畫界面
}
}, 3000); //啟動(dòng)動(dòng)畫持續(xù)3秒鐘
}
}
xml code:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="閃一下" > </TextView> </LinearLayout>
相關(guān)文章
android AlertDialog的簡(jiǎn)單使用實(shí)例
本篇文章主要介紹了android AlertDialog的簡(jiǎn)單使用實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Android使用RadioGroup實(shí)現(xiàn)底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了Android使用RadioGroup實(shí)現(xiàn)底部導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
android使用SwipeRefreshLayout實(shí)現(xiàn)ListView下拉刷新上拉加載
這篇文章主要為大家詳細(xì)介紹了android使用SwipeRefreshLayout實(shí)現(xiàn)ListView下拉刷新上拉加載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
unity5.6 導(dǎo)出gradle工程 Android Studio 導(dǎo)入問題及處理方法
這篇文章主要介紹了unity5.6 導(dǎo)出gradle工程 Android Studio 導(dǎo)入問題及處理方法,需要的朋友可以參考下2017-12-12
Flutter狀態(tài)管理Bloc之定時(shí)器示例
這篇文章主要為大家詳細(xì)介紹了Flutter狀態(tài)管理Bloc之定時(shí)器示例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

