你的Android App又需要适配了:Android 12,全新的App启动动画

再比如让机器人在Kotlin上侧滑。

或者让几何图案拼凑出字母K之后和机器人汇合,象征着Android和Kotlin的强强联合。

注意:

  • 动画Icon的时长上限为1000ms。

  • 图标的进入动画可以定制,但由系统控制,不可以被监听和额外处理。

1.6 延长启动画面

The splash screen is dismissed as soon as your app draws its first frame. If you need to load a small amount of data such as in-app theme settings from a local disk asynchronously, you can use ViewTreeObserver.OnPreDrawListener to suspend the app to draw its first frame.

后台数据的加载难免耗时,启动画面结束了主要内容仍未加载好的话,体验不是太好。能够控制启动画面的持续时时长就好了。

现有的ViewTreeObserver的OnPreDrawListener回调是可以挂起描画的,如果我们在数据准备好之后再放行描画,就可以间接地延长启动画面的显示。

比如Activity初始化2s后才放行描画。

class SplashActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

keepSplashScreenLonger()

}

private fun keepSplashScreenLonger() {

// 监听Content View的描画时机

val content: View = findViewById(android.R.id.content)

content.viewTreeObserver.addOnPreDrawListener(

object : ViewTreeObserver.OnPreDrawListener {

override fun onPreDraw(): Boolean {

// 准备好了描画放行,反之挂起

return if (viewModel.isDataReady()) {

content.viewTreeObserver.removeOnPreDrawListener(this)

true

} else {

false

}

}

}

)

}

}

class MyViewModel(application: Application): AndroidViewModel(application) {

companion object {

const val WORK_DURATION = 2000L

}

private val initTime = SystemClock.uptimeMillis()

fun isDataReady() = SystemClock.uptimeMillis() - initTime > WORK_DURATION

}

看一下效果,发现启动画面的展示时间确实变长了。

二、定制退出效果

====================================================================

当App的第一帧开始描画,SplashScreen将会退出展示。为了丰富退出环节的体验,系统也开放了相应的入口,即画面退出的回调。在这个回调里可以开始退出效果的定制,包括整体的退出动画和图标的退出动画。

2.1 监听启动画面的退出

向SplashScreen注册OnExitAnimationListener接口即可监听启动画面的退出。

override fun onCreate(savedInstanceState: Bundle?) {

customizeSplashScreenExit()

}

