现在很多的应用中都有这样一个效果,打开应用时,先展示应用的LOGO和公司的图标,然后在加载主界面。这样一个效果给用户的体验很好,在android这样的效果很多,自己也顺便记录一下。
创建一个spliashActivity来作为图片的运行的载体,代码结构如下:
private static int SEND = 1;
private static final int LOAD_DISPLAY_TIME = 1500;
private Handler hanlder = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
hanlder.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashActivity.this,
MainActivity.class));
finish();
}
}, LOAD_DISPLAY_TIME);
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
setContentView(R.layout.load);
Message message = new Message();
message.what = SEND;
hanlder.sendMessage(message);
}
其中在清单文件中添加了一下主题下效果,配置文件如下:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SplashActivity"
android:configChanges="orientation|keyboardHidden"
android:icon="@drawable/ic_launcher"
android:theme="@style/MyTheme.NoTitleBar.CustomBackground" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
在strings.xml中把主题集合成了一个,配置如下:
<style name="MyTheme.NoTitleBar.CustomBackground" parent="@android:Theme.Black">
<item name="android:windowBackground">@drawable/logo2</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
加载图片的load.xml文件,如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/load"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/logo2"
android:gravity="center|center"
android:orientation="vertical" >
</LinearLayout>