近期,一些 Android 12+ 设备上的动画启动画面 API 不起作用的问题已经被发现。为了解决这个问题,你可以使用以下代码示例中的方式创建一个交互式多媒体启动画面,以便在 Android 12+ 设备上正常工作。
在你的 Android 项目中添加以下类:
import android.app.Activity
import android.content.Intent
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.annotation.DrawableRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updateLayoutParams
import androidx.core.view.updateMargins
import androidx.dynamicanimation.animation.ObjectAnimatorCompat
import androidx.dynamicanimation.animation.SpringAnimation
import androidx.dynamicanimation.animation.SpringForce
class InteractiveSplashScreenActivity : AppCompatActivity() {
companion object {
const val EXTRA_BACKGROUND_COLOR = "extra_background_color"
const val EXTRA_LOGO = "extra_logo"
const val EXTRA_LOGO_SCALE_TYPE = "extra_logo_scale_tpe"
const val EXTRA_TITLE = "extra_title"
const val EXTRA_SUBTITLE = "extra_subtitle"
fun intent(
activity: Activity,
@DrawableRes logoRes: Int,
scaleType: Int,
background: Int,
title: String,
subtitle: String
): Intent {
return Intent(activity, InteractiveSplashScreenActivity::class.java).apply {
putExtra(EXTRA_BACKGROUND_COLOR, background)
putExtra(EXTRA_LOGO, logoRes)
putExtra(EXTRA_LOGO_SCALE_TYPE, scaleType)
putExtra(EXTRA_TITLE, title)
putExtra(EXTRA_SUBTITLE, subtitle)
}
}
}
private val background: Int by lazy {
intent.getIntExtra(EXTRA_BACKGROUND_COLOR, R.color.white)
}
private val logoRes: Int by lazy {
intent.getIntExtra(EXTRA_LOGO, R.drawable.ic_launcher_background)
}
private val logoScaleType: Int by lazy {
intent.getIntExtra(EXTRA_LOGO_SCALE_TYPE, ImageView.ScaleType.CENTER_INSIDE.ordinal)