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时间后,发送消息
    }
}

 

三、总结:

上面两种方法都可以实现应用启动前的开机画面,但在实际开发中还是建议使用第一种较好,因为主界面的代码不宜过多,应当简洁。

 

posted @ 2016-03-23 22:15  千个太阳在手中  阅读(5844)  评论(0编辑  收藏  举报