From 09721f1747beee2be7538fd9e1cfda0ef98725b6 Mon Sep 17 00:00:00 2001 From: Hinnka Date: Sat, 10 Jan 2026 13:27:57 +0800 Subject: [PATCH] feat: auto scroll to item --- .../mycamera/ui/components/LutSelector.kt | 37 +++++++++++--- .../mycamera/ui/gallery/PhotoEditScreen.kt | 48 +++++++++++++------ .../mycamera/ui/settings/SettingsScreen.kt | 38 +++++++++++---- 3 files changed, 92 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/hinnka/mycamera/ui/components/LutSelector.kt b/app/src/main/java/com/hinnka/mycamera/ui/components/LutSelector.kt index e6425434..94691297 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/components/LutSelector.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/components/LutSelector.kt @@ -1,8 +1,14 @@ package com.hinnka.mycamera.ui.components import android.graphics.Bitmap -import androidx.compose.foundation.* +import androidx.compose.foundation.Image +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* +import androidx.compose.foundation.lazy.LazyRow +import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Check @@ -10,6 +16,8 @@ import androidx.compose.material.icons.filled.FilterNone import androidx.compose.material3.Icon import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip @@ -17,14 +25,15 @@ import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp -import androidx.compose.ui.res.stringResource import com.hinnka.mycamera.R import com.hinnka.mycamera.lut.LutInfo +import kotlinx.coroutines.launch /** * LUT 选择器组件 @@ -39,15 +48,31 @@ fun LutSelector( onLutSelected: (String?) -> Unit, modifier: Modifier = Modifier ) { - Row( + val scrollState = rememberLazyListState() + val coroutineScope = rememberCoroutineScope() + + // 在组件首次加载时滚动到当前选中的 LUT + LaunchedEffect(currentLutId) { + currentLutId?.let { lutId -> + val selectedIndex = availableLuts.indexOfFirst { it.id == lutId } + if (selectedIndex >= 2) { + coroutineScope.launch { + // 使用 animateScrollTo 进行平滑滚动,将选中项滚动到可视区域中心 + scrollState.animateScrollToItem(selectedIndex - 2) + } + } + } + } + + LazyRow( modifier = modifier .fillMaxWidth() - .horizontalScroll(rememberScrollState()) .padding(vertical = 8.dp), - horizontalArrangement = Arrangement.spacedBy(8.dp) + horizontalArrangement = Arrangement.spacedBy(8.dp), + state = scrollState ) { // LUT 列表 - availableLuts.forEach { lut -> + items(availableLuts) { lut -> LutItem( name = lut.getName(), previewBitmap = lutPreviewBitmaps[lut.id], diff --git a/app/src/main/java/com/hinnka/mycamera/ui/gallery/PhotoEditScreen.kt b/app/src/main/java/com/hinnka/mycamera/ui/gallery/PhotoEditScreen.kt index a6dc7792..00dec869 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/gallery/PhotoEditScreen.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/gallery/PhotoEditScreen.kt @@ -1,7 +1,9 @@ package com.hinnka.mycamera.ui.gallery +import android.app.Activity +import android.content.Context +import android.content.ContextWrapper import android.graphics.Bitmap -import android.util.Log import androidx.activity.compose.BackHandler import androidx.compose.foundation.Image import androidx.compose.foundation.background @@ -11,13 +13,11 @@ import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyRow import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.itemsIndexed -import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ArrowBack import androidx.compose.material.icons.filled.Check -import androidx.compose.material.icons.filled.Rotate90DegreesCw -import androidx.compose.material.icons.filled.Save import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment @@ -28,6 +28,7 @@ import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp @@ -36,15 +37,11 @@ import coil.compose.AsyncImage import coil.request.ImageRequest import com.hinnka.mycamera.R import com.hinnka.mycamera.ui.components.CustomSlider +import com.hinnka.mycamera.ui.components.PaymentDialog import com.hinnka.mycamera.ui.theme.AccentOrange import com.hinnka.mycamera.viewmodel.GalleryViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext -import com.hinnka.mycamera.ui.components.PaymentDialog -import android.app.Activity -import android.content.Context -import android.content.ContextWrapper -import androidx.compose.ui.text.font.FontWeight /** * 照片编辑界面 @@ -69,6 +66,8 @@ fun PhotoEditScreen( var isSaving by remember { mutableStateOf(false) } var showExportDialog by remember { mutableStateOf(false) } var isLoadingPreview by remember { mutableStateOf(false) } + val lutScrollState = rememberLazyListState() + val frameScrollState = rememberLazyListState() BackHandler { viewModel.exitEditMode() @@ -81,10 +80,10 @@ fun PhotoEditScreen( // 边框编辑状态 val editFrameId = viewModel.editFrameId - val editShowAppBranding = viewModel.editShowAppBranding + val availableFrames = viewModel.availableFrames // 当编辑参数变化时更新预览 - LaunchedEffect(currentPhoto, editLutId, editLutConfig, editLutIntensity, rotation, brightness, editFrameId, editShowAppBranding) { + LaunchedEffect(currentPhoto, editLutId, editLutConfig, editLutIntensity, rotation, brightness, editFrameId) { if (currentPhoto == null) return@LaunchedEffect isLoadingPreview = true @@ -96,6 +95,24 @@ fun PhotoEditScreen( } isLoadingPreview = false } + + LaunchedEffect(editLutId) { + editLutId?.let { lutId -> + val selectedIndex = availableLuts.indexOfFirst { it.id == lutId } + if (selectedIndex >= 2) { + lutScrollState.animateScrollToItem(selectedIndex - 2) + } + } + } + + LaunchedEffect(editFrameId) { + editFrameId?.let { lutId -> + val selectedIndex = availableFrames.indexOfFirst { it.id == lutId } + if (selectedIndex >= 1) { + frameScrollState.animateScrollToItem(selectedIndex - 1) + } + } + } if (currentPhoto == null) { LaunchedEffect(Unit) { @@ -256,7 +273,8 @@ fun PhotoEditScreen( Spacer(modifier = Modifier.height(8.dp)) LazyRow( - horizontalArrangement = Arrangement.spacedBy(12.dp) + horizontalArrangement = Arrangement.spacedBy(12.dp), + state = lutScrollState ) { // LUT 选项 itemsIndexed(availableLuts) { index, lut -> @@ -309,11 +327,11 @@ fun PhotoEditScreen( ) Spacer(modifier = Modifier.height(8.dp)) - - val availableFrames = viewModel.availableFrames + LazyRow( - horizontalArrangement = Arrangement.spacedBy(12.dp) + horizontalArrangement = Arrangement.spacedBy(12.dp), + state = frameScrollState ) { // 无边框选项 item { diff --git a/app/src/main/java/com/hinnka/mycamera/ui/settings/SettingsScreen.kt b/app/src/main/java/com/hinnka/mycamera/ui/settings/SettingsScreen.kt index 36580b00..66c8b1d4 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/settings/SettingsScreen.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/settings/SettingsScreen.kt @@ -2,6 +2,9 @@ package com.hinnka.mycamera.ui.settings import androidx.compose.foundation.* import androidx.compose.foundation.layout.* +import androidx.compose.foundation.lazy.LazyRow +import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.icons.Icons @@ -10,6 +13,7 @@ import androidx.compose.material.icons.filled.Check import androidx.compose.material.icons.filled.FilterNone import androidx.compose.material3.* import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment @@ -341,6 +345,18 @@ fun FrameWatermarkSetting( onFrameSelected: (String?) -> Unit, modifier: Modifier = Modifier ) { + + val frameScrollState = rememberLazyListState() + + LaunchedEffect(currentFrameId) { + currentFrameId?.let { lutId -> + val selectedIndex = availableFrames.indexOfFirst { it.id == lutId } + if (selectedIndex >= 1) { + frameScrollState.animateScrollToItem(selectedIndex - 1) + } + } + } + Column(modifier = modifier.fillMaxWidth()) { Text( text = stringResource(R.string.settings_frame_style), @@ -351,23 +367,25 @@ fun FrameWatermarkSetting( ) // 边框选择器 - Row( + LazyRow( modifier = Modifier .fillMaxWidth() - .horizontalScroll(rememberScrollState()) .padding(vertical = 8.dp), - horizontalArrangement = Arrangement.spacedBy(8.dp) + horizontalArrangement = Arrangement.spacedBy(8.dp), + state = frameScrollState ) { // "无边框" 选项 - FrameItem( - name = stringResource(R.string.none), - isSelected = currentFrameId == null, - onClick = { onFrameSelected(null) }, - isNone = true - ) + item { + FrameItem( + name = stringResource(R.string.none), + isSelected = currentFrameId == null, + onClick = { onFrameSelected(null) }, + isNone = true + ) + } // 边框列表 - availableFrames.forEach { frame -> + items(availableFrames) { frame -> FrameItem( name = frame.name, isSelected = currentFrameId == frame.id,