Android低版本使用Android 12启动页面

Android低版本使用Android 12启动页面,第1张

1.在 build.gradle 文件中,更改 compileSdkVersion(需要大于等于31),并在依赖项中添加 SplashScreen compat 库。

2. 创建一个以 Theme.SplashScreen为 父级的主题,并将 postSplashScreenTheme 的值设为 Activity 的主题,同时将 windowSplashScreenAnimatedIcon 设为静态或动态图片。其他属性可视需要进行设置。

3.AndroidManifest.xml中,将启动的activity 的主题替换为上一步创建的主题。

4.在SplashActivity中,先调用installSplashScreen,然后再调用 setContentView。

1、首先,我们创建一个布局splash.xml;

[html] view plain copy print?

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<ImageView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:scaleType="fitXY"

android:src="@drawable/splash">

</ImageView>

</LinearLayout>

2、再者,创建一个新类SplashActivity.java;

[java] view plain copy print?

public class SplashActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState)

requestWindowFeature(Window.FEATURE_NO_TITLE)

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)

setContentView(R.layout.splash)

Handler hd = new Handler()

hd.postDelayed(new splashhandler(), 800)

}

class splashhandler implements Runnable {

public void run() {

startActivity(new Intent(getApplication(), MainActivity.class))

SplashActivity.this.finish()

}

}

}

3、在manifest中配置我们的启动入口;

[html] view plain copy print?

<activity android:name="x.x.x.SplashActivity"

android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"

android:label="@string/app_name"

android:launchMode="singleTask"

android:screenOrientation="landscape" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

<category android:name="android.intent.category.LEANBACK_LAUNCHER" />

</intent-filter>

</activity>

最后不要忘了要首屏的图片资源drawable/splash.png;

综上,我们APP的启动页已经做好了。雷达下载更专业。

Android 12开始应用冷启动和温启动时,系统会默认先加载SplashScreen(如下布局)

,然后在应用Activity渲染第一帧得时候移除;

SplashScreenView继承于FrameLayout,布局中包含一个居中的应用图标和一个置底的品牌图标。对应的配置,需要用到SDK 31新增的window属性:

上面提到的UI遮罩是Google为了解决各厂商对应用图标圆角不同,而导致图标不美观或者需要多套图标的问题,从Android 8.0开始,将应用图标拆分成背景,图标,遮罩3部分组合;

由于我们经常会在启动该页进行一些初始化或接口请求,所以需要在此页面停留超过1000ms,此时可以通过暂停activity渲染来达到延长显示。

如果应用图标动画时长1000ms不够用,可以添加动画结束监听,在默认动画结束后获取SplashScreenView进行 *** 作;需要注意getSplashScreen是SDK 31后在Activity中新增的方法。

目前存在的问题:

2022-02-17


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11456876.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存