方法一:
很多应用都会有一个启动界面。欢迎画面慢慢隐现,然后慢慢消隐。实现这种效果的方法有两种(暂时只发现两种)
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" > |
06 | <application android:icon = "@drawable/icon" ; android:label = "@string/app_name" > ; |
07 | < activity android:name = ".Splash" |
08 | android:label = "@string/app_name" > ; |
10 | < action android:name = "android.intent.action.MAIN" /> |
11 | < category android:name = "android.intent.category.LAUNCHER" /> |
14 | < activity android:name = "Main" > |
17 | < uses-sdk android:minSdkVersion = "3" /> |
然后是JAVA代码:
01 | package net.hlovey.splash; |
02 | import android.app.Activity; |
03 | import android.content.Intent; |
04 | import android.os.Bundle; |
05 | import android.os.Handler; |
07 | public class Splash extends Activity { |
09 | private final int SPLASH_DISPLAY_LENGHT = 3000 ; |
12 | public void onCreate(Bundle savedInstanceState) { |
13 | super .onCreate(savedInstanceState); |
14 | setContentView(R.layout.splash); |
15 | new Handler().postDelayed( new Runnable(){ |
19 | Intent mainIntent = new Intent(Splash. this ,Main. class ); |
20 | Splash. this .startActivity(mainIntent); |
24 | }, SPLASH_DISPLAY_LENGHT); |
当然可以再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” |
03 | android:orientation=”vertical” |
04 | android:layout_width=”fill_parent” |
05 | android: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”/> |
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!!”/> |
20 | < WebView android:id=”@+id/browser” |
21 | android:layout_width=”fill_parent” |
22 | android:layout_height=”fill_parent” android:layout_weight=”1″/> |
有一个id为splashscreen 的linearlayout,是程序启动时显现的部分。id为browser是程序的主界面显示部分。
02 | import android.app.Activity; |
03 | import android.app.AlertDialog; |
04 | import android.content.DialogInterface; |
05 | import android.content.Intent; |
06 | import android.os.Bundle; |
07 | import android.os.Handler; |
08 | import android.os.Message; |
09 | import android.util.Log; |
10 | import android.view.LayoutInflater; |
11 | import android.view.animation.Animation; |
12 | import android.view.animation.AnimationUtils; |
13 | import android.widget.LinearLayout; |
14 | import android.widget.TextView; |
15 | import android.widget.Toast; |
16 | public class WebGameActivity extends Activity { |
18 | private WebView webView; |
20 | private Handler mHandler = new Handler(); |
22 | private static final String TAG = "WebGameActivity" ; |
24 | private static final int MENU_RELOAD = Menu.FIRST; |
25 | private static final int MENU_HELP = Menu.FIRST + 1 ; |
26 | private static final int MENU_ABOUT = Menu.FIRST + 2 ; |
27 | private static final int MENU_CLOSE = Menu.FIRST + 3 ; |
28 | private int staus = 0 ; |
30 | private static final int STOPSPLASH = 0 ; |
32 | private static final long SPLASHTIME = 1000 ; |
34 | private LinearLayout splash; |
37 | private Animation myAnimation_Alpha; |
38 | private Animation animatinoGone ; |
40 | private Handler splashHandler = new Handler() { |
41 | public void handleMessage(Message msg) { |
45 | splash.startAnimation(animatinoGone); |
46 | splash.setVisibility(View.GONE); |
49 | sendEmptyMessageDelayed(STOPSPLASH, SPLASHTIME); |
51 | super .handleMessage(msg); |
56 | protected 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); |
63 | splash = (LinearLayout) findViewById(R.id.splashscreen); |
64 | tv = (TextView) findViewById(R.id.info); |
65 | tv.setText( "正在建立数据连接" ); |
66 | splash.startAnimation(myAnimation_Alpha); |
68 | Message msg = new Message(); |
69 | msg.what = STOPSPLASH; |
70 | splashHandler.sendMessageDelayed(msg, SPLASHTIME); |