android 之 啟動畫面的兩種方法,android兩種方法

來源:互聯網
上載者:User

android 之 啟動畫面的兩種方法,android兩種方法

現在,當我們開啟任意的一個app時,其中的大部分都會顯示一個啟動介面,展示本公司的logo和當前的版本,有的則直接把廣告放到了上面。啟動畫面的可以分為兩種設定方式:一種是兩個Activity實現和一個Ativity實現。下面介紹兩種設定啟動畫面的方式:

一:兩個Activity原始碼:

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.view.Window;public class SplashActivity extends Activity{        private static int SPLASH_DISPLAY_LENGHT= 6000;    //延遲6秒        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);                getWindow().requestFeature(Window.FEATURE_NO_TITLE);//去掉標題        setContentView(R.layout.activity_splash);        new Handler().postDelayed(new Runnable() {            public void run() {                Intent intent = new Intent(SplashActivity.this, MainActivity.class);                startActivity(intent);                SplashActivity.this.finish();   //關閉splashActivity,將其回收,否則按返回鍵會返回此介面            }        }, SPLASH_DISPLAY_LENGHT);    }    }

 

別忘設定AndroidManifest.xml

        <activity             android:name="com.example.andorid_splash_0.SplashActivity"            android:label="splash">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>                    </activity>        <activity            android:name=".MainActivity"            android:label="@string/app_name" >        </activity>

 

容易看出:SplashActivity是先於MainActivity之前啟動,當過了6秒後,才啟動MainActivity。

補充一點知識:

//  立即執行Runnable對象  public final boolean post(Runnable r);  //  在指定的時間(uptimeMillis)執行Runnable對象  public final boolean postAtTime(Runnable r, long uptimeMillis);  //  在指定的時間間隔(delayMillis)執行Runnable對象  public final boolean postDelayed(Runnable r, long delayMillis);

 

二:一個Activity啟動

先看布局檔案:裡面放了兩個充滿螢幕的ImageView和TextView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"     android:orientation="vertical">    <LinearLayout         android:id="@+id/splashScreen"        android:layout_width="match_parent"        android:layout_height="match_parent">        <ImageView             android:id="@+id/iv_image"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:src="@drawable/new00"/>    </LinearLayout>    <TextView         android:layout_width="match_parent"        android:layout_height="match_parent"        android:textSize="100dp"        android:gravity="center"        android:text="主介面"/></LinearLayout>

 

activity的代碼:

import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.os.SystemClock;import android.view.View;import android.widget.ImageView;import android.widget.LinearLayout;public class MainActivity extends Activity {        private LinearLayout splash;    private ImageView iv_image;        private static final int STOPSPLASH = 0;    private static final long SPLASHTIME = 1000;        private Handler splashHandler = new Handler(){        public void handleMessage(Message msg){            switch (msg.what){            case STOPSPLASH:                SystemClock.sleep(4000);   //休眠4s                splash.setVisibility(View.GONE);                break;            }            super.handleMessage(msg);        }    };        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        splash = (LinearLayout) findViewById(R.id.splashScreen);                Message msg = new Message();        msg.what = STOPSPLASH;        splashHandler.sendMessageDelayed(msg, SPLASHTIME);//設定在SPLASHTIME時間後,發送訊息    }}

 

三、總結:

上面兩種方法都可以實現應用啟動前的開機畫面,但在實際開發中還是建議使用第一種較好,因為主介面的代碼不宜過多,應當簡潔。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.