一、 界面效果图
当用户启动应用程序时,首先出现一闪屏界面,显示 3 秒后,跳转到主界面,这里假设
先跳转到登录界面。
二、实现过程
1、修改自动生成的 activity_welcome.xml 文件,主要代码如下:
<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"
android:background="@drawable/welcome_bg"
tools:context=".WelcomeActivity" >
</RelativeLayout>
2、修改 AndroidManifest.xml 文件,修改闪屏界面为全屏模式
<activity
android:name="cn.edu.bztc.walkersimulate.WelcomeActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:label="@string/title_activity_welcome" >
</activity>
3、修改 WelcomeActivity,实现停顿 3 秒后跳转
方式 1:利用动画持续时间,动画结束后跳转,主要代码如下:
public class WelcomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
RelativeLayout layoutWelcome=(RelativeLayout)findViewById(R.id.layoutWelcome);
AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f,1.0f);
alphaAnimation.setDuration(3000);
layoutWelcome.startAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Intent intent=new Intent(WelcomeActivity.this,GuideActivity.class);
startActivity(intent);
}
});
}
方式 2:使用 Handler 完成跳转
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent=new Intent(WelcomeActivity.this,LoginActivity.class);
startActivity(intent);
}
},3000);
方式 3:使用多线程完成跳转new Thread() {
public void run() {
try {
Thread.sleep(3000);
Intent intent = new Intent(WelcomeActivity.this,
LoginActivity.class);
startActivity(intent);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
new Thread() {
public void run() {
try {
Thread.sleep(3000);
Intent intent = new Intent(WelcomeActivity.this,
LoginActivity.class);
startActivity(intent);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();