private fun customizeSplashScreenExit() {

splashScreen.setOnExitAnimationListener { splashScreenView ->

Log.d(“Splash”, “SplashScreen#onSplashScreenExit view:$splashScreenView”)

sleep(1000)

Log.d(“Splash”, “SplashScreen#remove after sleeping”)

splashScreenView.remove()

}

}

可以看到启动画面展示之后,不作定制的默认情况下就是全屏一下再消失。

日志如下:

Splash : Activity:com.example.splash.MainActivity@f70c0d0 Activity:com.example.splash.MainActivity@f70c0d0 onCreate

Splash : Activity:com.example.splash.MainActivity@f70c0d0 onStart

Splash : Activity:com.example.splash.MainActivity@f70c0d0 onResume

Splash : SplashScreen#onSplashScreenExit view:android.window.SplashScreenView{18339d5 V.E… … 0,0-1080,2280}

Splash : SplashScreen#remove after sleeping

一定记得调用remove及时移除启动画面,否则SplashScreen会长时间盖在主画面上,大概在5s左右。

另外,回调的注册需要放在Activity#onResume前,不然监听不到。

2.2 定制整体的退出动画

可以给启动画面的整体设置TRANSLATE、SCALE、ROTATE、ALPHA等各种动画,使得退出更加自然。

比如给SplashScreen加上一个缩小出屏幕的动画。

private fun customizeSplashScreenExit() {

splashScreen.setOnExitAnimationListener { splashScreenView ->

showSplashExitAnimator(splashScreenView)

}

}

private fun showSplashExitAnimator(splashScreenView: SplashScreenView) {

val path = Path()

path.moveTo(1.0f, 1.0f)

path.lineTo(0f, 0f)

val scaleOut = ObjectAnimator.ofFloat(

splashScreenView,

View.SCALE_X,

View.SCALE_Y,

path

)

scaleOut.doOnEnd {

splashScreenView.remove()

}

scaleOut.start()

}

又或者从上方平移出屏幕的动画。

private fun showSplashExitAnimator(splashScreenView: SplashScreenView) {

val slideUp = ObjectAnimator.ofFloat(

splashScreenView,

View.TRANSLATION_Y,

0f,

-splashScreenView.height.toFloat()

)

slideUp.start()

}

2.3 定制图标的退出动画

当然也可以给图标单独加上动画,比如将Icon上滑。

private fun customizeSplashScreenExit() {

splashScreen.setOnExitAnimationListener { splashScreenView ->

showSplashIconExitAnimator(splashScreenView)

}

}

private fun showSplashIconExitAnimator(splashScreenView: SplashScreenView) {

val iconView = splashScreenView.iconView ?: return

val slideUp = ObjectAnimator.ofFloat(

splashScreenView.iconView,

View.TRANSLATION_Y,

0f,

-iconView.height * 2.toFloat()

)

slideUp.start()

}

2.4 退出动画的适当时长

针对退出动画的定制官方还有一段补充说明。

By the start of this callback, the animated vector drawable on the splash screen has begun. Depending on the duration of the app launch, the drawable might be in the middle of its animation. Use SplashScreenView.getIconAnimationStart to know when the animation started. You can calculate the remaining duration of the icon animation.

简言之,退出画面回调的时候Icon动画可能进行到了一半,最好计算Icon动画的剩余时长来执行退出动画。

原因在于设备性能会影响App描画的早晚,而第一帧描画的时候上述的退出回调将被执行。也就是说,性能的优劣会影响启动画面退出的回调时机。

  • 性能好的话,画面退出的回调较早。此时Icon动画尚在进行当中,可以将Icon动画的预设时长的剩余时间交接给退出效果来执行

  • 性能差的话,画面退出的回调稍晚。Icon动画早已经结束,为了让用户尽早看到画面内容,就不该再执行退出效果了而是直接退出

不能为了展示效果而让用户久等,否则会弄巧成拙。

借助SplashScreenView的iconAnimationStartMillis和iconAnimationDurationMillis方法可以推算出Icon动画的剩余时长。

  • 模拟器上运行的缘故,大部分时候我的Demo在启动画面退出的时候Icon动画都结束了,少部分情况下动画还剩余一点时间,可能实机的情况会不一样。

private fun showSplashIconExitAnimator(splashScreenView: SplashScreenView) {

slideUp.duration = getRemainingDuration(splashScreenView)

}

fun getRemainingDuration(splashScreenView: SplashScreenView): Long {

// 取得Icon动画的时长

val animationDuration = splashScreenView.iconAnimationDurationMillis

// 取得Icon动画的开始时刻

val animationStart = splashScreenView.iconAnimationStartMillis

// 再结合当前时间计算出Icon动画的剩余时长

// 1. 时长为负则固定为0ms即直接退出

// 2. 时长为正则采用该时长执行退出动画

return if (animationDuration != null && animationStart != null) {

(animationDuration - SystemClock.uptimeMillis() + animationStart)

.coerceAtLeast(0L)

} else {

0L

}

}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

文末

架构师不是天生的,是在项目中磨练起来的,所以,我们学了技术就需要结合项目进行实战训练,那么在Android里面最常用的架构无外乎 MVC,MVP,MVVM,但是这些思想如果和模块化,层次化,组件化混和在一起,那就不是一件那么简单的事了,我们需要一个真正身经百战的架构师才能讲解透彻其中蕴含的深理。

移动架构师

系统学习技术大纲

一线互联网Android面试题总结含详解(初级到高级专题)

image

大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-QVp5dGkc-1712010437592)]

文末

架构师不是天生的,是在项目中磨练起来的,所以,我们学了技术就需要结合项目进行实战训练,那么在Android里面最常用的架构无外乎 MVC,MVP,MVVM,但是这些思想如果和模块化,层次化,组件化混和在一起,那就不是一件那么简单的事了,我们需要一个真正身经百战的架构师才能讲解透彻其中蕴含的深理。

[外链图片转存中…(img-wi3EVwwn-1712010437592)]

[外链图片转存中…(img-5zvZxiG4-1712010437593)]

一线互联网Android面试题总结含详解(初级到高级专题)

[外链图片转存中…(img-9I1w4Jze-1712010437593)]

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

  • 28
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值