Android程序启动画面之Splash总结

来源:互联网 发布:qq等级加速器软件 编辑:程序博客网 时间:2024/05/28 23:12
方法一:


很多应用都会有一个启动界面。欢迎画面慢慢隐现,然后慢慢消隐。实现这种效果的方法有两种(暂时只发现两种)
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);   }






本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2011-09/42417.htm
原创粉丝点击