安卓开发-应用启动“开屏页设置”

关于安卓开发--开屏页

安卓开发过程中,增强用户体验必须添加开屏页面,那么如何添加开平页面呢?

参考如下:

这里实现简单功能:开始显示一张图片,2秒后跳转到主界面。

1.布局文件:activity_splash.xml

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/splash_root"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="@drawable/login_app"  
  7.     android:orientation="vertical" >  
  8.      
  9.    <ImageView  
  10.     android:layout_width="match_parent"  
  11.     android:layout_height="match_parent"   
  12.     android:scaleType="fitXY"  
  13.     android:src="@drawable/login_app"  
  14.      />  
  15.      
  16. </LinearLayout>  

2.开屏界面实现:SplashActivity.java
[java]  view plain  copy
  1. import android.app.Activity;  
  2. import android.content.Intent;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5.   
  6. /** 
  7.  * 开屏页 
  8.  * 
  9.  */  
  10. public class SplashActivity extends Activity {  
  11.   
  12.     private static final int sleepTime = 2000;  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle arg0) {  
  16.         final View view = View.inflate(this, R.layout.activity_splash, null);  
  17.         setContentView(view);  
  18.         super.onCreate(arg0);  
  19.     }  
  20.   
  21.     @Override  
  22.     protected void onStart() {  
  23.         super.onStart();  
  24.         new Thread(new Runnable() {  
  25.             public void run() {       
  26.                 long start = System.currentTimeMillis();  
  27.                 long costTime = System.currentTimeMillis() - start;  
  28.                     //等待sleeptime时长  
  29.                     if (sleepTime - costTime > 0) {  
  30.                         try {  
  31.                             Thread.sleep(sleepTime - costTime);  
  32.                         } catch (InterruptedException e) {  
  33.                             e.printStackTrace();  
  34.                         }  
  35.                     }  
  36.                     //进入主页面  
  37.                     startActivity(new Intent(SplashActivity.this, MainActivity.class));  
  38.                     finish();  
  39.             }  
  40.         }).start();  
  41.     }  
  42. }  

3.新建MainActivity.java(暂时未实现相关功能)
[java]  view plain  copy
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3.   
  4. public class MainActivity extends Activity {  
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.     }  
  11.   
  12. }  

4.MainActivity的布局文件(暂时未实现相关功能):activity_main.xml
[java]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context="com.th.myphone.MainActivity" >  
  10.   
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="@string/hello_world" />  
  15.   
  16. </RelativeLayout>  

5.AndroidManifest.xml
[java]  view plain  copy
  1. <activity  
  2.          android:name=".SplashActivity"  
  3.          android:label="@string/app_name" >  
  4.          <intent-filter>  
  5.              <action android:name="android.intent.action.MAIN" />  
  6.   
  7.              <category android:name="android.intent.category.LAUNCHER" />  
  8.          </intent-filter>  
  9.      </activity>  
  10.        
  11.       <activity  
  12.          android:name=".MainActivity"/>  
欢迎到我的下载页下载(完整代码),便于交流学习,代码下载传送门: 点击打开下载项目源码
发布了149 篇原创文章 · 获赞 132 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/Sailor_luo/article/details/80283586