Android Splash界面簡單實現

jopen 10年前發布 | 40K 次閱讀 Android Android開發 移動開發

大家都知道,android的應用很多的開始都是有一個Splash界面,

如:


Android Splash界面簡單實現

實現起來其實很簡單

 

實現原理: 

 

    通過TimerTimerTask,Handler的結合。Timer來計時,TimerTask來判斷是不是已經滿足設定時間,hanlder來具體啟動新的Activity


Android Splash界面簡單實現

    import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.os.Handler;  
import android.os.Message;  
import android.view.MotionEvent;  

public class SplashActivity extends Activity {  
    private long startTime;  
    private boolean touched=false;  
    private Timer timer ;  

    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        this.setContentView(R.layout.splash);  
        //開啟 定時器  
        timer = new Timer(true);  
        startTime = System.currentTimeMillis();  
        timer.schedule(task, 0, 1);  
    }  


    private final TimerTask task = new TimerTask() {  
        public void run() {  
            if (task.scheduledExecutionTime() - startTime == 2000 || touched) {  
                Message message = new Message();  
                message.what = 0;  
                timerHandler.sendMessage(message);  
                timer.cancel();  
                this.cancel();  
            }  

        }  
    };  

    private final Handler timerHandler = new Handler() {  
        public void handleMessage(Message msg) {  
            switch (msg.what) {  
            case 0:  
                SplashActivity.this.finish();  
                // 跳轉到新的 activity  
                Intent intent = new Intent(SplashActivity.this,TabMain.class);  
                SplashActivity.this.startActivity(intent);  


                break;  
            }  
            super.handleMessage(msg);  
        }  
    };  


    /** 
     * 點擊直接跳轉 
     */  
    public boolean onTouchEvent(MotionEvent event) {  
        if (event.getAction() == MotionEvent.ACTION_DOWN) {  
            touched = true;  
        }  
        return true;  
    }  
}  </pre></span>
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!