android程序启动画面之Splash总结

来源:互联网 发布:办公隔断网络布线 编辑:程序博客网 时间:2024/05/19 18:39

方法一:

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

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

01<?xml version="1.0" encoding="utf-8"?>
02<manifest xmlns:android="http://schemas.android.com/apk/res/android"
03      package="com.sunshine.splash"
04      android:versionCode="1"
05      android:versionName="1.0"&gt;
06    <application android:icon="@drawable/icon" ;android:label="@string/app_name"> ;
07        <activity android:name=".Splash"
08                  android:label="@string/app_name"> ;
09            <intent-filter>
10                <action android:name="android.intent.action.MAIN"/>
11                <category android:name="android.intent.category.LAUNCHER" />
12            </intent-filter>
13        </activity>
14    <activity android:name="Main">
15    </activity>
16</application>
17    <uses-sdk android:minSdkVersion="3" />
18</manifest>

然后是JAVA代码:

01package net.hlovey.splash;
02import android.app.Activity;
03import android.content.Intent;
04import android.os.Bundle;
05import android.os.Handler;
06 
07public class Splash extends Activity {   
08 
09    private final int SPLASH_DISPLAY_LENGHT = 3000//延迟三秒
10 
11    @Override
12    public void onCreate(Bundle savedInstanceState) {
13        super.onCreate(savedInstanceState);
14        setContentView(R.layout.splash);
15        new Handler().postDelayed(new Runnable(){
16 
17         @Override
18         public void run() {
19             Intent mainIntent = new Intent(Splash.this,Main.class);
20             Splash.this.startActivity(mainIntent);
21                 Splash.this.finish();
22         }
23             
24        }, SPLASH_DISPLAY_LENGHT);
25    }
26}

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

方法二:

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

01<?xml version=”1.0″ encoding=”utf-8″?>
02<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
03android:orientation=”vertical”
04android:layout_width=”fill_parent”
05android:layout_height=”fill_parent”>  
06     <LinearLayout android:id=”@+id/splashscreen” android:orientation=”vertical”
07          android:layout_width=”fill_parent” android:layout_height=”fill_parent”>
08          <ImageView android:layout_width=”wrap_content”
09               android:layout_height=”wrap_content” android:src=”@drawable/splash”
10               android:layout_gravity=”center”
11               android:layout_marginTop=”130px”/>
12          <TextView
13               android:id=”@+id/info”
14               android:layout_width=”fill_parent”
15               android:layout_height=”wrap_content”
16               android:gravity=”center”
17               android:paddingTop=”10px”
18               android:text=”This is a splash!!”/>
19     </LinearLayout>    
20<WebView android:id=”@+id/browser”
21android:layout_width=”fill_parent”
22android:layout_height=”fill_parent” android:layout_weight=”1″/>
23</LinearLayout>

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

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