【Android】android開發之splash閃屏頁的四種實現方式,啟動頁的實現教程。
阿新 • • 發佈:2019-01-02
作者:程式設計師小冰,GitHub主頁:https://github.com/QQ986945193
https://github.com/QQ986945193/XiaoBingSplashMoreMethod
首先給大家看一下今天實現的效果圖(其他三種都差不太多底下詳細介紹):
這個啟動頁實現的方法是四種,兩種是利用handler,其它兩種是利用了動畫的方式。
具體給大家貼一下原始碼吧,很簡單。
佈局檔案就一個圖片,mainfest新增上我們的類就行了。
java實現程式碼如下:
(AndroidStudio版)github下載地址:package xiaobingsplashmoremethod.qq986945193.xiaobingsplashmoremethod.xiaobingsplashmoremethod; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.widget.LinearLayout; /** * @author :程式設計師小冰 * @新浪微博 :http://weibo.com/mcxiaobing * @GitHub:https://github.com/QQ986945193 * @CSDN部落格: http://blog.csdn.net/qq_21376985 * @交流Qq :986945193 * 類名:splash閃屏頁的四種實現方式 */ public class SplashActivity extends Activity { private LinearLayout ll_splash; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 1: startMainActivity(); break; case 2: startMainActivity(); break; } } }; @SuppressLint("WrongViewCast") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); ll_splash = (LinearLayout) findViewById(R.id.ll_splash); /** * 第一種方法,利用handler自帶的sendEmptyMessageDelayed()方法。 */ mHandler.sendEmptyMessageDelayed(1, 2000); /** * 第二種方法,其實實現原理和第一種是一樣的, */ // // Message message = new Message(); // message.what = 2; // mHandler.sendMessageDelayed(message, 2000); /** * 第三種方法,利用動畫實現 */ // startAdimThree(); /** * 第四種方法,利用動畫實現 */ // StartAniFour(); } private void StartAniFour() { AlphaAnimation start = new AlphaAnimation(0.0f, 1.0f); start.setDuration(1000); // findViewById(R.id.splash).startAnimation(start); start.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { startMainActivity(); } @Override public void onAnimationRepeat(Animation animation) { } }); ll_splash.startAnimation(start); } /** * 開啟動畫 */ private void startAdimThree() { // 動畫集合 AnimationSet set = new AnimationSet(false); // 旋轉動畫 RotateAnimation rotateAnimation = new RotateAnimation(180, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(2000);// 設定動畫時間 rotateAnimation.setFillAfter(true);// 保持動畫狀態 // 縮放動畫 ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(2000);// 設定動畫時間 scaleAnimation.setFillAfter(true);// 保持動畫狀態 // 漸變動畫 AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1); alphaAnimation.setDuration(2000); alphaAnimation.setFillAfter(true);// 保持動畫狀態 // 新增動畫 set.addAnimation(rotateAnimation); set.addAnimation(scaleAnimation); set.addAnimation(alphaAnimation); /* * 設定動畫的監聽事件,當動畫執行完成後,啟動新的activity */ set.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub startMainActivity(); } }); ll_splash.startAnimation(set); } /** * 跳轉到主介面 */ private void startMainActivity() { startActivity(new Intent(SplashActivity.this, MainActivity.class)); finish(); } @Override protected void onDestroy() { super.onDestroy(); // mHandler.removeMessages(1); // mHandler.removeMessages(2); } }
https://github.com/QQ986945193/XiaoBingSplashMoreMethod