android程序启动画面之Splash总结

来源:互联网 发布:淘宝关键词你懂的 编辑:程序博客网 时间:2024/06/08 14:27

方法一:

很多应用都会有一个启动界面。欢迎画面慢慢隐现,然后慢慢消隐。实现这种效果的方法有两种(暂时只发现两种)
1、使用两个Activity,程序启动时候load第一张Activity,然后由tick触发N秒钟后startActivity另外一张Activity。
2、使用一个Activity,可以用到View.gone() 这个方法。把Acitivity的某些元素移除。

1、两个Activity:
首先是AndroidManifest.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
      package="com.sunshine.splash"
      android:versionCode="1"
      android:versionName="1.0"&gt;
    <applicationandroid:icon="@drawable/icon";android:label="@string/app_name"> ;
        <activityandroid:name=".Splash"
                  android:label="@string/app_name"> ;
            <intent-filter>
                <actionandroid:name="android.intent.action.MAIN"/>
                <categoryandroid:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    <activityandroid:name="Main">
    </activity>
</application>
    <uses-sdkandroid:minSdkVersion="3"/>
</manifest>

然后是JAVA代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
packagenet.hlovey.splash;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.os.Handler;
 
publicclass Splash extendsActivity {   
 
    privatefinal int SPLASH_DISPLAY_LENGHT = 3000;//延迟三秒
 
    @Override
    publicvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        newHandler().postDelayed(newRunnable(){
 
         @Override
         publicvoid run() {
             Intent mainIntent = newIntent(Splash.this,Main.class);
             Splash.this.startActivity(mainIntent);
                 Splash.this.finish();
         }
             
        }, SPLASH_DISPLAY_LENGHT);
    }
}

当然可以再Splash中加入动画效果。(我觉得先要布局好AndroidManifest.xml。因为那才是工程的索引文件。首先在那要有一个统筹!而不是先写java code。然后逐步往xml中增加 ,这说明对整个项目没有一个统筹的设计)

方法二:

androidmanifest.xml就不多说了。先看布局代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xmlversion=”1.0″ encoding=”utf-8″?>
<LinearLayoutxmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>  
     <LinearLayoutandroid:id=”@+id/splashscreen” android:orientation=”vertical”
          android:layout_width=”fill_parent” android:layout_height=”fill_parent”>
          <ImageViewandroid:layout_width=”wrap_content”
               android:layout_height=”wrap_content” android:src=”@drawable/splash”
               android:layout_gravity=”center”
               android:layout_marginTop=”130px”/>
          <TextView
               android:id=”@+id/info”
               android:layout_width=”fill_parent”
               android:layout_height=”wrap_content”
               android:gravity=”center”
               android:paddingTop=”10px”
               android:text=”This is a splash!!”/>
     </LinearLayout>    
<WebViewandroid:id=”@+id/browser”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” android:layout_weight=”1″/>
</LinearLayout>

有一个id为splashscreen 的linearlayout,是程序启动时显现的部分。id为browser是程序的主界面显示部分。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
packagenet.hlovey.s;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.content.DialogInterface;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.util.Log;
importandroid.view.LayoutInflater;
importandroid.view.animation.Animation;
importandroid.view.animation.AnimationUtils;
importandroid.widget.LinearLayout;
importandroid.widget.TextView;
importandroid.widget.Toast;
publicclass WebGameActivity extendsActivity {
 
privateWebView webView;
     
privateHandler mHandler = newHandler();
 
privatestatic final String TAG = "WebGameActivity";  
//菜单
privatestatic final int MENU_RELOAD = Menu.FIRST;
privatestatic final int MENU_HELP = Menu.FIRST + 1;
privatestatic final int MENU_ABOUT = Menu.FIRST + 2;
privatestatic final int MENU_CLOSE = Menu.FIRST + 3;  
privateint staus = 0;
    
privatestatic final int STOPSPLASH = 0;
//time in milliseconds
privatestatic final long SPLASHTIME = 1000;
    
privateLinearLayout splash;
privateTextView tv;
 
privateAnimation myAnimation_Alpha;
privateAnimation animatinoGone ;
 
privateHandler splashHandler = newHandler() {
    publicvoid handleMessage(Message msg) {
         switch(msg.what) {
         caseSTOPSPLASH:
              if( staus == 1){               
                splash.startAnimation(animatinoGone);
                splash.setVisibility(View.GONE);
                break;
              }
              sendEmptyMessageDelayed(STOPSPLASH, SPLASHTIME);
         }
         super.handleMessage(msg);
    }
};
 
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_PROGRESS);//去标题栏
    setContentView(R.layout.main);   
    animatinoGone = AnimationUtils.loadAnimation(this,R.anim.alpha_gone);//动画效果
    myAnimation_Alpha = AnimationUtils.loadAnimation(this,R.anim.alpha_action);//动画效果
     
    splash = (LinearLayout) findViewById(R.id.splashscreen);
    tv = (TextView) findViewById(R.id.info);
    tv.setText("正在建立数据连接");
    splash.startAnimation(myAnimation_Alpha);
     
    Message msg = newMessage();
    msg.what = STOPSPLASH;
    splashHandler.sendMessageDelayed(msg, SPLASHTIME);
 
}
0 0
原创粉丝点击