首页 > 其他分享 >Android 如何 简单的添加 启动页 SplashScreen

Android 如何 简单的添加 启动页 SplashScreen

时间:2023-01-05 17:58:40浏览次数:56  
标签:xml isReady App 添加 跳转 Android SplashScreen

1. 在 Android App 启动中, 为了体验优化,各大App 都是有 添加 启动页的, 比较土的 方法就是直接 弄个 loadingActivity, 充当启动页, 在启动初始化相关工作做完以后,再跳转. 就像下面这样.

themes.xml 代码

    <style name="Fullscreen" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowBackground">@drawable/lakjdlfak</item>
    </style>

AndroidManifest.xml 代码

    <activity
      android:name=".xxxxx"
      android:theme="@style/Fullscreen"
      android:screenOrientation="landscape"
      android:exported="true">
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

xxxxActivity.java 中 跳转

    Intent intent=new Intent(xxxxx.this, xxxxx.class);
    startActivity(intent);

2. 现在有 官方支持的方法 是通过 Android 12 加入的 SplashScreen.

这样体验会更好, 避免 Acitivity 跳转; 但是 Android 12 系统版本太新了,并不是主流,之前的版本怎么办呢? 现在有了:

AndroidX包也刚推出了一个叫SplashScreen的同名API。我们都知道AndroidX归属Jetpack,是独立于SDK版本以外的开发框架集合,很显然它就是用来兼容低版本的Splash Screen功能库。
Jetpack SplashScreen是向后兼容Android 12 Splash Screen功能的开发库。 其最早支持到Android 6(API 23),全球的Android设备中6及以上的版本占用率达到了9成以上,可以说完全够用了。

2.1 App的 build.gradle 中 需要引入 splashscreen 库支持
dependencies {
    ...
    implementation 'androidx.core:core-splashscreen:1.0.0-beta01'
}

themes.xml 代码, 自定义 SplashScreen 主题, 一定要继承自 Theme.SplashScreen. 而 postSplashScreenTheme 是 指定目标Activity主题. 一定要指定, 根据你的 Activity 的父类确定,如果你的Activity 继承自 AppCompatActivity, 那么你的 主题也得是AppCompat系主题. 我这里 的 Activity 继承自 ComponentActivity, 所以可以是 非 AppCompat系主题.

    <style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_foreground</item>
        <item name="windowSplashScreenBackground">@color/teal_700</item>
        <item name="postSplashScreenTheme">@style/Theme.MyApplicationTestSplash</item>
    </style>

同样, AndroidManifest.xml 代码

    <activity
      android:name=".xxxxx"
      android:theme="@style/Theme.App.Starting"
      android:screenOrientation="landscape"
      android:exported="true">
      <intent-filter>
          <action android:name="android.intent.action.MAIN" />
          <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

xxxxActivity.java 中

public class MainActivity extends ComponentActivity {

    private static boolean isReady = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SplashScreen splash = SplashScreen.installSplashScreen(this);
        setContentView(R.layout.activity_main);

        splash.setKeepOnScreenCondition(() ->{
            return isReady;
        } );

//        View contentView = findViewById(android.R.id.content);
//        contentView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener(){
//            @Override
//            public boolean onPreDraw() {
//                //Return true to proceed with the current drawing pass, or false to cancel.
//                System.out.println("onPreDraw been called! ");
//                return isReady;
//            }
//        });

这里, 可以通过 isReady 这个值 来控制是否继续显示 SplashScreen. 也就是是否跳转.
有两种方式 一种是 通过 ViewTreeObserver 的 OnPreDrawListener, 另一种是 SplashScreen 官方的 setKeepOnScreenCondition, 都能达到效果.

参考:

https://juejin.cn/post/6997217571208445965 Jetpack新成员SplashScreen:打造全新的App启动画面
https://www.jianshu.com/p/4f68d62c809b Android ViewTreeObserver使用总结
https://blog.csdn.net/guolin_blog/article/details/120275319 Android 12 SplashScreen API快速入门
https://github.com/ellisonchan/ComposeBird Demo 地址

标签:xml,isReady,App,添加,跳转,Android,SplashScreen
From: https://www.cnblogs.com/lesten/p/17028434.html

相关文章

  • TapTap实名认证-Android
    我这边使用的是Android版。导入防沉迷SDK,从官网下载。implementation(name:"AntiAddiction_3.16.5",ext:"aar")//防沉迷SDKimplementation(name:"Ant......
  • android 程序开发的插件化
    框架已经放出:​​android-application-plug-ins-frame-work​​​​安卓应用程序插件化开发框架-AAPFramework​​ 在android的项目开发中,都会遇到后期......
  • Android中RelativeLayout及TableLayout使用说明
    RelativeLayout用到的一些重要的属性:第一类:属性值为true或falseandroid:layout_centerHrizontal水平居中android:layout_centerVertical垂直居中android:layout_centerIn......
  • phoneGap-Android开发环境搭建
    phoneGap-Android开发环境搭建一.安装在安装PhoneGap开发环境之前,需要按顺序安装以下工具:1.​​JavaSDK​​ javasdk,不安装的......
  • Android四大基本组件介绍与生命周期
    ​​Android四大基本组件介绍与生命周期​​Android四大基本组件分别是Activity,Service服务,ContentProvider内容提供者,BroadcastReceiver广播接收器。一:了解四大......
  • cocos2d-x基于windows平台交叉编辑android工程
    cocos2d-x确实是一款优秀的引擎,尽管和正规军的unity3d比起来它显得有点土,但它在移动平台上的性能表现着实惊艳。以下讲解如何将win32工程交叉编译到android平台。 一、环境......
  • android常用布局基础学习
     总结:可水平放置可垂直放置也可穿插使用,默认为水平  <!--我在第一次使用权重的时候忽视了本线性布局中的宽度与高度,如果要使用权重,请将线性布局的最初大小设置为ma......
  • Adobe AIR for Android 缓存本地数据常用方法
    Local SharedObject这种方法比较简单方便的保存少的数据到到设备中。你不用自己去管理这些数据,设备会自动管理他。SharedObject 在 flash.net 包中,继承自EventDispatche......
  • android平台解释器+JIT+AOT代码执行学习
    dalvikJIT(Just-In-Time)JIT即时编译,即在代码运行时进行编译。对于dalvik虚拟机而言其检测到执行频率较高的函数时就会进行jit编译将其编译为本地机器码,这样下次此函数执行......
  • BBS项目(四):临时评论渲染 文章子评论功能 后台管理页面搭建 添加文章页面搭建
    目录临时评论样式渲染文章子评论业务逻辑后台管理页面搭建后台管理页面模板创建添加文章页面搭建富文本编辑器添加文章初步实现添加文章功能优化beautifulsoup模块基本使用......