$Android启动界面(Splash)的两种实现方法
(一)用2个Activity实现
用Handler对象的postDelayed方法来实现延迟跳转的目的。
补充:Handler的常用方法:
// 立即执行Runnable对象
public final boolean post(Runnable r);
// 在指定的时间(uptimeMillis)执行Runnable对象
public final boolean postAtTime(Runnable r, long uptimeMillis);
// 在指定的时间间隔(delayMillis)执行Runnable对象
public final boolean postDelayed(Runnable r, long delayMillis);
1、activity_splash.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" /> </LinearLayout>
2、activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这里是主界面" /> </LinearLayout>
3、SplashActivity:
package com.example.splashtest; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window; public class SplashActivity extends Activity { private final int SPLASH_DISPLAY_LENGHT = 3000;
private Handler handler; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash); handler = new Handler();
// 延迟SPLASH_DISPLAY_LENGHT时间然后跳转到MainActivity
handler.postDelayed(new Runnable() { @Override
public void run() {
Intent intent = new Intent(SplashActivity.this,
MainActivity.class);
startActivity(intent);
SplashActivity.this.finish();
}
}, SPLASH_DISPLAY_LENGHT); }
}
4、MainActivity:
package com.example.splashtest; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
6、修改AndroidManifest.xml文件:
...
<activity
android:name=".SplashActivity"
android:label="splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
...
7、在SplashActivity中禁用返回键:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
return true;
}
return super.onKeyDown(keyCode, event); }
(二)用一个Activity实现
主要利用控件的隐藏来实现。
1、xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:id="@+id/splash_lt"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
</LinearLayout> <TextView
android:id="@+id/main_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是主界面" /> </LinearLayout>
2、MainActivity
package com.example.splashtest2; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout; public class MainActivity extends Activity { private final int STOP_SPLASH = 0;
private final int SPLASH_TIME = 3000; private LinearLayout splashLt; private Handler splashHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case STOP_SPLASH:
splashLt.setVisibility(View.GONE);
break;
default:
break;
} super.handleMessage(msg);
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main); splashLt = (LinearLayout) findViewById(R.id.splash_lt); Message msg = new Message();
msg.what = STOP_SPLASH; // 注:这里必须用延迟发送消息的方法,否则ImageView不会显示出来
splashHandler.sendMessageDelayed(msg, SPLASH_TIME);
} }
(三)小结
建议使用第一种方法,用两个Activity实现,因为MainActivity中的代码不宜过多。
随机推荐
- 通过主机名(域名)获取IP地址,主机别名等信息
一.所用API函数介绍 struct hostent FAR*gethostbyname( const char FAR* name ); 传入參数:const char FAR* name.主机名或 ...
- linux无密登录
ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub bigdata@cor2
- 开源播放器ijkplayer源码结构
ijkplayer核心源码主要在ijkmedia文件夹下ijkplayer.ijksdl及ijkutils. 注:tag k0.3.1 player: remove ijkutil android相关 ...
- Cut the rope
http://acm.nyist.net/JudgeOnline/problem.php?pid=651 描述We have a rope whose length is L. We will cut ...
- Ubuntu安装特定版本安装包
Ubuntu安装特定版本安装包可以用aptitude,aptitude是apt-get的高级版,使用起来更强大. aptitude install package=version 比如我要安装2.6. ...
- Core Services层
本文转载至 http://jingyan.baidu.com/article/cdddd41c57360853cb00e124.html Core Services层是系统很多部分的基础部分,也许应用 ...
- 使用yum 出现 Loaded plugins: fastestmirror
使用yum 安装是出现 : Loaded plugins: fastestmirror [root@localhost yum.repos.d]# yum –y install httpd http ...
- std::vector<std::vector<> >
上次看到这个有点晕了,其实这个vector保存的是std::vector<> #include <vector> #include <iostream> using ...
- 从网上搜索到的一些关于pcap源代码,入门级的
/*pcap_1.c*/ #include <stdio.h>#include <stdlib.h>#include <pcap.h> /* 如果没有pcap的系 ...
- 《从零开始学Swift》学习笔记(Day 35)——会使用下标吗?
原创文章,欢迎转载.转载请注明:关东升的博客 看下面的示例代码是不是使用过: var studentList: String[] = ["张三","李四",&q ...