页面效果:
没点击跳过自然进入主页,点击跳过之后立即进入主页HelloWorld
实现步骤:
1、欢迎页布局activity_sp.xml放一张背景图(图片随你便啦)再放一个盛放倒计时的TextView。
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:app="http://schemas.android.com/apk/res-auto"
4 xmlns:tools="http://schemas.android.com/tools"
5 android:layout_width="match_parent"
6 android:layout_height="match_parent"
7 android:background="@drawable/a">
8
9 <RelativeLayout 10 android:layout_width="match_parent"
11 android:layout_height="wrap_content">
12
13 <TextView 14 android:id="@+id/tv"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:layout_alignParentRight="true"
18 android:layout_marginRight="20dp"
19 android:layout_marginTop="10dp"
20 android:textSize="20sp" />
21 </RelativeLayout>
2、新建一个Activity用来关联我们的layout布局文件
1 package com.cwj.test; 2
3 import android.content.Intent; 4 import android.os.Handler; 5 import android.support.v7.app.AppCompatActivity; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.WindowManager; 9 import android.widget.TextView; 10
11 import java.util.Timer; 12 import java.util.TimerTask; 13
14 public class SpActivity extends AppCompatActivity implements View.OnClickListener { 15
16 private int recLen = 5;//跳过倒计时提示5秒
17 private TextView tv; 18 Timer timer = new Timer(); //定义一个计时器 19 private Handler handler; 20 private Runnable runnable; 21
22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 //定义全屏参数
26 int flag= WindowManager.LayoutParams.FLAG_FULLSCREEN; 27 //设置当前窗体为全屏显示
28 getWindow().setFlags(flag, flag); 29 setContentView(R.layout.activity_sp); 30 initView(); 31 timer.schedule(task, 1000, 1000);//等待时间一秒,停顿时间一秒
32 /**
33 * 正常情况下不点击跳过 34 */
35 handler = new Handler(); 36 handler.postDelayed(runnable = new Runnable() { 37 @Override 38 public void run() { 39 //从闪屏界面跳转到首界面
40 Intent intent = new Intent(SpActivity.this, MainActivity.class); 41 startActivity(intent); 42 finish(); 43 } 44 }, 5000);//延迟5S后发送handler信息
45
46 } 47
48 private void initView() { 49 tv = findViewById(R.id.tv);//跳过
50 tv.setOnClickListener(this);//跳过监听
51 } 52
53 TimerTask task = new TimerTask() { 54 @Override 55 public void run() { 56 runOnUiThread(new Runnable() { // UI thread
57 @Override 58 public void run() { 59 recLen--; 60 tv.setText("跳过 " + recLen); 61 if (recLen < 0) { 62 timer.cancel(); 63 tv.setVisibility(View.GONE);//倒计时到0隐藏字体
64 } 65 } 66 }); 67 } 68 }; 69
70 /**
71 * 点击跳过 72 */
73 @Override 74 public void onClick(View view) { 75 switch (view.getId()) { 76 case R.id.tv: 77 //从闪屏界面跳转到首界面
78 Intent intent = new Intent(SpActivity.this, MainActivity.class); 79 startActivity(intent); 80 finish(); 81 if (runnable != null) { 82 handler.removeCallbacks(runnable); 83 } 84 break; 85 default: 86 break; 87 } 88 }
3、我们还可以对样式进行少许的修改,我们平常见到的欢迎界面是全屏的,所以我们在这里将标题栏隐藏掉
最简单的方法:
在AndroidManifest.xml文件中添加
1 android:theme="@android:style/Theme.NoTitleBar"
以下是系统自带的一些主题样式,我们也可以对其进行部分修改
android:theme="@android:style/Theme.Dialog" 将一个Activity显示为对话框模式 android:theme="@android:style/Theme.NoTitleBar" 不显示应用程序标题栏 android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 不显示应用程序标题栏,并全屏 android:theme="Theme.Light" 背景为白色 android:theme="Theme.Light.NoTitleBar" 白色背景并无标题栏 android:theme="Theme.Light.NoTitleBar.Fullscreen" 白色背景,无标题栏,全屏 android:theme="Theme.Black" 背景黑色 android:theme="Theme.Black.NoTitleBar" 黑色背景并无标题栏 android:theme="Theme.Black.NoTitleBar.Fullscreen" 黑色背景,无标题栏,全屏 android:theme="Theme.Wallpaper" 用系统桌面为应用程序背景 android:theme="Theme.Wallpaper.NoTitleBar" 用系统桌面为应用程序背景,且无标题栏 android:theme="Theme.Wallpaper.NoTitleBar.Fullscreen" 用系统桌面为应用程序背景,无标题栏,全屏 android:theme="Translucent" 透明背景 android:theme="Theme.Translucent.NoTitleBar" 透明背景并无标题 android:theme="Theme.Translucent.NoTitleBar.Fullscreen" 透明背景并无标题,全屏 android:theme="Theme.Panel" 面板风格显示 android:theme="Theme.Light.Panel" 平板风格显示
参考博客:
https://blog.csdn.net/juer2017/article/details/79069970