diff --git a/app/src/main/java/com/hinnka/mycamera/data/UserPreferencesRepository.kt b/app/src/main/java/com/hinnka/mycamera/data/UserPreferencesRepository.kt index 976b807f..db968ee8 100644 --- a/app/src/main/java/com/hinnka/mycamera/data/UserPreferencesRepository.kt +++ b/app/src/main/java/com/hinnka/mycamera/data/UserPreferencesRepository.kt @@ -68,6 +68,7 @@ data class UserPreferences( val useSuperResolution: Boolean = false, // 是否使用超分辨率 val photoQuality: Int = 95, // 照片质量: 90, 95, 100 val useLivePhoto: Boolean = false, // 是否启用 Live Photo (Motion Photo) + val enableDevelopAnimation: Boolean = true, // 是否启用拍摄后的显影动画 val backgroundImage: String = "camera_bg", // 背景图资源名或文件路径 val useGpuAcceleration: Boolean = DeviceUtil.defaultGpuAcceleration, // 多帧合成是否使用 GPU 加速 val droMode: String = "OFF", // DRO 模式 @@ -139,6 +140,7 @@ class UserPreferencesRepository(private val context: Context) { private val USE_SUPER_RESOLUTION = booleanPreferencesKey("use_super_resolution") private val PHOTO_QUALITY = intPreferencesKey("photo_quality") private val USE_LIVE_PHOTO = booleanPreferencesKey("use_live_photo") + private val ENABLE_DEVELOP_ANIMATION = booleanPreferencesKey("enable_develop_animation") private val BACKGROUND_IMAGE = stringPreferencesKey("background_image") private val USE_GPU_ACCELERATION = booleanPreferencesKey("use_gpu_acceleration") private val DRO_MODE = stringPreferencesKey("dro_mode") @@ -202,6 +204,7 @@ class UserPreferencesRepository(private val context: Context) { useSuperResolution = preferences[USE_SUPER_RESOLUTION] ?: false, photoQuality = preferences[PHOTO_QUALITY] ?: 95, useLivePhoto = preferences[USE_LIVE_PHOTO] ?: false, + enableDevelopAnimation = preferences[ENABLE_DEVELOP_ANIMATION] ?: true, backgroundImage = preferences[BACKGROUND_IMAGE] ?: "camera_bg", useGpuAcceleration = preferences[USE_GPU_ACCELERATION] ?: DeviceUtil.defaultGpuAcceleration, droMode = preferences[DRO_MODE] ?: "OFF", @@ -555,6 +558,15 @@ class UserPreferencesRepository(private val context: Context) { } } + /** + * 保存是否启用显影动画 + */ + suspend fun saveEnableDevelopAnimation(enabled: Boolean) { + context.dataStore.edit { preferences -> + preferences[ENABLE_DEVELOP_ANIMATION] = enabled + } + } + /** * 保存背景图 */ diff --git a/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraScreen.kt b/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraScreen.kt index eac8c8bb..a61a7e1e 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraScreen.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraScreen.kt @@ -4,6 +4,7 @@ import android.app.Activity import android.content.Context import android.content.ContextWrapper import android.content.Intent +import android.graphics.Bitmap import android.os.Environment import android.provider.Settings import androidx.activity.compose.rememberLauncherForActivityResult @@ -54,7 +55,9 @@ import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.drawscope.Stroke import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.layout.boundsInRoot import androidx.compose.ui.layout.layout +import androidx.compose.ui.layout.onGloballyPositioned import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalWindowInfo @@ -109,9 +112,14 @@ fun CameraScreen( val useMultipleExposure by viewModel.useMultipleExposure.collectAsState() val useSuperResolution by viewModel.useSuperResolution.collectAsState() val useLivePhoto by viewModel.useLivePhoto.collectAsState() + val enableDevelopAnimation by viewModel.enableDevelopAnimation.collectAsState() val phantomMode by viewModel.phantomMode.collectAsState() val multipleExposureState = viewModel.multipleExposureState var previewRecipeParamsOverride by remember(currentLutId) { mutableStateOf(null) } + var pendingCaptureAnimationBitmap by remember { mutableStateOf(null) } + var previewBounds by remember { mutableStateOf(null) } + var galleryThumbnailBounds by remember { mutableStateOf(null) } + var captureAnimationSnapshot by remember { mutableStateOf(null) } // 标记相机是否已打开 var cameraOpened by remember { mutableStateOf(false) } @@ -180,6 +188,29 @@ fun CameraScreen( LaunchedEffect(Unit) { viewModel.imageSavedEvent.collect { galleryViewModel.refreshLatestPhoto() + if (!enableDevelopAnimation) { + pendingCaptureAnimationBitmap = null + captureAnimationSnapshot = null + MyCameraApplication.updateWidgets(context) + return@collect + } + val sourceBounds = previewBounds + val targetBounds = galleryThumbnailBounds + + fun startCaptureAnimation(bitmap: Bitmap) { + if (sourceBounds == null || targetBounds == null) { + return + } + captureAnimationSnapshot = CaptureAnimationSnapshot( + bitmap = bitmap.asImageBitmap(), + sourceBounds = sourceBounds, + targetBounds = targetBounds + ) + } + + pendingCaptureAnimationBitmap?.let(::startCaptureAnimation) + ?: viewModel.glSurfaceView?.capturePreviewFrame(::startCaptureAnimation) + pendingCaptureAnimationBitmap = null MyCameraApplication.updateWidgets(context) } } @@ -348,6 +379,9 @@ fun CameraScreen( .padding(4.dp) .weight(1f) .background(Color.Black) + .onGloballyPositioned { coordinates -> + previewBounds = coordinates.boundsInRoot() + } .pointerInput(Unit) { var totalDrag = 0f detectHorizontalDragGestures( @@ -656,6 +690,17 @@ fun CameraScreen( latestPhoto = latestPhoto, useMultipleExposure = useMultipleExposure, multipleExposureState = multipleExposureState, + onGalleryThumbnailBoundsChanged = { bounds -> + galleryThumbnailBounds = bounds + }, + onCaptureTap = { + if (enableDevelopAnimation) { + viewModel.glSurfaceView?.capturePreviewFrame { bitmap -> + pendingCaptureAnimationBitmap = bitmap + } + } + viewModel.capture() + }, onGalleryClick = { galleryViewModel.loadPhotos() onGalleryClick() @@ -686,6 +731,18 @@ fun CameraScreen( ) } + captureAnimationSnapshot?.let { snapshot -> + CaptureAnimationOverlay( + snapshot = snapshot, + modifier = Modifier.fillMaxSize(), + onFinished = { + if (captureAnimationSnapshot?.id == snapshot.id) { + captureAnimationSnapshot = null + } + } + ) + } + // TopSheet for settings CameraTopSheet( visible = activePanel == ActivePanel.SETTINGS, @@ -871,6 +928,8 @@ fun Controls( latestPhoto: com.hinnka.mycamera.gallery.PhotoData?, useMultipleExposure: Boolean, multipleExposureState: com.hinnka.mycamera.viewmodel.MultipleExposureSessionState, + onGalleryThumbnailBoundsChanged: (Rect) -> Unit, + onCaptureTap: () -> Unit, onGalleryClick: () -> Unit ) { BoxWithConstraints( @@ -889,6 +948,9 @@ fun Controls( modifier = Modifier .align(Alignment.CenterStart) .padding(start = 32.dp) + .onGloballyPositioned { coordinates -> + onGalleryThumbnailBoundsChanged(coordinates.boundsInRoot()) + } .autoRotate() ) { GalleryThumbnail( @@ -905,7 +967,7 @@ fun Controls( multipleExposureEnabled = useMultipleExposure, multipleExposureProgress = multipleExposureState.capturedCount.toFloat() / multipleExposureState.targetCount.coerceAtLeast(1).toFloat(), - onTap = { viewModel.capture() }, + onTap = onCaptureTap, onLongPressStart = { viewModel.startContinuousCapture() }, onLongPressEnd = { viewModel.stopContinuousCapture() } ) diff --git a/app/src/main/java/com/hinnka/mycamera/ui/camera/CaptureAnimationOverlay.kt b/app/src/main/java/com/hinnka/mycamera/ui/camera/CaptureAnimationOverlay.kt new file mode 100644 index 00000000..7d6998d5 --- /dev/null +++ b/app/src/main/java/com/hinnka/mycamera/ui/camera/CaptureAnimationOverlay.kt @@ -0,0 +1,302 @@ +package com.hinnka.mycamera.ui.camera + +import androidx.compose.animation.core.Animatable +import androidx.compose.animation.core.FastOutSlowInEasing +import androidx.compose.animation.core.LinearEasing +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.layout.Box +import androidx.compose.foundation.layout.BoxWithConstraints +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.offset +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.alpha +import androidx.compose.ui.draw.clip +import androidx.compose.ui.geometry.Rect +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.ColorFilter +import androidx.compose.ui.graphics.ColorMatrix +import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.unit.IntOffset +import androidx.compose.ui.unit.dp +import kotlin.math.min +import kotlin.math.roundToInt + +data class CaptureAnimationSnapshot( + val bitmap: ImageBitmap, + val sourceBounds: Rect, + val targetBounds: Rect, + val id: Long = System.nanoTime() +) + +private enum class CaptureAnimationPhase { + EXPAND, + DEVELOP, + COLLAPSE +} + +@Composable +fun CaptureAnimationOverlay( + snapshot: CaptureAnimationSnapshot, + modifier: Modifier = Modifier, + onFinished: () -> Unit +) { + BoxWithConstraints(modifier = modifier) { + val density = LocalDensity.current + val rootWidth = constraints.maxWidth.toFloat() + val rootHeight = constraints.maxHeight.toFloat() + val imageAspectRatio = remember(snapshot.bitmap) { + snapshot.bitmap.width.toFloat() / snapshot.bitmap.height.coerceAtLeast(1).toFloat() + } + val centerBounds = remember(snapshot, rootWidth, rootHeight) { + calculateCenterBounds( + rootWidth = rootWidth, + rootHeight = rootHeight, + aspectRatio = imageAspectRatio + ) + } + + val expandProgress = remember(snapshot.id) { Animatable(0f) } + val developProgress = remember(snapshot.id) { Animatable(0f) } + val collapseProgress = remember(snapshot.id) { Animatable(0f) } + var phase by remember(snapshot.id) { mutableStateOf(CaptureAnimationPhase.EXPAND) } + + LaunchedEffect(snapshot.id) { + phase = CaptureAnimationPhase.EXPAND + expandProgress.snapTo(0f) + developProgress.snapTo(0f) + collapseProgress.snapTo(0f) + + expandProgress.animateTo( + targetValue = 1f, + animationSpec = tween(durationMillis = 500, easing = FastOutSlowInEasing) + ) + + phase = CaptureAnimationPhase.DEVELOP + developProgress.animateTo( + targetValue = 1f, + animationSpec = tween(durationMillis = 4000, easing = LinearEasing) + ) + + phase = CaptureAnimationPhase.COLLAPSE + collapseProgress.animateTo( + targetValue = 1f, + animationSpec = tween(durationMillis = 500, easing = FastOutSlowInEasing) + ) + + onFinished() + } + + val currentBounds = when (phase) { + CaptureAnimationPhase.EXPAND -> lerpRect( + start = snapshot.sourceBounds, + stop = centerBounds, + fraction = expandProgress.value + ) + + CaptureAnimationPhase.DEVELOP -> centerBounds + + CaptureAnimationPhase.COLLAPSE -> lerpRect( + start = centerBounds, + stop = snapshot.targetBounds, + fraction = collapseProgress.value + ) + } + + val scrimAlpha = when (phase) { + CaptureAnimationPhase.EXPAND -> 0.62f * expandProgress.value + CaptureAnimationPhase.DEVELOP -> 0.62f + CaptureAnimationPhase.COLLAPSE -> 0.62f * (1f - collapseProgress.value) + } + + val look = when (phase) { + CaptureAnimationPhase.EXPAND -> CaptureDevelopLook( + inversion = expandProgress.value * 0.35f, + contrast = lerp(0.92f, 0.82f, expandProgress.value), + brightness = lerp(0f, -26f, expandProgress.value), + saturation = lerp(1f, 0.18f, expandProgress.value), + warmth = lerp(0f, 0.12f, expandProgress.value) + ) + + CaptureAnimationPhase.DEVELOP -> calculateDevelopLook(developProgress.value) + + CaptureAnimationPhase.COLLAPSE -> CaptureDevelopLook( + inversion = 0f, + contrast = 1f, + brightness = 0f, + saturation = 1f, + warmth = 0f + ) + } + + val colorFilter = remember(look) { + ColorFilter.colorMatrix(createDevelopColorMatrix(look)) + } + + val imageAlpha = when (phase) { + CaptureAnimationPhase.EXPAND -> 0f + CaptureAnimationPhase.DEVELOP -> calculateDevelopImageAlpha(developProgress.value) + CaptureAnimationPhase.COLLAPSE -> 1f + } + + Box( + modifier = Modifier + .fillMaxSize() + .background(Color.Black.copy(alpha = scrimAlpha)) + ) + + Box( + modifier = Modifier + .offset { + IntOffset( + x = currentBounds.left.roundToInt(), + y = currentBounds.top.roundToInt() + ) + } + .size( + width = with(density) { currentBounds.width.toDp() }, + height = with(density) { currentBounds.height.toDp() } + ) + .clip(RoundedCornerShape(16.dp)) + .background(Color(0xFF020202).copy(alpha = 0.9f)) + .border( + width = 1.dp, + color = Color.White.copy(alpha = 0.3f), + shape = RoundedCornerShape(16.dp) + ) + ) { + Image( + bitmap = snapshot.bitmap, + contentDescription = null, + contentScale = ContentScale.Crop, + colorFilter = colorFilter, + modifier = Modifier + .fillMaxSize() + .alpha(imageAlpha) + ) + } + } +} + +private fun calculateCenterBounds( + rootWidth: Float, + rootHeight: Float, + aspectRatio: Float +): Rect { + val maxWidth = rootWidth * 0.84f + val maxHeight = rootHeight * 0.42f + val targetWidth = min(maxWidth, maxHeight * aspectRatio) + val targetHeight = targetWidth / aspectRatio + val left = (rootWidth - targetWidth) / 2f + val top = (rootHeight - targetHeight) / 2f + return Rect( + left = left, + top = top, + right = left + targetWidth, + bottom = top + targetHeight + ) +} + +private fun lerpRect(start: Rect, stop: Rect, fraction: Float): Rect { + val t = fraction.coerceIn(0f, 1f) + return Rect( + left = lerp(start.left, stop.left, t), + top = lerp(start.top, stop.top, t), + right = lerp(start.right, stop.right, t), + bottom = lerp(start.bottom, stop.bottom, t) + ) +} + +private fun lerp(start: Float, stop: Float, fraction: Float): Float { + return start + (stop - start) * fraction +} + +private data class CaptureDevelopLook( + val inversion: Float, + val contrast: Float, + val brightness: Float, + val saturation: Float, + val warmth: Float +) + +private fun calculateDevelopLook(progress: Float): CaptureDevelopLook { + val t = progress.coerceIn(0f, 1f) + return when { + t < 0.5f -> { + val stage = t / 0.5f + CaptureDevelopLook( + inversion = lerp(0.98f, 0.9f, stage), + contrast = lerp(0.5f, 0.6f, stage), + brightness = lerp(-76f, -56f, stage), + saturation = lerp(0.02f, 0.05f, stage), + warmth = lerp(0.08f, 0.18f, stage) + ) + } + + t < 0.6f -> { + val stage = (t - 0.6f) / 0.1f + CaptureDevelopLook( + inversion = lerp(0.9f, 0.7f, stage), + contrast = lerp(0.6f, 0.65f, stage), + brightness = lerp(-56f, -40f, stage), + saturation = lerp(0.05f, 0.1f, stage), + warmth = lerp(0.18f, 0.14f, stage) + ) + } + + else -> { + val stage = (t - 0.6f) / 0.4f + CaptureDevelopLook( + inversion = lerp(0.7f, 0f, stage), + contrast = lerp(0.65f, 1f, stage), + brightness = lerp(-40f, 0f, stage), + saturation = lerp(0.1f, 1f, stage), + warmth = lerp(0.14f, 0f, stage) + ) + } + } +} + +private fun calculateDevelopImageAlpha(progress: Float): Float { + val t = progress.coerceIn(0f, 1f) + return when { + t < 0.5f -> lerp(0f, 1f, t / 0.5f) + else -> 1f + } +} + +private fun createDevelopColorMatrix(look: CaptureDevelopLook): ColorMatrix { + val t = look.inversion.coerceIn(0f, 1f) + val diagonal = 1f - (2f * t) + val baseOffset = 255f * t + val redOffset = baseOffset + look.brightness + (24f * look.warmth) + val greenOffset = baseOffset + look.brightness + (8f * look.warmth) + val blueOffset = baseOffset + look.brightness - (18f * look.warmth) + val sat = look.saturation.coerceIn(0f, 2f) + val invSat = 1f - sat + val rw = 0.213f * invSat + val gw = 0.715f * invSat + val bw = 0.072f * invSat + val contrast = look.contrast + + return ColorMatrix( + floatArrayOf( + contrast * (rw + sat) * diagonal, contrast * gw * diagonal, contrast * bw * diagonal, 0f, redOffset, + contrast * rw * diagonal, contrast * (gw + sat) * diagonal, contrast * bw * diagonal, 0f, greenOffset, + contrast * rw * diagonal, contrast * gw * diagonal, contrast * (bw + sat) * diagonal, 0f, blueOffset, + 0f, 0f, 0f, 1f, 0f + ) + ) +} 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 ac976329..06e94e13 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 @@ -135,6 +135,7 @@ fun SettingsScreen( val useMultipleExposure by viewModel.useMultipleExposure.collectAsState() val multipleExposureCount by viewModel.multipleExposureCount.collectAsState() val useLivePhoto by viewModel.useLivePhoto.collectAsState() + val enableDevelopAnimation by viewModel.enableDevelopAnimation.collectAsState() val photoQuality by viewModel.photoQuality.collectAsState(initial = 95) val useGpuAcceleration by viewModel.useGpuAcceleration.collectAsState() val rawProfile by viewModel.rawProfile.collectAsState() @@ -769,6 +770,18 @@ fun SettingsScreen( onCheckedChange = { viewModel.setUseLivePhoto(it) } ) + HorizontalDivider( + color = Color.White.copy(alpha = 0.1f), + modifier = Modifier.padding(vertical = 8.dp) + ) + + SwitchSettingItem( + title = stringResource(R.string.settings_develop_animation), + description = stringResource(R.string.settings_develop_animation_description), + checked = enableDevelopAnimation, + onCheckedChange = { viewModel.setEnableDevelopAnimation(it) } + ) + HorizontalDivider( color = Color.White.copy(alpha = 0.1f), modifier = Modifier.padding(vertical = 12.dp) diff --git a/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt b/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt index e2aff999..b6e00f58 100644 --- a/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt +++ b/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt @@ -183,6 +183,9 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) val useLivePhoto: StateFlow = userPreferencesRepository.userPreferences .map { it.useLivePhoto } .stateIn(viewModelScope, SharingStarted.Eagerly, false) + val enableDevelopAnimation: StateFlow = userPreferencesRepository.userPreferences + .map { it.enableDevelopAnimation } + .stateIn(viewModelScope, SharingStarted.Eagerly, true) val backgroundImage: StateFlow = userPreferencesRepository.userPreferences .map { it.backgroundImage } .stateIn(viewModelScope, SharingStarted.Eagerly, "camera_bg") @@ -1475,6 +1478,12 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) } } + fun setEnableDevelopAnimation(enabled: Boolean) { + viewModelScope.launch { + userPreferencesRepository.saveEnableDevelopAnimation(enabled) + } + } + /** * 切换延时拍摄档位(0s → 3s → 5s → 10s → 0s) */ diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index a94057b1..6105eb5d 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -137,6 +137,8 @@ 自研 动态照片 (Live Photo) 在拍照时同时录制一段短视频,让照片动起来。 + 显影动画 + 拍摄后显示胶片显影动画效果。 GPU 加速 使用 GPU (Vulkan) 加速多帧合成,建议开启。 边框水印 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a547b728..2d0da85b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -139,6 +139,8 @@ Self-developed Live Photo Capture a short video clip along with your photo for a dynamic live photo effect. + Develop Animation + Show the film-like developing animation after capture. GPU Acceleration Use GPU (Vulkan) to speed up multi-frame stacking. Default is on. Frame Watermark