从开发的流程来看,引导页面应该是开发者完成相关的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>
复制代码
时间: 2024-11-04 17:22:40