feat: custom bg

This commit is contained in:
Hinnka committed 2026-02-01 10:34:37 +08:00
1 parent 947b533d01
commit 271d41123e
12 files changed
+295 -5

No files matched your search

@@ -61,7 +61,8 @@ data class UserPreferences(
val useSuperResolution: Boolean = false, // 是否使用超分辨率
val rawEngine: RawEngine = RawEngine.NATIVE, // RAW处理引擎
val photoQuality: Int = 95, // 照片质量: 90, 95, 100
val useLivePhoto: Boolean = false // 是否启用 Live Photo (Motion Photo)
val useLivePhoto: Boolean = false, // 是否启用 Live Photo (Motion Photo)
val backgroundImage: String = "camera_bg" // 背景图资源名或文件路径
)
/**
@@ -109,6 +110,7 @@ class UserPreferencesRepository(private val context: Context) {
private val RAW_ENGINE = stringPreferencesKey("raw_engine")
private val PHOTO_QUALITY = intPreferencesKey("photo_quality")
private val USE_LIVE_PHOTO = booleanPreferencesKey("use_live_photo")
private val BACKGROUND_IMAGE = stringPreferencesKey("background_image")
}
/**
@@ -150,7 +152,8 @@ class UserPreferencesRepository(private val context: Context) {
preferences[RAW_ENGINE] ?: RawEngine.NATIVE.name
),
photoQuality = preferences[PHOTO_QUALITY] ?: 95,
useLivePhoto = preferences[USE_LIVE_PHOTO] ?: false
useLivePhoto = preferences[USE_LIVE_PHOTO] ?: false,
backgroundImage = preferences[BACKGROUND_IMAGE] ?: "camera_bg"
)
}
@@ -457,4 +460,13 @@ class UserPreferencesRepository(private val context: Context) {
preferences[USE_LIVE_PHOTO] = enabled
}
}
/**
* 保存背景图
*/
suspend fun saveBackgroundImage(image: String) {
context.dataStore.edit { preferences ->
preferences[BACKGROUND_IMAGE] = image
}
}
}
@@ -44,6 +44,7 @@ import com.hinnka.mycamera.ui.components.GalleryThumbnail
import com.hinnka.mycamera.ui.components.HistogramView
import com.hinnka.mycamera.ui.components.LutControlPanel
import com.hinnka.mycamera.ui.components.PaymentDialog
import com.hinnka.mycamera.ui.components.rememberBackgroundPainter
import com.hinnka.mycamera.utils.OrientationObserver
import com.hinnka.mycamera.viewmodel.CameraViewModel
import com.hinnka.mycamera.viewmodel.GalleryViewModel
@@ -136,10 +137,11 @@ fun CameraScreen(
}
Box(modifier = modifier.fillMaxSize()) {
val backgroundPainter = rememberBackgroundPainter(viewModel)
Column(
modifier = Modifier
.fillMaxSize()
.paint(painterResource(R.drawable.camera_bg), contentScale = ContentScale.Crop)
.paint(backgroundPainter, contentScale = ContentScale.Crop)
) {
// 顶部控制条
CameraTopBar(
@@ -0,0 +1,55 @@
package com.hinnka.mycamera.ui.components
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import com.hinnka.mycamera.R
import com.hinnka.mycamera.viewmodel.CameraViewModel
@Composable
fun rememberBackgroundPainter(viewModel: CameraViewModel): Painter {
val bgImage by viewModel.backgroundImage.collectAsState()
val context = LocalContext.current
return remember(bgImage) {
if (bgImage.startsWith("/")) {
try {
val bitmap = android.graphics.BitmapFactory.decodeFile(bgImage)
if (bitmap != null) {
BitmapPainter(bitmap.asImageBitmap())
} else {
null
}
} catch (e: Exception) {
null
}
} else {
val resId = context.resources.getIdentifier(bgImage, "drawable", context.packageName)
if (resId != 0) {
// We can't use painterResource inside remember like this if it's not a composable call
// But we can get the resource ID and use it outside.
null // Will handle below
} else {
null
}
}
} ?: run {
// Fallback or Resource handling
if (!bgImage.startsWith("/")) {
val resId = context.resources.getIdentifier(bgImage, "drawable", context.packageName)
if (resId != 0) {
painterResource(resId)
} else {
painterResource(R.drawable.camera_bg)
}
} else {
painterResource(R.drawable.camera_bg)
}
}
}
@@ -26,12 +26,15 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.paint
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
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.painterResource
import androidx.compose.ui.res.stringResource
import android.content.Intent
import android.net.Uri
@@ -40,6 +43,7 @@ import com.hinnka.mycamera.frame.FrameInfo
import com.hinnka.mycamera.ui.camera.autoRotate
import com.hinnka.mycamera.ui.components.SliderSettingItem
import com.hinnka.mycamera.ui.components.LogViewerDialog
import com.hinnka.mycamera.ui.components.rememberBackgroundPainter
import com.hinnka.mycamera.data.VolumeKeyAction
import com.hinnka.mycamera.viewmodel.CameraViewModel
import androidx.compose.foundation.layout.ExperimentalLayoutApi
@@ -90,10 +94,11 @@ fun SettingsScreen(
val backgroundColor = Color(0xFF151515)
val backgroundPainter = rememberBackgroundPainter(viewModel)
Column(
modifier = modifier
.fillMaxSize()
.background(backgroundColor)
.paint(backgroundPainter, contentScale = ContentScale.Crop)
) {
// 顶部标题栏
TopAppBar(
@@ -118,7 +123,7 @@ fun SettingsScreen(
}
},
colors = TopAppBarDefaults.topAppBarColors(
containerColor = backgroundColor
containerColor = Color.Transparent
)
)
@@ -159,6 +164,17 @@ fun SettingsScreen(
// 内容管理设置
SettingsSection(title = stringResource(R.string.settings_section_management)) {
// 背景设置
BackgroundSetting(
viewModel = viewModel,
modifier = Modifier.padding(bottom = 16.dp)
)
HorizontalDivider(
color = Color.White.copy(alpha = 0.1f),
modifier = Modifier.padding(vertical = 8.dp)
)
NavigationSettingItem(
title = stringResource(R.string.settings_filter_management),
description = stringResource(R.string.settings_filter_management_description),
@@ -1133,3 +1149,159 @@ fun DefaultFocalLengthSetting(
}
}
}
/**
* 背景设置
*/
@Composable
fun BackgroundSetting(
viewModel: CameraViewModel,
modifier: Modifier = Modifier
) {
val currentBg by viewModel.backgroundImage.collectAsState()
val context = androidx.compose.ui.platform.LocalContext.current
val launcher = androidx.activity.compose.rememberLauncherForActivityResult(
contract = androidx.activity.result.contract.ActivityResultContracts.GetContent()
) { uri: Uri? ->
uri?.let { viewModel.saveCustomBackgroundImage(it) }
}
val bgList = listOf("camera_bg", "camera_bg2", "camera_bg3", "camera_bg4")
Column(modifier = modifier.fillMaxWidth()) {
Text(
text = stringResource(R.string.settings_background),
color = Color.White,
fontSize = 16.sp,
fontWeight = FontWeight.Normal,
modifier = Modifier.padding(bottom = 12.dp)
)
LazyRow(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(12.dp)
) {
items(bgList) { bgName ->
BackgroundItem(
name = bgName,
isSelected = currentBg == bgName,
onClick = { viewModel.setBackgroundImage(bgName) }
)
}
item {
CustomBackgroundItem(
isSelected = currentBg.startsWith("/"),
onClick = { launcher.launch("image/*") }
)
}
}
Spacer(modifier = Modifier.height(12.dp))
Text(
text = stringResource(R.string.settings_background_description),
color = Color.White.copy(alpha = 0.6f),
fontSize = 13.sp
)
}
}
@Composable
private fun BackgroundItem(
name: String,
isSelected: Boolean,
onClick: () -> Unit
) {
val context = androidx.compose.ui.platform.LocalContext.current
val resId = context.resources.getIdentifier(name, "drawable", context.packageName)
Box(
modifier = Modifier
.size(80.dp, 80.dp)
.clip(RoundedCornerShape(8.dp))
.border(
width = 2.dp,
color = if (isSelected) Color(0xFFFF6B35) else Color.Transparent,
shape = RoundedCornerShape(8.dp)
)
.clickable(onClick = onClick)
) {
if (resId != 0) {
Image(
painter = painterResource(resId),
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier.fillMaxSize()
)
}
if (isSelected) {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Black.copy(alpha = 0.3f)),
contentAlignment = Alignment.Center
) {
Icon(
imageVector = Icons.Default.Check,
contentDescription = null,
tint = Color.White,
modifier = Modifier.size(24.dp)
)
}
}
}
}
@Composable
private fun CustomBackgroundItem(
isSelected: Boolean,
onClick: () -> Unit
) {
Box(
modifier = Modifier
.size(80.dp, 80.dp)
.clip(RoundedCornerShape(8.dp))
.background(Color.White.copy(alpha = 0.1f))
.border(
width = 2.dp,
color = if (isSelected) Color(0xFFFF6B35) else Color.White.copy(alpha = 0.2f),
shape = RoundedCornerShape(8.dp)
)
.clickable(onClick = onClick),
contentAlignment = Alignment.Center
) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Icon(
imageVector = Icons.Default.FilterNone,
contentDescription = null,
tint = Color.White.copy(alpha = 0.6f),
modifier = Modifier.size(24.dp)
)
Spacer(modifier = Modifier.height(8.dp))
Text(
text = stringResource(R.string.settings_custom_background),
color = Color.White.copy(alpha = 0.6f),
fontSize = 11.sp,
textAlign = TextAlign.Center
)
}
if (isSelected) {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Black.copy(alpha = 0.3f)),
contentAlignment = Alignment.Center
) {
Icon(
imageVector = Icons.Default.Check,
contentDescription = null,
tint = Color.White,
modifier = Modifier.size(24.dp)
)
}
}
}
}
@@ -6,6 +6,7 @@ import android.graphics.SurfaceTexture
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CaptureResult
import android.media.Image
import android.net.Uri
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
@@ -155,6 +156,9 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
val useLivePhoto: StateFlow<Boolean> = userPreferencesRepository.userPreferences
.map { it.useLivePhoto }
.stateIn(viewModelScope, SharingStarted.Eagerly, false)
val backgroundImage: StateFlow<String> = userPreferencesRepository.userPreferences
.map { it.backgroundImage }
.stateIn(viewModelScope, SharingStarted.Eagerly, "camera_bg")
// 软件处理参数 Flow
val sharpening: Flow<Float> = userPreferencesRepository.userPreferences.map { it.sharpening }
@@ -1496,6 +1500,45 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
return zoomStops.map { it * mainCamera.focalLength35mmEquivalent }
}
/**
* 设置背景图
*/
fun setBackgroundImage(image: String) {
viewModelScope.launch {
userPreferencesRepository.saveBackgroundImage(image)
}
}
/**
* 保存从外部选择的背景图
*/
fun saveCustomBackgroundImage(uri: Uri) {
viewModelScope.launch {
withContext(Dispatchers.IO) {
try {
val context = getApplication<Application>()
val inputStream = context.contentResolver.openInputStream(uri)
if (inputStream != null) {
val backgroundDir = File(context.filesDir, "backgrounds")
if (!backgroundDir.exists()) {
backgroundDir.mkdirs()
}
val fileName = "custom_bg_${System.currentTimeMillis()}.jpg"
val outputFile = File(backgroundDir, fileName)
inputStream.use { input ->
outputFile.outputStream().use { output ->
input.copyTo(output)
}
}
setBackgroundImage(outputFile.absolutePath)
}
} catch (e: Exception) {
PLog.e(TAG, "Failed to save custom background image", e)
}
}
}
}
override fun onCleared() {
super.onCleared()
Binary file not shown.

After

Width:  |  Height:  |  Size: 323 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 929 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 440 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 642 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 950 KiB

@@ -92,4 +92,7 @@
<string name="settings_multi_frame_count_description">多帧合成所使用的帧数(帧数越多效果越好,但合成也越慢)。</string>
<string name="settings_use_super_resolution">超分辨率</string>
<string name="settings_use_super_resolution_description">使用 2x 多帧超分辨率以获得更多细节。(需要开启多帧合成)</string>
<string name="settings_background">背景设置</string>
<string name="settings_background_description">更改应用背景图</string>
<string name="settings_custom_background">自定义...</string>
</resources>
+3
View File
@@ -149,6 +149,9 @@
<string name="settings_detail_enhancement_description">Fine-tune sharpening and noise reduction parameters</string>
<string name="settings_photo_quality">Photo Quality</string>
<string name="settings_photo_quality_description">Select the JPEG compression quality for photos (higher = better quality, larger file size)</string>
<string name="settings_background">Background</string>
<string name="settings_background_description">Change the app background image</string>
<string name="settings_custom_background">Custom...</string>
<!-- LUT -->
<string name="filter_intensity">Filter Intensity</string>