@SuppressLint("CustomSplashScreen")
class SplashActivity : AppCompatActivity() {
code
private lateinit var viewModel: SplashViewModel
private lateinit var fractalView: FractalView
override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
fractalView = findViewById(R.id.fractal_view)
fractalView.fractalType = getRandomFractalType()
viewModel = ViewModelProvider(this).get(SplashViewModel::class.java)
// Keep the splash screen visible until data is ready
splashScreen.setKeepOnScreenCondition { !viewModel.isReady }
splashScreen.setOnExitAnimationListener { splashScreenView ->
val slideUp = ObjectAnimator.ofFloat(
splashScreenView, View.TRANSLATION_Y, 0f, -splashScreenView.height.toFloat()
)
slideUp.interpolator = AnticipateInterpolator()
slideUp.duration = 500L
slideUp.doOnEnd { splashScreenView.remove() }
slideUp.start()
}
// Start loading data
viewModel.loadData()
// Pre-draw listener (optional)
val content: View = findViewById(R.id.splash_root)
content.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
return if (viewModel.isReady) {
content.viewTreeObserver.removeOnPreDrawListener(this)
true
} else {
false
}
}
})
viewModel.isReadyLiveData.observe(this) { isReady ->
if (isReady) {
// Navigate to MainActivity
startActivity(Intent(this, MainActivity::class.java))
finish()
}
}
}
private fun getRandomFractalType(): FractalType {
val fractalTypes = FractalType.values()
val randomIndex = Random.nextInt(fractalTypes.size)
return fractalTypes[randomIndex]
}
Assistant
}
Unresolved reference: height
Variable expected:39
It seems you're encountering several errors in your SplashActivity. Let's address each one step by step.
Error 1:
code
None of the following functions can be called with the arguments supplied:
...
Cause: The ObjectAnimator.ofFloat function is being called with incorrect arguments.
Error 2:
code
Unresolved reference: height
Cause: The height property is not directly available on splashScreenView.
Error 3:
code
Variable expected:39
Cause: Likely due to incorrect usage of a function or missing imports.
Solution:
Update the ObjectAnimator.ofFloat call:
The second parameter of ofFloat expects a String property name or a Property object. The View.TRANSLATION_Y you're using is not a valid property in this context. Instead, you should use the string "translationY".
The splashScreenView object doesn't have a height property directly. However, you can access its underlying view or iconView, which does have a height.
Use:
kotlin
splashScreenView.view.height
// or
splashScreenView.iconView.height
Import the doOnEnd extension function:
The doOnEnd function is an extension function that requires an import.
Add:
kotlin
import androidx.core.animation.doOnEnd
Updated Code:
kotlin
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewTreeObserver
import android.view.animation.AnticipateInterpolator
import androidx.appcompat.app.AppCompatActivity
import androidx.core.animation.doOnEnd
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.ViewModelProvider
import kotlin.random.Random
@SuppressLint("CustomSplashScreen")
class SplashActivity : AppCompatActivity() {
private lateinit var viewModel: SplashViewModel
private lateinit var fractalView: FractalView
override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_splash)
fractalView = findViewById(R.id.fractal_view)
fractalView.fractalType = getRandomFractalType()
viewModel = ViewModelProvider(this).get(SplashViewModel::class.java)
// Keep the splash screen visible until data is ready
splashScreen.setKeepOnScreenCondition { !viewModel.isReady }
splashScreen.setOnExitAnimationListener { splashScreenView ->
val slideUp = ObjectAnimator.ofFloat(
splashScreenView.iconView, "translationY", 0f, -splashScreenView.iconView.height.toFloat()
)
slideUp.interpolator = AnticipateInterpolator()
slideUp.duration = 500L
slideUp.doOnEnd { splashScreenView.remove() }
slideUp.start()
}
// Start loading data
viewModel.loadData()
// Pre-draw listener (optional)
val content: View = findViewById(R.id.splash_root)
content.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
return if (viewModel.isReady) {
content.viewTreeObserver.removeOnPreDrawListener(this)
true
} else {
false
}
}
})
viewModel.isReadyLiveData.observe(this) { isReady ->
if (isReady) {
// Navigate to MainActivity
startActivity(Intent(this, MainActivity::class.java))
finish()
}
}
}
private fun getRandomFractalType(): FractalType {
val fractalTypes = FractalType.values()
val randomIndex = Random.nextInt(fractalTypes.size)
return fractalTypes[randomIndex]
}
}
Explanation:
import androidx.core.animation.doOnEnd: This adds the missing import for the doOnEnd extension function.
Using "translationY": The property name is passed as a string to ofFloat instead of View.TRANSLATION_Y.
Accessing iconView.height: The splashScreenView's iconView provides access to the height property.
Notes:
Ensure that your FractalView and SplashViewModel classes are implemented correctly.
The SplashScreen API might have variations depending on the Android version. Make sure you have the correct dependencies and your app's compileSdkVersion and targetSdkVersion are set appropriately.
If you continue to face issues, consider cleaning your project (Build > Clean Project) and rebuilding it.
By making these changes, you should resolve the errors and have a functioning splash screen with a custom exit animation.