现在的位置: 首页 > 综合 > 正文

创建Android应用启动界面

2014年09月05日 ⁄ 综合 ⁄ 共 3570字 ⁄ 字号 评论关闭

每个Android应用启动之后都会出现一个Splash启动界面,显示产品的LOGO、公司的LOGO或者开发者信息。如果应用程序启动时间比较长,那么启动界面就是一个很好的东西,可以让用户耐心等待这段枯燥的时间。

  • 制作Splash界面

    突出产品LOGO,产品名称,产品主要特色;

    注明产品的版本信息;

    注明公司信息或者开发者信息;

    背景图片,亦可以用背景颜色代替;
  • 除了等待还能做点什么

    大多数的Splash界面都是会等待一定时间,然后切换到下一个界面;

    其实,在这段时间里,可以对系统状况进行检测,比如网络是否通,电源是否充足;

    或者,预先加载相关数据;

    为了能让启动界面展现时间固定,需要计算执行以上预处理任务所花费的时间,那么:启动界面SLEEP的时间=固定时间-预处理任务时间
  • 源码示例(以Wordpress的Android客户端为例)

    AndroidMenifest.xml

    1 <activity android:icon="@drawable/app_icon"
    2                   android:screenOrientation="portrait"
    3                   android:name=".splashScreen"
    4                   android:theme="@android:style/Theme.NoTitleBar">
    5             <intent-filter>
    6                 <action android:name="android.intent.action.MAIN"/>
    7                 <category android:name="android.intent.category.LAUNCHER"/>
    8             </intent-filter>
    9         </activity>

    splashScreen.java

    01 package org.wordpress.android;
    02 import android.app.Activity;
    03 import android.content.Intent;
    04 import android.content.pm.PackageInfo;
    05 import android.content.pm.PackageManager;
    06 import android.content.pm.PackageManager.NameNotFoundException;
    07 import android.graphics.PixelFormat;
    08 import android.os.Bundle;
    09 import android.os.Handler;
    10 import android.view.WindowManager;
    11 import android.widget.TextView;
    12 public class splashScreen extends Activity
    {
    13 /**
    14 *
    Called when the activity is first created.
    15 */
    16 @Override
    17 public void onCreate(Bundle
    icicle) {
    18 super.onCreate(icicle);
    19 getWindow().setFormat(PixelFormat.RGBA_8888);
    20 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
    21 setContentView(R.layout.splashscreen);
    22 //Display
    the current version number
    23 PackageManager
    pm = getPackageManager();
    24 try {
    25 PackageInfo
    pi = pm.getPackageInfo(
    "org.wordpress.android"0);
    26 TextView
    versionNumber = (TextView) findViewById(R.id.versionNumber);
    27 versionNumber.setText("Version
    "
     +
    pi.versionName);
    28 catch (NameNotFoundException
    e) {
    29 e.printStackTrace();
    30 }
    31 new Handler().postDelayed(new Runnable()
    {
    32 public void run()
    {
    33 /*
    Create an Intent that will start the Main WordPress Activity. */
    34 Intent
    mainIntent = 
    new Intent(splashScreen.this,
    wpAndroid.
    class);
    35 splashScreen.this.startActivity(mainIntent);
    36 splashScreen.this.finish();
    37 }
    38 }, 2900); //2900
    for release
    39 }
    40 }

    splashscreen.xml

    01 <!--
    02 android:gravity是对元素本身说的,元素本身的文本显示在什么地方靠着换个属性设置,不过不设置默认是在左侧的。
    03 android:layout_gravity是相对与它的父元素说的,说明元素显示在父元素的什么位置
    04 -->
    05 <LinearLayout android:id="@+id/LinearLayout01"
    06 android:layout_width="fill_parent"
    07 android:layout_height="fill_parent"
    08 xmlns:android="http://schemas.android.com/apk/res/android"
    09 android:gravity="center|center"
    10 android:background="@drawable/home_gradient"
    11 android:orientation="vertical">
    12 <!--
    13 android:scaleType是控制图片如何resized/moved来匹对ImageView的size
    14 CENTER_INSIDE
    / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽
    15 -->
    16 <ImageView android:layout_marginTop="-60dip"
    17 android:paddingLeft="20dip"
    18 android:paddingRight="20dip"
    19 android:scaleType="centerInside"
    20 android:layout_width="wrap_content"
    21 android:layout_height="wrap_content"
    22 android:id="@+id/wordpress_logo"
    23 android:src="@drawable/wordpress_home">
    24 </ImageView>
    25 <!--
    26 android:typeface
    字体风格
    27 -->
    28 <TextView android:text="@+id/TextView01"
    29 android:layout_width="wrap_content"
    30 android:layout_height="wrap_content"
    31 android:layout_marginTop="20dip"
    32 android:typeface="serif"
    33 android:shadowDx="0"
    34 android:shadowDy="2"
    35 android:shadowRadius="1"
    36 android:shadowColor="#FFFFFF"
    37 android:textColor="#444444"
    38 android:textSize="20dip"
    39 android:id="@+id/versionNumber"
    40 android:gravity="bottom">
    41 </TextView>
    42 </LinearLayout>

    转载地址:  http://www.cnblogs.com/xiongbo/archive/2011/05/17/splash.html

抱歉!评论已关闭.