diff --git a/app/src/main/java/com/aiosman/riderpro/ui/login/login.kt b/app/src/main/java/com/aiosman/riderpro/ui/login/login.kt index 39c08ae..5fd41de 100644 --- a/app/src/main/java/com/aiosman/riderpro/ui/login/login.kt +++ b/app/src/main/java/com/aiosman/riderpro/ui/login/login.kt @@ -4,13 +4,8 @@ import android.content.ContentValues.TAG import android.content.res.Resources import android.util.Log import android.widget.Toast -import androidx.compose.animation.core.LinearEasing -import androidx.compose.animation.core.animateFloatAsState -import androidx.compose.animation.core.infiniteRepeatable -import androidx.compose.animation.core.tween import androidx.compose.foundation.Image import androidx.compose.foundation.background -import androidx.compose.foundation.border import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.gestures.rememberScrollableState import androidx.compose.foundation.gestures.scrollable @@ -23,11 +18,9 @@ import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width -import androidx.compose.foundation.layout.widthIn import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.verticalScroll @@ -43,7 +36,6 @@ import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.scale import androidx.compose.ui.graphics.Brush @@ -51,16 +43,11 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp -import androidx.lifecycle.Lifecycle -import androidx.lifecycle.LifecycleEventObserver -import androidx.lifecycle.LifecycleOwner -import androidx.lifecycle.compose.LocalLifecycleOwner import com.aiosman.riderpro.AppState import com.aiosman.riderpro.AppStore import com.aiosman.riderpro.LocalNavController @@ -73,7 +60,6 @@ import com.aiosman.riderpro.ui.modifiers.noRippleClickable import com.aiosman.riderpro.utils.GoogleLogin import com.google.accompanist.systemuicontroller.rememberSystemUiController import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.cancel import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -237,35 +223,25 @@ fun MovingImageWall(resources: Resources) { val imageHeight = 208.dp val imageHeightPx = imageHeight.value * density // 将 dp 转换为像素 val resetThreshold = imageHeightPx - var offset1 by remember { mutableFloatStateOf( -resetThreshold * 3) } + // 使用 remember 保存动画状态,并在应用停止时重置 + // 每次 recomposition 时重置 offset + var offset1 by remember { mutableFloatStateOf(-resetThreshold * 3) } var offset2 by remember { mutableFloatStateOf(0f) } - var offset3 by remember { mutableFloatStateOf( -resetThreshold * 3) } + var offset3 by remember { mutableFloatStateOf(-resetThreshold * 3) } - val lifecycleOwner = LocalLifecycleOwner.current - val coroutineScope = rememberCoroutineScope() // 使用 rememberCoroutineScope - LaunchedEffect(key1 = lifecycleOwner) { - lifecycleOwner.lifecycle.addObserver(object : LifecycleEventObserver { - override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { - when (event) { - Lifecycle.Event.ON_RESUME -> { - coroutineScope.launch { // 在 coroutineScope 中启动协程 - animateImageWall(imageList1, offset1, speed = 1f, resources = resources) { offset1 = it } - } - coroutineScope.launch { - animateImageWall(imageList2, offset2, speed = 1.5f, reverse = true, resources = resources) { offset2 = it } - } - coroutineScope.launch { - animateImageWall(imageList3, offset3, speed = 2f, resources = resources) { offset3 = it } - } - } - Lifecycle.Event.ON_PAUSE -> { - // 可选: 在Composable暂停时取消协程 -// coroutineScope.cancel() - } - else -> {} - } - } - }) + val coroutineScope = rememberCoroutineScope() + + // 使用 LaunchedEffect 在每次 recomposition 时启动动画 + LaunchedEffect(Unit) { + coroutineScope.launch { + animateImageWall(imageList1, offset1, speed = 1f, resources = resources) { offset1 = it } + } + coroutineScope.launch { + animateImageWall(imageList2, offset2, speed = 1.5f, reverse = true, resources = resources) { offset2 = it } + } + coroutineScope.launch { + animateImageWall(imageList3, offset3, speed = 2f, resources = resources) { offset3 = it } + } } Box(