方法一:
很多應用都會有一個啟動介面。歡迎畫面慢慢隱現,然後慢慢消隱。實現這種效果的方法有兩種(暫時只發現兩種)
1、使用兩個Activity,程式啟動時候load第一張Activity,然後由tick觸發N秒鐘後startActivity另外一張Activity。
2、使用一個Activity,可以用到View.gone() 這個方法。把Acitivity的某些元素移除。
1、兩個Activity:
首先是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sunshine.splash" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" ;android:label="@string/app_name"> ; <activity android:name=".Splash" android:label="@string/app_name"> ; <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="Main"> </activity> </application> <uses-sdk android:minSdkVersion="3" /> </manifest>
然後是JAVA代碼:
package net.hlovey.splash; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; public class Splash extends Activity { private final int SPLASH_DISPLAY_LENGHT = 3000; //延遲三秒 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); new Handler().postDelayed(new Runnable(){ @Override public void run() { Intent mainIntent = new Intent(Splash.this,Main.class); Splash.this.startActivity(mainIntent); Splash.this.finish(); } }, SPLASH_DISPLAY_LENGHT); } }
當然可以再Splash中加入動畫效果。(我覺得先要布局好AndroidManifest.xml。因為那才是工程的索引檔案。首先在那要有一個統籌!而不是先寫java code。然後逐步往xml中增加 ,這說明對整個項目沒有一個統籌的設計)
方法二:
androidmanifest.xml就不多說了。先看布局代碼:
<?xml version=”1.0″ encoding=”utf-8″?><LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”android:orientation=”vertical”android:layout_width=”fill_parent”android:layout_height=”fill_parent”> <LinearLayout android:id=”@+id/splashscreen” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent”> <ImageView android: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> <WebView android:id=”@+id/browser”android:layout_width=”fill_parent”android:layout_height=”fill_parent” android:layout_weight=”1″/></LinearLayout>
有一個id為splashscreen 的linearlayout,是程式啟動時顯現的部分。id為browser是程式的主介面顯示部分。
package net.hlovey.s;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class WebGameActivity extends Activity {
private WebView webView;
private Handler mHandler = new Handler();
private static final String TAG = "WebGameActivity";
//菜單
private static final int MENU_RELOAD = Menu.FIRST;
private static final int MENU_HELP = Menu.FIRST + 1;
private static final int MENU_ABOUT = Menu.FIRST + 2;
private static final int MENU_CLOSE = Menu.FIRST + 3;
private int staus = 0;
private static final int STOPSPLASH = 0;
//time in milliseconds
private static final long SPLASHTIME = 1000;
private LinearLayout splash;
private TextView tv;
private Animation myAnimation_Alpha;
private Animation animatinoGone ;
private Handler splashHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case STOPSPLASH:
if( staus == 1 ){
splash.startAnimation(animatinoGone);
splash.setVisibility(View.GONE);
break;
}
sendEmptyMessageDelayed(STOPSPLASH, SPLASHTIME);
}
super.handleMessage(msg);
}
};
@Override
protected void 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 = new Message();
msg.what = STOPSPLASH;
splashHandler.sendMessageDelayed(msg, SPLASHTIME);
}