從開發的流程來看,引導頁面應該是開發者完成相關的app所有的功能後,最後再編寫的。所以現在先來寫開機歡迎頁面。
1. 先設置主頁面 爲 splash 頁面。
-
<activity android:name="com.happy.ui.SplashActivity" >
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
- </activity>
2.activity_splash.xml 佈局文件
-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:tools="http://schemas.android.com/tools"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
tools:context=".SplashActivity" >
-
-
<ImageView
-
android:id="@+id/splash"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent" />
-
- </RelativeLayout>
3.
-
package com.happy.ui;
-
-
import android.app.Activity;
-
import android.content.Intent;
-
import android.os.Bundle;
-
import android.os.Handler;
-
import android.os.Message;
-
import android.view.Menu;
-
import android.widget.ImageView;
-
-
public class SplashActivity extends Activity {
-
/**
-
* 跳轉到主頁面
-
*/
-
private final int GOHOME = 0;
-
/**
-
* 跳轉到引導頁面
-
*/
-
private final int GOGUIDE = 1;
-
/**
-
* 頁面停留時間 3s
-
*/
-
private final int SLEEPTIME = 3000;
-
/**
-
* splash ImageView
-
*/
-
private ImageView splashImageView = null;
-
-
private Handler mHandler = new Handler() {
-
-
@Override
-
public void handleMessage(Message msg) {
-
switch (msg.what) {
-
case GOHOME:
-
goHome();
-
break;
-
case GOGUIDE:
-
goGuide();
-
break;
-
-
default:
-
break;
-
}
-
}
-
-
};
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_splash);
-
init();
-
loadData();
-
}
-
-
@Override
-
public boolean onCreateOptionsMenu(Menu menu) {
-
getMenuInflater().inflate(R.menu.splash, menu);
-
return true;
-
}
-
-
private void init() {
-
splashImageView = (ImageView) findViewById(R.id.splash);
-
}
-
-
private void loadData() {
-
splashImageView.setBackgroundResource(R.drawable.splash);
-
mHandler.sendEmptyMessageDelayed(GOHOME, SLEEPTIME);
-
}
-
-
/**
-
* 跳轉到引導頁面
-
*/
-
protected void goGuide() {
-
-
}
-
-
/**
-
* 跳轉到主界面
-
*/
-
protected void goHome() {
-
Intent intent = new Intent(this, MainActivity.class);
-
startActivity(intent);
-
// 添加界面切換效果,注意只有Android的2.0(SdkVersion版本號爲5)以後的版本才支持
-
int version = Integer.valueOf(android.os.Build.VERSION.SDK);
-
if (version >= 5) {
-
overridePendingTransition(R.anim.anim_in, R.anim.anim_out);
-
}
-
finish();
-
}
-
}
4.附加動畫。在 res/anim 路徑下添加 動畫文件
anim_in.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<set xmlns:android="http://schemas.android.com/apk/res/android" >
-
-
<scale
-
android:duration="2000"
-
android:fromXScale="0.7"
-
android:fromYScale="0.7"
-
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
-
android:pivotX="50%"
-
android:pivotY="50%"
-
android:toXScale="1"
-
android:toYScale="1" />
-
-
<alpha
-
android:duration="2000"
-
android:fromAlpha="0"
-
android:toAlpha="1.0" />
-
- </set>
anim_out.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<set xmlns:android="http://schemas.android.com/apk/res/android" >
-
-
<scale
-
android:duration="2000"
-
android:fromXScale="1"
-
android:fromYScale="1"
-
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
-
android:pivotX="50%"
-
android:pivotY="50%"
-
android:toXScale="0.7"
-
android:toYScale="0.7" />
-
-
<alpha
-
android:duration="2000"
-
android:fromAlpha="1"
-
android:toAlpha="0" />
-
- </set>