App启动界面效果设计

来源:互联网 发布:西岐网络我有上将 编辑:程序博客网 时间:2024/05/18 02:22

转载请标明出处:http://blog.csdn.net/u012637501/article/details/45746617

     每个Android应用启动之后都会出现一个Splash启动界面,大多数的Splash界面都是会等待一定时间,然后切换到下一个界面。但如果app启动时间过长,可使用启动界面让用户耐心等待这段枯燥的时间。Splash界面一般用于显示产品的LOGO、产品名称、版本信息等,也可以完成对系统状况的检测,如网络是否连通、电源是否充足、检测新版本等,也可以预先加载相关数据。启动界面SLEEP的时间=固定时间-预处理任务时间。

一、为APP创建一个简单的启动界面

    所谓简单的启动界面,即界面只用于显示产品的LOGO、产品名称等常规信息,不做系统状态检测或数据加载的操作。设计方法如下:实现两activity,一个是SplashActivity,用来做启动画面,另一个是将要跳转的Activity。通过创建一个新的线程,延迟指定的时间再执行Activity的跳转,并调用finish方法结束当前启动activity。
实例:高仿QQ启动界面
1.src/.../WelcomeActivity.java
  1. package com.example.qq2012;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.view.Window;  
  6. /*欢迎动画*/  
  7. public class WelcomeActivity extends Activity {  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.  //       requestWindowFeature(Window.FEATURE_NO_TITLE);    //设置显示窗口界面特征,此时为窗口无标题  
  10.      super.onCreate(savedInstanceState);         
  11.         setContentView(R.layout.welcome);  
  12.         final Intent intent = new Intent(WelcomeActivity.this,LoginActivity.class);  //设置一个用于启动新Activity的"意图"  
  13.         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  //使系统为需要启动的Activity寻找与当前Activity不同的任务栈  
  14.         new Thread(new Runnable(){  //创建一个新的线程来显示欢迎动画,指定时间后结束,跳转至指定界面  
  15.    public void run() {  
  16.      try {  
  17.       Thread.sleep(3000);   //欢迎界面启动持续时间  
  18.       getApplicationContext().startActivity(intent);    //启动新的界面,获取应用的上下文,生命周期是整个应用,应用结束才会结束  
  19.       finish();  //结束欢迎界面activity  
  20.      } catch (InterruptedException e) {  
  21.       e.printStackTrace();  
  22.      }    
  23.    }          
  24.         }).start();  
  25.     }  
  26. }  
2.res/layout/welcome.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  
  5.     android:background="@drawable/splash">  
  6. </LinearLayout>  
分析说明
(1)隐藏应用标题方法
    a.在显示的布局文件中,添加 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    b.在逻辑代码setContentView(R.layout.login)之前添加requestWindowFeature(Window.FEATURE_NO_TITLE); 
