feat: implement customizable and custom aspect ratios

This commit is contained in:
zhoubinjia committed 2026-05-15 17:39:54 +08:00
1 parent a86e0cc018
commit 6bf8177b8a
10 files changed
+664 -52

No files matched your search

@@ -9,15 +9,25 @@ import com.hinnka.mycamera.video.VideoConfig
import com.hinnka.mycamera.video.VideoRecordingState
/**
* 画面比例枚举
* 画面比例。内置比例使用稳定名称持久化,自定义比例使用 CUSTOM_宽_高。
*/
enum class AspectRatio(val widthRatio: Int, val heightRatio: Int) {
RATIO_3_2(3, 2),
RATIO_4_3(4, 3),
RATIO_16_9(16, 9),
RATIO_1_1(1, 1),
RATIO_21_9(21, 9),
XPAN(65, 24);
class AspectRatio private constructor(
val name: String,
val widthRatio: Int,
val heightRatio: Int
) {
override fun equals(other: Any?): Boolean {
return other is AspectRatio && name == other.name
}
override fun hashCode(): Int {
return name.hashCode()
}
override fun toString(): String {
return name
}
fun getValue(isLandscape: Boolean): Float {
return if (isLandscape) {
@@ -35,8 +45,83 @@ enum class AspectRatio(val widthRatio: Int, val heightRatio: Int) {
}
companion object {
const val TOP_SHEET_MAX_COUNT = 5
private const val CUSTOM_PREFIX = "CUSTOM_"
val RATIO_3_2 = AspectRatio("RATIO_3_2", 3, 2)
val RATIO_4_3 = AspectRatio("RATIO_4_3", 4, 3)
val RATIO_16_9 = AspectRatio("RATIO_16_9", 16, 9)
val RATIO_1_1 = AspectRatio("RATIO_1_1", 1, 1)
val RATIO_21_9 = AspectRatio("RATIO_21_9", 21, 9)
val XPAN = AspectRatio("XPAN", 65, 24)
val entries: List<AspectRatio> = listOf(
RATIO_3_2,
RATIO_4_3,
RATIO_16_9,
RATIO_1_1,
RATIO_21_9,
XPAN
)
val defaultTopSheetRatios: List<AspectRatio> = listOf(
RATIO_3_2,
RATIO_4_3,
RATIO_16_9,
RATIO_1_1,
XPAN
)
fun fromString(string: String): AspectRatio {
return entries.firstOrNull { it.getDisplayName() == string } ?: RATIO_4_3
return valueOfOrNull(string) ?: entries.firstOrNull { it.getDisplayName() == string } ?: RATIO_4_3
}
fun valueOf(name: String): AspectRatio {
return valueOfOrNull(name) ?: throw IllegalArgumentException("Unknown AspectRatio: $name")
}
fun valueOfOrNull(name: String): AspectRatio? {
entries.firstOrNull { it.name == name }?.let { return it }
if (!name.startsWith(CUSTOM_PREFIX)) return null
val parts = name.removePrefix(CUSTOM_PREFIX).split("_")
if (parts.size != 2) return null
val width = parts[0].toIntOrNull() ?: return null
val height = parts[1].toIntOrNull() ?: return null
return custom(width, height)
}
fun custom(widthRatio: Int, heightRatio: Int): AspectRatio {
val width = widthRatio.coerceIn(1, 999)
val height = heightRatio.coerceIn(1, 999)
val divisor = gcd(width, height)
val normalizedWidth = width / divisor
val normalizedHeight = height / divisor
entries.firstOrNull {
it.widthRatio == normalizedWidth && it.heightRatio == normalizedHeight
}?.let { return it }
return AspectRatio(
name = "${CUSTOM_PREFIX}${normalizedWidth}_${normalizedHeight}",
widthRatio = normalizedWidth,
heightRatio = normalizedHeight
)
}
fun sanitizeTopSheetRatios(ratios: List<AspectRatio>): List<AspectRatio> {
val sanitized = ratios
.distinct()
.take(TOP_SHEET_MAX_COUNT)
return sanitized.ifEmpty { defaultTopSheetRatios }
}
fun sanitizeCustomRatios(ratios: List<AspectRatio>): List<AspectRatio> {
return ratios
.map { custom(it.widthRatio, it.heightRatio) }
.filter { ratio -> entries.none { it.name == ratio.name } }
.distinctBy { it.name }
}
private tailrec fun gcd(a: Int, b: Int): Int {
return if (b == 0) kotlin.math.abs(a) else gcd(b, a % b)
}
}
}
@@ -9,6 +9,7 @@ import androidx.datastore.preferences.core.floatPreferencesKey
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import com.hinnka.mycamera.camera.AspectRatio
import com.hinnka.mycamera.camera.MultiFrameConfig
import com.hinnka.mycamera.camera.MeteringMode
import com.hinnka.mycamera.lut.BaselineColorCorrectionTarget
@@ -51,6 +52,8 @@ enum class WidgetTheme {
data class UserPreferences(
val captureMode: CaptureMode = CaptureMode.PHOTO,
val aspectRatio: String = "RATIO_4_3",
val topSheetAspectRatios: List<AspectRatio> = AspectRatio.defaultTopSheetRatios,
val customAspectRatios: List<AspectRatio> = emptyList(),
val lutId: String? = null, // 默认为 null,由 CameraViewModel 根据配置文件设置
val phantomLutId: String? = null,
val jpgBaselineLutId: String? = null,
@@ -149,6 +152,8 @@ class UserPreferencesRepository(private val context: Context) {
// DataStore Keys
private val CAPTURE_MODE = stringPreferencesKey("capture_mode")
private val ASPECT_RATIO_KEY = stringPreferencesKey("aspect_ratio")
private val TOP_SHEET_ASPECT_RATIOS = stringPreferencesKey("top_sheet_aspect_ratios")
private val CUSTOM_ASPECT_RATIOS = stringPreferencesKey("custom_aspect_ratios")
private val LUT_ID_KEY = stringPreferencesKey("lut_id")
private val PHANTOM_LUT_ID_KEY = stringPreferencesKey("phantom_lut_id")
private val JPG_BASELINE_LUT_ID_KEY = stringPreferencesKey("jpg_baseline_lut_id")
@@ -252,9 +257,16 @@ class UserPreferencesRepository(private val context: Context) {
*/
val userPreferences: Flow<UserPreferences> = context.dataStore.data
.map { preferences ->
val customAspectRatios = parseCustomAspectRatios(preferences[CUSTOM_ASPECT_RATIOS])
val availableAspectRatios = AspectRatio.entries + customAspectRatios
UserPreferences(
captureMode = CaptureMode.valueOf(preferences[CAPTURE_MODE] ?: CaptureMode.PHOTO.name),
aspectRatio = preferences[ASPECT_RATIO_KEY] ?: "RATIO_4_3",
topSheetAspectRatios = parseTopSheetAspectRatios(
preferences[TOP_SHEET_ASPECT_RATIOS],
availableAspectRatios
),
customAspectRatios = customAspectRatios,
lutId = preferences[LUT_ID_KEY], // 不提供默认值,由 CameraViewModel 处理
phantomLutId = preferences[PHANTOM_LUT_ID_KEY],
jpgBaselineLutId = preferences[JPG_BASELINE_LUT_ID_KEY],
@@ -463,6 +475,31 @@ class UserPreferencesRepository(private val context: Context) {
return result
}
private fun parseCustomAspectRatios(value: String?): List<AspectRatio> {
if (value.isNullOrBlank()) {
return emptyList()
}
return AspectRatio.sanitizeCustomRatios(
value.split(",")
.mapNotNull { name -> AspectRatio.valueOfOrNull(name) }
)
}
private fun parseTopSheetAspectRatios(
value: String?,
availableAspectRatios: List<AspectRatio>
): List<AspectRatio> {
if (value.isNullOrBlank()) {
return AspectRatio.defaultTopSheetRatios
}
val availableByName = availableAspectRatios.associateBy { it.name }
val ratios = value.split(",")
.mapNotNull { name ->
availableByName[name] ?: AspectRatio.valueOfOrNull(name)
}
return AspectRatio.sanitizeTopSheetRatios(ratios)
}
/**
* 保存最近拍摄模式
*/
@@ -481,6 +518,20 @@ class UserPreferencesRepository(private val context: Context) {
}
}
suspend fun saveTopSheetAspectRatios(aspectRatios: List<AspectRatio>) {
context.dataStore.edit { preferences ->
preferences[TOP_SHEET_ASPECT_RATIOS] = AspectRatio.sanitizeTopSheetRatios(aspectRatios)
.joinToString(",") { it.name }
}
}
suspend fun saveCustomAspectRatios(aspectRatios: List<AspectRatio>) {
context.dataStore.edit { preferences ->
preferences[CUSTOM_ASPECT_RATIOS] = AspectRatio.sanitizeCustomRatios(aspectRatios)
.joinToString(",") { it.name }
}
}
/**
* 保存 LUT 配置
*/
@@ -127,6 +127,7 @@ fun CameraScreen(
val enableDevelopAnimation by viewModel.enableDevelopAnimation.collectAsState()
val hlgHardwareCompatibilityEnabled by viewModel.hlgHardwareCompatibilityEnabled.collectAsState()
val phantomMode by viewModel.phantomMode.collectAsState()
val topSheetAspectRatios by viewModel.topSheetAspectRatios.collectAsState()
val videoCodec by viewModel.videoCodec.collectAsState()
val videoAudioInputOptions by viewModel.videoAudioInputOptions.collectAsState()
val phantomPipPreview by viewModel.phantomPipPreview.collectAsState()
@@ -1116,6 +1117,7 @@ fun CameraScreen(
visible = activePanel == ActivePanel.SETTINGS,
captureMode = state.captureMode,
aspectRatio = state.aspectRatio,
topSheetAspectRatios = topSheetAspectRatios,
onAspectRatioChange = { runPreviewTransition { viewModel.setAspectRatio(it) } },
videoAspectRatio = state.videoConfig.aspectRatio,
onVideoAspectRatioChange = { runPreviewTransition { viewModel.setVideoAspectRatio(it) } },
@@ -47,6 +47,7 @@ fun CameraTopSheet(
visible: Boolean,
captureMode: CaptureMode,
aspectRatio: AspectRatio,
topSheetAspectRatios: List<AspectRatio>,
onAspectRatioChange: (AspectRatio) -> Unit,
videoAspectRatio: VideoAspectRatio,
onVideoAspectRatioChange: (VideoAspectRatio) -> Unit,
@@ -127,7 +128,7 @@ fun CameraTopSheet(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
AspectRatio.entries.forEach { ratio ->
AspectRatio.sanitizeTopSheetRatios(topSheetAspectRatios).forEach { ratio ->
val isSelected = aspectRatio == ratio
Box(
modifier = Modifier
@@ -29,6 +29,11 @@ import androidx.compose.ui.unit.sp
import com.hinnka.mycamera.R
import com.hinnka.mycamera.camera.AspectRatio
import com.hinnka.mycamera.ui.theme.AccentOrange
import androidx.compose.ui.Alignment
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Article
import androidx.compose.material.icons.filled.FilterNone
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min
@@ -77,15 +82,18 @@ sealed class CropAspectOption(
/**
* 获取所有可用的裁剪选项列表
*/
fun getCropAspectOptions(): List<CropAspectOption> {
fun getCropAspectOptions(allRatios: List<AspectRatio>): List<CropAspectOption> {
val options = mutableListOf<CropAspectOption>()
options.add(CropAspectOption.Free)
options.add(CropAspectOption.Original)
for (ratio in AspectRatio.entries) {
// Sort ratios to put common ones first if needed, but here we just take them all
for (ratio in allRatios) {
val option = CropAspectOption.FromAspectRatio(ratio)
options.add(option)
if (option.widthRatio / option.heightRatio != 1f) {
options.add(CropAspectOption.Custom(option.heightRatio, option.widthRatio))
// Also add the inverted version (e.g., 3:4 for 4:3) if not square
if (ratio.widthRatio != ratio.heightRatio) {
options.add(CropAspectOption.Custom(ratio.heightRatio.toFloat(), ratio.widthRatio.toFloat()))
}
}
return options
@@ -98,46 +106,58 @@ fun getCropAspectOptions(): List<CropAspectOption> {
fun CropEditPanel(
selectedOption: CropAspectOption,
onOptionSelected: (CropAspectOption) -> Unit,
availableRatios: List<AspectRatio>,
imageWidth: Int,
imageHeight: Int,
modifier: Modifier = Modifier
) {
val options = remember {
getCropAspectOptions()
val options = remember(availableRatios) {
getCropAspectOptions(availableRatios)
}
Column(
modifier = modifier
.fillMaxWidth()
.padding(vertical = 16.dp)
.padding(vertical = 12.dp)
) {
// 比例选择标题
Text(
text = stringResource(R.string.crop).uppercase(),
color = Color.White.copy(alpha = 0.4f),
fontSize = 10.sp,
fontWeight = FontWeight.Bold,
letterSpacing = 1.sp
)
Row(
modifier = Modifier.fillMaxWidth().padding(horizontal = 4.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = stringResource(R.string.crop).uppercase(),
color = Color.White.copy(alpha = 0.4f),
fontSize = 11.sp,
fontWeight = FontWeight.Bold,
letterSpacing = 1.sp
)
Text(
text = when (selectedOption) {
is CropAspectOption.Free -> stringResource(R.string.crop_free)
is CropAspectOption.Original -> stringResource(R.string.crop_original)
else -> selectedOption.displayName
},
color = AccentOrange,
fontSize = 12.sp,
fontWeight = FontWeight.Bold
)
}
Spacer(modifier = Modifier.height(4.dp))
Text(
text = selectedOption.displayName,
color = Color.White,
fontSize = 16.sp,
fontWeight = FontWeight.Bold
)
Spacer(modifier = Modifier.height(12.dp))
Spacer(modifier = Modifier.height(16.dp))
LazyRow(
horizontalArrangement = Arrangement.spacedBy(8.dp)
contentPadding = PaddingValues(horizontal = 4.dp),
horizontalArrangement = Arrangement.spacedBy(10.dp)
) {
items(options) { option ->
CropAspectOptionItem(
option = option,
isSelected = option == selectedOption,
isSelected = option.displayName == selectedOption.displayName &&
option.widthRatio == selectedOption.widthRatio &&
option.heightRatio == selectedOption.heightRatio,
onClick = { onOptionSelected(option) }
)
}
@@ -160,26 +180,82 @@ private fun CropAspectOptionItem(
else -> option.displayName
}
Box(
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.clip(RoundedCornerShape(16.dp))
.background(
if (isSelected) AccentOrange.copy(alpha = 0.2f)
else Color.White.copy(alpha = 0.08f)
)
.border(
1.dp,
if (isSelected) AccentOrange else Color.White.copy(alpha = 0.15f),
RoundedCornerShape(16.dp)
)
.width(64.dp)
.clip(RoundedCornerShape(12.dp))
.clickable { onClick() }
.padding(horizontal = 14.dp, vertical = 8.dp)
.padding(vertical = 4.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
// Visual Preview
Box(
modifier = Modifier
.size(48.dp)
.background(
if (isSelected) AccentOrange.copy(alpha = 0.15f)
else Color.White.copy(alpha = 0.05f),
RoundedCornerShape(10.dp)
)
.border(
1.dp,
if (isSelected) AccentOrange else Color.White.copy(alpha = 0.1f),
RoundedCornerShape(10.dp)
),
contentAlignment = Alignment.Center
) {
when (option) {
is CropAspectOption.Free -> {
Icon(
imageVector = Icons.Default.FilterNone,
contentDescription = null,
tint = if (isSelected) AccentOrange else Color.White.copy(alpha = 0.5f),
modifier = Modifier.size(20.dp)
)
}
is CropAspectOption.Original -> {
Icon(
imageVector = Icons.Default.Article,
contentDescription = null,
tint = if (isSelected) AccentOrange else Color.White.copy(alpha = 0.5f),
modifier = Modifier.size(20.dp)
)
}
else -> {
val w = option.widthRatio
val h = option.heightRatio
val maxDim = 24.dp
val displayW: androidx.compose.ui.unit.Dp
val displayH: androidx.compose.ui.unit.Dp
if (w > h) {
displayW = maxDim
displayH = maxDim * (h / w)
} else {
displayH = maxDim
displayW = maxDim * (w / h)
}
Box(
modifier = Modifier
.size(displayW, displayH)
.background(
if (isSelected) AccentOrange else Color.White.copy(alpha = 0.4f),
RoundedCornerShape(2.dp)
)
)
}
}
}
Text(
text = displayText,
color = if (isSelected) AccentOrange else Color.White.copy(alpha = 0.7f),
fontSize = 13.sp,
fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal
color = if (isSelected) AccentOrange else Color.White.copy(alpha = 0.6f),
fontSize = 11.sp,
fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Medium,
maxLines = 1,
textAlign = TextAlign.Center
)
}
}
@@ -921,9 +921,11 @@ fun PhotoEditScreen(
}
3 -> {
// 裁剪编辑
val availablePhotoAspectRatios by cameraViewModel.availablePhotoAspectRatios.collectAsState()
CropEditPanel(
selectedOption = editCropAspectOption,
onOptionSelected = { viewModel.setCropAspectOption(it) },
availableRatios = availablePhotoAspectRatios,
imageWidth = previewBitmap?.width ?: 1,
imageHeight = previewBitmap?.height ?: 1
)
@@ -95,9 +95,9 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LifecycleEventEffect
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.ui.text.input.KeyboardType
import androidx.media3.common.DeviceInfo
import com.hinnka.mycamera.BuildConfig
import com.hinnka.mycamera.R
import com.hinnka.mycamera.camera.AspectRatio
import com.hinnka.mycamera.camera.MultiFrameConfig
import com.hinnka.mycamera.data.VolumeKeyAction
import com.hinnka.mycamera.frame.FrameInfo
@@ -153,6 +153,9 @@ fun SettingsScreen(
val shutterSoundEnabled by viewModel.shutterSoundEnabled.collectAsState(initial = true)
val vibrationEnabled by viewModel.vibrationEnabled.collectAsState(initial = true)
val volumeKeyAction by viewModel.volumeKeyAction.collectAsState()
val topSheetAspectRatios by viewModel.topSheetAspectRatios.collectAsState()
val customAspectRatios by viewModel.customAspectRatios.collectAsState()
val availablePhotoAspectRatios by viewModel.availablePhotoAspectRatios.collectAsState()
val autoSaveAfterCapture by viewModel.autoSaveAfterCapture.collectAsState(initial = true)
val nrLevel by viewModel.nrLevel.collectAsState(initial = 5)
val edgeLevel by viewModel.edgeLevel.collectAsState(initial = 1)
@@ -215,6 +218,7 @@ fun SettingsScreen(
var rawExposureCompensationUi by remember { mutableStateOf(rawExposureCompensation) }
var rawBlackPointCorrectionUi by remember { mutableStateOf(rawBlackPointCorrection) }
var rawWhitePointCorrectionUi by remember { mutableStateOf(rawWhitePointCorrection) }
var showAspectRatioDialog by remember { mutableStateOf(false) }
LaunchedEffect(rawNlmNoiseFactor, rawExposureCompensation, rawBlackPointCorrection, rawWhitePointCorrection) {
if (!isRawSliderAdjusting) {
@@ -732,6 +736,34 @@ fun SettingsScreen(
modifier = Modifier.padding(vertical = 8.dp)
)
NavigationSettingItem(
title = stringResource(R.string.settings_top_sheet_aspect_ratios),
description = stringResource(
R.string.settings_top_sheet_aspect_ratios_summary,
topSheetAspectRatios.joinToString(" / ") { it.getDisplayName() }
),
onClick = { showAspectRatioDialog = true }
)
if (showAspectRatioDialog) {
AspectRatioDialog(
availableRatios = availablePhotoAspectRatios,
selectedRatios = topSheetAspectRatios,
customRatios = customAspectRatios,
onSelectionChange = { viewModel.setTopSheetAspectRatios(it) },
onAddCustomRatio = { width, height ->
viewModel.addCustomAspectRatio(width, height)
},
onDeleteCustomRatio = { viewModel.deleteCustomAspectRatio(it) },
onDismiss = { showAspectRatioDialog = false }
)
}
HorizontalDivider(
color = Color.White.copy(alpha = 0.1f),
modifier = Modifier.padding(vertical = 8.dp)
)
VolumeKeyActionSetting(
action = volumeKeyAction,
onActionSelected = { viewModel.setVolumeKeyAction(it) }
@@ -1778,6 +1810,316 @@ fun SettingsSection(
}
}
@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun AspectRatioDialog(
availableRatios: List<AspectRatio>,
selectedRatios: List<AspectRatio>,
customRatios: List<AspectRatio>,
onSelectionChange: (List<AspectRatio>) -> Unit,
onAddCustomRatio: (Int, Int) -> Unit,
onDeleteCustomRatio: (AspectRatio) -> Unit,
onDismiss: () -> Unit
) {
val selected = AspectRatio.sanitizeTopSheetRatios(selectedRatios)
var customWidth by remember { mutableStateOf("") }
var customHeight by remember { mutableStateOf("") }
val parsedWidth = customWidth.toIntOrNull()
val parsedHeight = customHeight.toIntOrNull()
val canAddCustomRatio = parsedWidth != null && parsedHeight != null && parsedWidth > 0 && parsedHeight > 0
AlertDialog(
onDismissRequest = onDismiss,
containerColor = Color(0xFF1A1A1A),
title = {
Text(
text = stringResource(R.string.settings_top_sheet_aspect_ratios),
color = Color.White,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
},
text = {
Column(
modifier = Modifier.verticalScroll(rememberScrollState())
) {
Text(
text = stringResource(R.string.settings_top_sheet_aspect_ratios_description),
color = Color.White.copy(alpha = 0.6f),
fontSize = 13.sp,
lineHeight = 18.sp
)
Spacer(modifier = Modifier.height(20.dp))
// Built-in Ratios
Text(
text = stringResource(R.string.built_in).uppercase(),
color = Color.White.copy(alpha = 0.4f),
fontSize = 11.sp,
fontWeight = FontWeight.Bold,
letterSpacing = 1.sp,
modifier = Modifier.padding(bottom = 12.dp)
)
FlowRow(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(10.dp),
verticalArrangement = Arrangement.spacedBy(10.dp)
) {
AspectRatio.entries.forEach { ratio ->
val isChecked = selected.any { it.name == ratio.name }
val canToggle = if (isChecked) selected.size > 1 else selected.size < AspectRatio.TOP_SHEET_MAX_COUNT
AspectRatioGridItem(
ratio = ratio,
isSelected = isChecked,
enabled = canToggle,
onClick = {
if (canToggle) {
onSelectionChange(toggleTopSheetAspectRatio(selected, ratio))
}
}
)
}
}
if (customRatios.isNotEmpty()) {
Spacer(modifier = Modifier.height(24.dp))
Text(
text = stringResource(R.string.category_custom).uppercase(),
color = Color.White.copy(alpha = 0.4f),
fontSize = 11.sp,
fontWeight = FontWeight.Bold,
letterSpacing = 1.sp,
modifier = Modifier.padding(bottom = 12.dp)
)
FlowRow(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(10.dp),
verticalArrangement = Arrangement.spacedBy(10.dp)
) {
customRatios.forEach { ratio ->
val isChecked = selected.any { it.name == ratio.name }
val canToggle = if (isChecked) selected.size > 1 else selected.size < AspectRatio.TOP_SHEET_MAX_COUNT
AspectRatioGridItem(
ratio = ratio,
isSelected = isChecked,
enabled = canToggle,
isCustom = true,
onClick = {
if (canToggle) {
onSelectionChange(toggleTopSheetAspectRatio(selected, ratio))
}
},
onDelete = { onDeleteCustomRatio(ratio) }
)
}
}
}
Spacer(modifier = Modifier.height(24.dp))
Text(
text = stringResource(R.string.settings_custom_aspect_ratio),
color = Color.White,
fontSize = 15.sp,
fontWeight = FontWeight.SemiBold
)
Spacer(modifier = Modifier.height(12.dp))
Row(
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.fillMaxWidth()
.background(Color.White.copy(alpha = 0.05f), RoundedCornerShape(12.dp))
.padding(12.dp)
) {
androidx.compose.material3.OutlinedTextField(
value = customWidth,
onValueChange = { customWidth = it.filter(Char::isDigit).take(3) },
modifier = Modifier.weight(1f),
placeholder = { Text(stringResource(R.string.settings_custom_aspect_ratio_width), fontSize = 12.sp) },
singleLine = true,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
colors = androidx.compose.material3.OutlinedTextFieldDefaults.colors(
focusedTextColor = Color.White,
unfocusedTextColor = Color.White,
focusedContainerColor = Color.Transparent,
unfocusedContainerColor = Color.Transparent,
focusedBorderColor = Color(0xFFFF6B35),
unfocusedBorderColor = Color.White.copy(alpha = 0.2f),
cursorColor = Color(0xFFFF6B35)
)
)
Text(
text = ":",
color = Color.White.copy(alpha = 0.7f),
fontSize = 18.sp,
fontWeight = FontWeight.Bold
)
androidx.compose.material3.OutlinedTextField(
value = customHeight,
onValueChange = { customHeight = it.filter(Char::isDigit).take(3) },
modifier = Modifier.weight(1f),
placeholder = { Text(stringResource(R.string.settings_custom_aspect_ratio_height), fontSize = 12.sp) },
singleLine = true,
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
colors = androidx.compose.material3.OutlinedTextFieldDefaults.colors(
focusedTextColor = Color.White,
unfocusedTextColor = Color.White,
focusedContainerColor = Color.Transparent,
unfocusedContainerColor = Color.Transparent,
focusedBorderColor = Color(0xFFFF6B35),
unfocusedBorderColor = Color.White.copy(alpha = 0.2f),
cursorColor = Color(0xFFFF6B35)
)
)
IconButton(
enabled = canAddCustomRatio,
onClick = {
onAddCustomRatio(parsedWidth ?: 1, parsedHeight ?: 1)
customWidth = ""
customHeight = ""
},
modifier = Modifier
.size(48.dp)
.background(
if (canAddCustomRatio) Color(0xFFFF6B35) else Color.White.copy(alpha = 0.1f),
RoundedCornerShape(12.dp)
)
) {
Icon(
imageVector = Icons.Default.Add,
contentDescription = stringResource(R.string.settings_custom_aspect_ratio),
tint = if (canAddCustomRatio) Color.White else Color.White.copy(alpha = 0.3f)
)
}
}
}
},
confirmButton = {
TextButton(onClick = onDismiss) {
Text(
text = stringResource(R.string.confirm),
color = Color(0xFFFF6B35),
fontWeight = FontWeight.Bold
)
}
}
)
}
@Composable
private fun AspectRatioGridItem(
ratio: AspectRatio,
isSelected: Boolean,
enabled: Boolean,
isCustom: Boolean = false,
onClick: () -> Unit,
onDelete: (() -> Unit)? = null
) {
Box(
modifier = Modifier
.width(72.dp)
.clip(RoundedCornerShape(12.dp))
.background(
if (isSelected) Color(0xFFFF6B35).copy(alpha = 0.15f)
else Color.White.copy(alpha = 0.05f)
)
.border(
1.dp,
if (isSelected) Color(0xFFFF6B35) else Color.White.copy(alpha = 0.1f),
RoundedCornerShape(12.dp)
)
.clickable(enabled = enabled) { onClick() }
.padding(vertical = 10.dp, horizontal = 4.dp),
contentAlignment = Alignment.Center
) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(6.dp)
) {
// Visual Shape Preview
Box(
modifier = Modifier
.size(32.dp, 24.dp),
contentAlignment = Alignment.Center
) {
val w = ratio.widthRatio.toFloat()
val h = ratio.heightRatio.toFloat()
val maxWidth = 28.dp
val maxHeight = 20.dp
val displayW: androidx.compose.ui.unit.Dp
val displayH: androidx.compose.ui.unit.Dp
if (w / h > maxWidth / maxHeight) {
displayW = maxWidth
displayH = maxWidth * (h / w)
} else {
displayH = maxHeight
displayW = maxHeight * (w / h)
}
Box(
modifier = Modifier
.size(displayW, displayH)
.background(
if (isSelected) Color(0xFFFF6B35) else Color.White.copy(alpha = 0.3f),
RoundedCornerShape(2.dp)
)
)
}
Text(
text = ratio.getDisplayName(),
color = if (isSelected) Color(0xFFFF6B35) else if (enabled) Color.White else Color.White.copy(alpha = 0.3f),
fontSize = 11.sp,
fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Medium,
textAlign = TextAlign.Center
)
}
if (isCustom && onDelete != null) {
Box(
modifier = Modifier
.align(Alignment.TopEnd)
.padding(2.dp)
.size(16.dp)
.clip(CircleShape)
.background(Color.Black.copy(alpha = 0.3f))
.clickable { onDelete() },
contentAlignment = Alignment.Center
) {
Icon(
imageVector = Icons.Default.Close,
contentDescription = null,
tint = Color.White.copy(alpha = 0.7f),
modifier = Modifier.size(10.dp)
)
}
}
}
}
private fun toggleTopSheetAspectRatio(
selectedRatios: List<AspectRatio>,
ratio: AspectRatio
): List<AspectRatio> {
val updated = if (selectedRatios.any { it.name == ratio.name }) {
selectedRatios.filterNot { it.name == ratio.name }
} else {
selectedRatios + ratio
}
return AspectRatio.sanitizeTopSheetRatios(
updated
)
}
/**
* 开关设置项
*/
@@ -209,6 +209,15 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
userPreferencesRepository.userPreferences.map { it.volumeKeyAction }
.stateIn(viewModelScope, SharingStarted.Eagerly, initialValue = VolumeKeyAction.NONE)
val autoSaveAfterCapture: Flow<Boolean> = userPreferencesRepository.userPreferences.map { it.autoSaveAfterCapture }
val topSheetAspectRatios: StateFlow<List<AspectRatio>> = userPreferencesRepository.userPreferences
.map { it.topSheetAspectRatios }
.stateIn(viewModelScope, SharingStarted.Eagerly, AspectRatio.defaultTopSheetRatios)
val customAspectRatios: StateFlow<List<AspectRatio>> = userPreferencesRepository.userPreferences
.map { it.customAspectRatios }
.stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())
val availablePhotoAspectRatios: StateFlow<List<AspectRatio>> = userPreferencesRepository.userPreferences
.map { AspectRatio.entries + it.customAspectRatios }
.stateIn(viewModelScope, SharingStarted.Eagerly, AspectRatio.entries)
val nrLevel: StateFlow<Int> = userPreferencesRepository.userPreferences
.map { it.nrLevel }
.stateIn(viewModelScope, SharingStarted.Eagerly, 5)
@@ -1414,6 +1423,38 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
}
}
fun setTopSheetAspectRatios(ratios: List<AspectRatio>) {
val sanitizedRatios = AspectRatio.sanitizeTopSheetRatios(ratios)
if (state.value.aspectRatio !in sanitizedRatios) {
setAspectRatio(sanitizedRatios.first())
}
viewModelScope.launch {
userPreferencesRepository.saveTopSheetAspectRatios(sanitizedRatios)
}
}
fun addCustomAspectRatio(widthRatio: Int, heightRatio: Int) {
val ratio = AspectRatio.custom(widthRatio, heightRatio)
val customRatios = AspectRatio.sanitizeCustomRatios(customAspectRatios.value + ratio)
viewModelScope.launch {
userPreferencesRepository.saveCustomAspectRatios(customRatios)
val selectedRatios = AspectRatio.sanitizeTopSheetRatios(topSheetAspectRatios.value + ratio)
userPreferencesRepository.saveTopSheetAspectRatios(selectedRatios)
}
}
fun deleteCustomAspectRatio(ratio: AspectRatio) {
val customRatios = customAspectRatios.value.filterNot { it.name == ratio.name }
val selectedRatios = topSheetAspectRatios.value.filterNot { it.name == ratio.name }
if (state.value.aspectRatio.name == ratio.name) {
setAspectRatio(AspectRatio.RATIO_4_3)
}
viewModelScope.launch {
userPreferencesRepository.saveCustomAspectRatios(customRatios)
userPreferencesRepository.saveTopSheetAspectRatios(selectedRatios)
}
}
fun setCaptureMode(mode: CaptureMode) {
if (state.value.videoRecordingState.isRecording && mode != state.value.captureMode) return
val shouldDisableVideoLog = mode == CaptureMode.PHOTO &&
@@ -36,6 +36,12 @@
<!-- Aspect Ratio -->
<string name="aspect_ratio">画面比例</string>
<string name="settings_top_sheet_aspect_ratios">画面比例</string>
<string name="settings_top_sheet_aspect_ratios_description">选择最多 5 个在相机顶部面板中显示的画面比例。</string>
<string name="settings_top_sheet_aspect_ratios_summary">当前显示:%1$s</string>
<string name="settings_custom_aspect_ratio">自定义画面比例</string>
<string name="settings_custom_aspect_ratio_width">宽</string>
<string name="settings_custom_aspect_ratio_height">高</string>
<string name="ratio_3_2">3:2</string>
<string name="ratio_4_3">4:3</string>
<string name="ratio_16_9">16:9</string>
+6
View File
@@ -36,6 +36,12 @@
<!-- Aspect Ratio -->
<string name="aspect_ratio">Aspect Ratio</string>
<string name="settings_top_sheet_aspect_ratios">Aspect Ratios</string>
<string name="settings_top_sheet_aspect_ratios_description">Choose up to 5 aspect ratios to show in the camera top sheet.</string>
<string name="settings_top_sheet_aspect_ratios_summary">Shown: %1$s</string>
<string name="settings_custom_aspect_ratio">Custom Aspect Ratio</string>
<string name="settings_custom_aspect_ratio_width">Width</string>
<string name="settings_custom_aspect_ratio_height">Height</string>
<string name="ratio_3_2">3:2</string>
<string name="ratio_4_3">4:3</string>
<string name="ratio_16_9">16:9</string>