• $Android启动界面(Splash)的两种实现方法


      (一)用2个Activity实现

      用Handler对象的postDelayed方法来实现延迟跳转的目的。

      补充:Handler的常用方法:

    1 //  立即执行Runnable对象  
    2 public final boolean post(Runnable r);  
    3 //  在指定的时间(uptimeMillis)执行Runnable对象  
    4 public final boolean postAtTime(Runnable r, long uptimeMillis);  
    5 //  在指定的时间间隔(delayMillis)执行Runnable对象  
    6 public final boolean postDelayed(Runnable r, long delayMillis);

      1、activity_splash.xml:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6 
     7     <ImageView
     8         android:layout_width="match_parent"
     9         android:layout_height="wrap_content"
    10         android:src="@drawable/ic_launcher" />
    11 
    12 </LinearLayout>

      2、activity_main.xml:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:orientation="vertical" >
     5 
     6     <TextView
     7         android:layout_width="match_parent"
     8         android:layout_height="wrap_content"
     9         android:text="这里是主界面" />
    10 
    11 </LinearLayout>

      3、SplashActivity:

     1 package com.example.splashtest;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.os.Bundle;
     6 import android.os.Handler;
     7 import android.view.Window;
     8 
     9 public class SplashActivity extends Activity {
    10 
    11     private final int SPLASH_DISPLAY_LENGHT = 3000;
    12     private Handler handler;
    13 
    14     @Override
    15     protected void onCreate(Bundle savedInstanceState) {
    16         super.onCreate(savedInstanceState);
    17         getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    18         setContentView(R.layout.activity_splash);
    19 
    20         handler = new Handler();
    21         // 延迟SPLASH_DISPLAY_LENGHT时间然后跳转到MainActivity
    22         handler.postDelayed(new Runnable() {
    23 
    24             @Override
    25             public void run() {
    26                 Intent intent = new Intent(SplashActivity.this,
    27                         MainActivity.class);
    28                 startActivity(intent);
    29                 SplashActivity.this.finish();
    30             }
    31         }, SPLASH_DISPLAY_LENGHT);
    32 
    33     }
    34 }

      4、MainActivity:

     1 package com.example.splashtest;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.view.Menu;
     6 import android.view.MenuItem;
     7 
     8 public class MainActivity extends Activity {
     9 
    10     @Override
    11     protected void onCreate(Bundle savedInstanceState) {
    12         super.onCreate(savedInstanceState);
    13         setContentView(R.layout.activity_main);
    14     }
    15 }

      6、修改AndroidManifest.xml文件:

     1      ...
     2      <activity
     3             android:name=".SplashActivity"
     4             android:label="splash" >
     5             <intent-filter>
     6                 <action android:name="android.intent.action.MAIN" />
     7 
     8                 <category android:name="android.intent.category.LAUNCHER" />
     9             </intent-filter>
    10         </activity>
    11         <activity
    12             android:name=".MainActivity"
    13             android:label="@string/app_name" >
    14         </activity>
    15      ...

       7、在SplashActivity中禁用返回键:

    1 @Override    
    2 public boolean onKeyDown(int keyCode, KeyEvent event) {  
    3   if(keyCode == KeyEvent.KEYCODE_BACK){      
    4     return  true;
    5   }  
    6   return  super.onKeyDown(keyCode, event);     
    7 
    8 } 

      

      (二)用一个Activity实现

      主要利用控件的隐藏来实现。

      1、xml文件:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     android:layout_width="match_parent"
     3     android:layout_height="match_parent"
     4     android:orientation="vertical" >
     5 
     6     <LinearLayout
     7         android:id="@+id/splash_lt"
     8         android:layout_width="match_parent"
     9         android:layout_height="match_parent" >
    10 
    11         <ImageView
    12             android:layout_width="match_parent"
    13             android:layout_height="match_parent"
    14             android:src="@drawable/ic_launcher" />
    15     </LinearLayout>
    16 
    17     <TextView
    18         android:id="@+id/main_tv"
    19         android:layout_width="match_parent"
    20         android:layout_height="wrap_content"
    21         android:text="这是主界面" />
    22 
    23 </LinearLayout>

      2、MainActivity

     1 package com.example.splashtest2;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.os.Handler;
     6 import android.os.Message;
     7 import android.os.SystemClock;
     8 import android.view.Menu;
     9 import android.view.MenuItem;
    10 import android.view.View;
    11 import android.view.Window;
    12 import android.widget.LinearLayout;
    13 
    14 public class MainActivity extends Activity {
    15 
    16     private final int STOP_SPLASH = 0;
    17     private final int SPLASH_TIME = 3000;
    18 
    19     private LinearLayout splashLt;
    20 
    21     private Handler splashHandler = new Handler() {
    22         public void handleMessage(Message msg) {
    23             switch (msg.what) {
    24             case STOP_SPLASH:
    25                 splashLt.setVisibility(View.GONE);
    26                 break;
    27             default:
    28                 break;
    29             }
    30 
    31             super.handleMessage(msg);
    32         }
    33     };
    34 
    35     @Override
    36     protected void onCreate(Bundle savedInstanceState) {
    37         super.onCreate(savedInstanceState);
    38         getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    39         setContentView(R.layout.activity_main);
    40 
    41         splashLt = (LinearLayout) findViewById(R.id.splash_lt);
    42 
    43         Message msg = new Message();
    44         msg.what = STOP_SPLASH;
    45 
    46         // 注:这里必须用延迟发送消息的方法,否则ImageView不会显示出来
    47         splashHandler.sendMessageDelayed(msg, SPLASH_TIME);
    48     }
    49 
    50 }

      (三)小结

      建议使用第一种方法,用两个Activity实现,因为MainActivity中的代码不宜过多。

  • 相关阅读:
    Embedding技术在商业搜索与推荐场景的实践
    Elasticsearch 性能调优:段合并(Segment merge)
    elasticsearch关于index性能优化
    nginx优化图片防盗链
    NextCloud搭建
    nginx优化限制连接请求limit_req
    Android 去除标题栏和状态栏的方法
    Struts2 集合属性接收问题探讨
    解决JAVA调用mysqldump备份数据库时文件为空的问题
    Hibernate自定义字段查询 注意事项
  • 原文地址:https://www.cnblogs.com/jiayongji/p/5325282.html
Copyright © 2020-2023  润新知