(2)调用 Activity.finish()方法时,结果和用户按下 BACK 键一样:告诉 Activity Manager 该 Activity 实例完成了相应的工作,可以被“回收”。SplashActivity 从 Active 状态转换 Stoped 状态,并被系统从栈中移除,标志可以被“回收”。
(3)创建一个新的线程完成界面跳转,两种方法:
方法一:
new Thread(new Runnable(){  
       public void run() {  
                 Thread.sleep(3000);  //延时时间
                    .....
                 finish();
            
}
方法二:
new Handler().postDelayed(new Runnable() {  
            public void run() {  
                ........
                SplashActivity.this.finish();  
            }      
        }, 3000); 
说明:
Handler().postDelayed  是延迟指定的时间再执行
Handler类主要可以使用如下3个方法来设置执行Runnable对象的时间:
>立即执行Runnable对象    
    public final boolean post(Runnable r);    
>在指定的时间(uptimeMillis)执行Runnable对象    
    public final boolean postAtTime(Runnable r, long uptimeMillis);    
>在指定的时间间隔(delayMillis)执行Runnable对象    
    public final boolean postDelayed(Runnable r, long delayMillis); 
运行结果

二、自定义复杂启动界面设计
    在启动界面友好展示的同时,后台可以做很多操作,比如系统检测,预加载数据等,这些后台操作可以使用AsyncTask来实现。

1.Splash启动界面设计,res/layout/welcome.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  
  5.     android:background="#FFFFFF">  
  6.  <!--第一级 -->  
  7.  <TextView  
  8.      android:id="@+id/copy_right"  
  9.      android:layout_height="wrap_content"  
  10.      android:layout_width="wrap_content"  
  11.      android:layout_centerHorizontal="true"  
  12.      android:layout_alignParentBottom="true"  
  13.      android:padding="12dp"  
  14.      android:text="By 老蒋良心出品"/>  
  15.  <!-- 第一级 -->  
  16.  <RelativeLayout  
  17.      android:layout_width="fill_parent"  
  18.      android:layout_height="fill_parent">  
  19.         <!-- 第二级 -->  
  20.        <LinearLayout  
  21.           android:layout_width="wrap_content"  
  22.           android:layout_height="wrap_content"  
  23.           android:orientation="vertical"  
  24.        android:layout_centerInParent="true">  
  25.             <!-- logo图片 -->  
  26.             <ImageView  
  27.                 android:id="@+id/logoImage"  
  28.                 android:layout_width="wrap_content"  
  29.                 android:layout_height="wrap_content"  
  30.                 android:src="@drawable/logo"  
  31.                 android:layout_gravity="center_horizontal"  
  32.                 android:background="#ffffff"/>  
  33.             <!-- 产品名称、版本号 -->  
  34.             <LinearLayout  
  35.                android:layout_width="wrap_content"  
  36.                android:layout_height="wrap_content"  
  37.                 android:layout_marginTop="20dp"  
  38.                android:orientation="horizontal">  
  39.                 <TextView  
  40.                     android:layout_width="wrap_content"  
  41.                     android:layout_height="wrap_content"  
  42.                     android:padding="6dip"  
  43.                     android:text="创新生活app"  
  44.                     android:textStyle="bold"                                        
  45.                     android:textSize="20sp"/>  
  46.                    <TextView  
  47.                     android:layout_width="wrap_content"  
  48.                     android:layout_height="wrap_content"  
  49.                     android:padding="3dip"  
  50.                     android:text="V1.0"     
  51.                     android:textStyle="bold"                                    
  52.                     android:textSize="15sp"/>  
  53.             </LinearLayout>  
  54.             <!-- 分隔线 -->  
  55.             <View  
  56.                 android:layout_width="fill_parent"  
  57.                 android:layout_height="1dp"  
  58.                 android:layout_marginLeft="17dp"  
  59.                 android:layout_marginRight="17dp"  
  60.                 android:background="#dddddd"/>  
  61.           <!-- 内容描述 -->  
  62.           <TextView android:layout_width="wrap_content"  
  63.                 android:layout_height="wrap_content"  
  64.                 android:layout_gravity="center_horizontal"  
  65.                 android:padding="6dip"  
  66.                 android:text="科技创新,振兴中华."  
  67.                 android:textSize="13sp"/>  
  68.             <ProgressBar android:id="@+id/refresh_list_footer_progressbar"  
  69.                 android:layout_width="24dip"  
  70.                 android:layout_height="24dip"  
  71.                 style="@android:attr/progressBarStyleLargeInverse"  
  72.                 android:layout_gravity="center">  
  73.             </ProgressBar>               
  74.         </LinearLayout>  
  75.  </RelativeLayout> 
2.后台处理,SplashActivity.java
  1. public class SplashActivity extends Activity {   
  2.     @Override  
  3.     protected void onCreate(Bundle savedInstanceState) {   
  4.         super.onCreate(savedInstanceState);   
  5.         setContentView(R.layout.splash);   
  6.   
  7.         new AsyncTask<Void, Void, Integer>() {  
  8.             //后台操作 
  9.             protected Integer doInBackground(Void... params) {   
  10.                 int result;   
  11.                 long startTime = System.currentTimeMillis();   
  12.                 result = loadingCache();   
  13.                 long loadingTime = System.currentTimeMillis() - startTime;   
  14.                 if (loadingTime < SHOW_TIME_MIN) {   
  15.                     try {   
  16.                         Thread.sleep(SHOW_TIME_MIN - loadingTime);   
  17.                     } catch (InterruptedException e) {   
  18.                         e.printStackTrace();   
  19.                     }   
  20.                 }   
  21.                 return result;   
  22.             }   
  23.            //操作后台操作的结果
  24.             @Override  
  25.             protected void onPostExecute(Integer result) {   
  26.                 // ... ...   
  27.                 Intent intent = new Intent();   
  28.                 intent.setClassName(SplashActivity.this, getString(R.string.splash_out_activity));   
  29.                 startActivity(intent);   
  30.                 finish();   
  31.                 //两个参数分别表示进入的动画,退出的动画   
  32.                 overridePendingTransition(R.anim.fade_in, R.anim.fade_out);   
  33.             }.execute(new Void[]{});   
  34.     }   
  35.     private int loadingCache()   
  36.     {   
  37.     }   
  38.     。。。。。  
  39. }  



参考资料:
http://blog.csdn.net/wangjinyu501/article/details/7643396
http://www.jb51.net/article/36190.htm

3 0