安卓开发之如何设置添加启动画面

1492572070838689.jpg

今天春哥给大家讲讲安卓开发教程之如何设置添加启动画面,本文实例讲述了Android简单启动画面实现方法,分享给大家供大家参考,具体如下:

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

1. splash.xml布局文件

1
2
3
4
5
6
7
8
9
10
11
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".SplashActivity" >
  <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/welcome_android"
    android:scaleType="fitCenter" />
</RelativeLayout>

2. SplashActivity类,使用Handler的postDelayed方法,3秒后执行跳转到主视图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package cn.eoe.leigo.splash;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;



public class SplashActivity extends Activity {
  //延迟3秒
  private static final long SPLASH_DELAY_MILLIS = 3000;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    // 使用Handler的postDelayed方法,3秒后执行跳转到MainActivity
    new Handler().postDelayed(new Runnable() {
      public void run() {
        goHome();
      }
    }, SPLASH_DELAY_MILLIS);
  }
  private void goHome() {
    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
    SplashActivity.this.startActivity(intent);
    SplashActivity.this.finish();
  }
}

3. 配置AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="cn.eoe.leigo.splash"
  android:versionCode="1"
  android:versionName="1.0" >
  <uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="10" />
  <application
    android:icon="@drawable/logo"
    android:label="@string/app_name" >
    <activity
      android:name=".SplashActivity"
      android:configChanges="keyboardHidden"
      android:label="@string/app_name"
      android:launchMode="singleTask"
      android:screenOrientation="portrait"
      android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name=".MainActivity" />
  </application>




来源:春哥技术博客,欢迎分享,转载请注明出处。(欢迎加春哥团队客服微信号:taike668)

本文地址:https://www.cgtblog.com/jishu/1084.html
上一篇:安卓开发中如何将标题栏隐藏      下一篇:春哥教你如何将html5网站转换(打包)成ap