feat: vibrate
This commit is contained in:
1 parent
9834048d8d
commit
7bf3ce0c6c
8 files changed
+133
-4
No files matched your search
@@ -3,6 +3,7 @@
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<uses-permission android:name="android.permission.CAMERA" />
|
||||
<uses-permission android:name="android.permission.VIBRATE" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-feature android:name="android.hardware.camera" android:required="true" />
|
||||
|
||||
@@ -29,6 +29,7 @@ data class UserPreferences(
|
||||
val showGrid: Boolean = false, // 网格线显示
|
||||
val showLevelIndicator: Boolean = false, // 水平仪显示
|
||||
val shutterSoundEnabled: Boolean = true, // 快门声音
|
||||
val vibrationEnabled: Boolean = true, // 拍摄震动
|
||||
val volumeKeyCapture: Boolean = false, // 音量键拍摄
|
||||
val autoSaveAfterCapture: Boolean = true // 自动保存
|
||||
)
|
||||
@@ -50,6 +51,7 @@ class UserPreferencesRepository(private val context: Context) {
|
||||
private val SHOW_GRID = booleanPreferencesKey("show_grid")
|
||||
private val SHOW_LEVEL_INDICATOR = booleanPreferencesKey("show_level_indicator")
|
||||
private val SHUTTER_SOUND_ENABLED = booleanPreferencesKey("shutter_sound_enabled")
|
||||
private val VIBRATION_ENABLED = booleanPreferencesKey("vibration_enabled")
|
||||
private val VOLUME_KEY_CAPTURE = booleanPreferencesKey("volume_key_capture")
|
||||
private val AUTO_SAVE_AFTER_CAPTURE = booleanPreferencesKey("auto_save_after_capture")
|
||||
}
|
||||
@@ -69,6 +71,7 @@ class UserPreferencesRepository(private val context: Context) {
|
||||
showGrid = preferences[SHOW_GRID] ?: false,
|
||||
showLevelIndicator = preferences[SHOW_LEVEL_INDICATOR] ?: false,
|
||||
shutterSoundEnabled = preferences[SHUTTER_SOUND_ENABLED] ?: true,
|
||||
vibrationEnabled = preferences[VIBRATION_ENABLED] ?: true,
|
||||
volumeKeyCapture = preferences[VOLUME_KEY_CAPTURE] ?: false,
|
||||
autoSaveAfterCapture = preferences[AUTO_SAVE_AFTER_CAPTURE] ?: true
|
||||
)
|
||||
@@ -164,7 +167,16 @@ class UserPreferencesRepository(private val context: Context) {
|
||||
preferences[SHUTTER_SOUND_ENABLED] = enabled
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 保存是否启用拍摄震动
|
||||
*/
|
||||
suspend fun saveVibrationEnabled(enabled: Boolean) {
|
||||
context.dataStore.edit { preferences ->
|
||||
preferences[VIBRATION_ENABLED] = enabled
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存是否启用音量键拍摄
|
||||
*/
|
||||
|
||||
@@ -50,6 +50,7 @@ fun SettingsScreen(
|
||||
val state by viewModel.state.collectAsState()
|
||||
val showLevelIndicator by viewModel.showLevelIndicator.collectAsState(initial = false)
|
||||
val shutterSoundEnabled by viewModel.shutterSoundEnabled.collectAsState(initial = true)
|
||||
val vibrationEnabled by viewModel.vibrationEnabled.collectAsState(initial = true)
|
||||
val volumeKeyCapture by viewModel.volumeKeyCapture.collectAsState(initial = false)
|
||||
val autoSaveAfterCapture by viewModel.autoSaveAfterCapture.collectAsState(initial = true)
|
||||
val isPurchased by viewModel.isPurchased.collectAsState()
|
||||
@@ -183,6 +184,18 @@ fun SettingsScreen(
|
||||
modifier = Modifier.padding(vertical = 8.dp)
|
||||
)
|
||||
|
||||
SwitchSettingItem(
|
||||
title = stringResource(R.string.settings_vibration),
|
||||
description = stringResource(R.string.settings_vibration_description),
|
||||
checked = vibrationEnabled,
|
||||
onCheckedChange = { viewModel.setVibrationEnabled(it) }
|
||||
)
|
||||
|
||||
HorizontalDivider(
|
||||
color = Color.White.copy(alpha = 0.1f),
|
||||
modifier = Modifier.padding(vertical = 8.dp)
|
||||
)
|
||||
|
||||
SwitchSettingItem(
|
||||
title = stringResource(R.string.settings_volume_key),
|
||||
description = stringResource(R.string.settings_volume_key_description),
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.hinnka.mycamera.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.os.VibrationEffect
|
||||
import android.os.Vibrator
|
||||
import android.os.VibratorManager
|
||||
|
||||
/**
|
||||
* 震动辅助类
|
||||
*
|
||||
* 负责在拍照时提供触觉反馈
|
||||
* 使用 Vibrator 实现震动效果
|
||||
*/
|
||||
class VibrationHelper(private val context: Context) {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "VibrationHelper"
|
||||
private const val VIBRATION_DURATION_MS = 50L // 震动持续时间(毫秒)
|
||||
}
|
||||
|
||||
private val vibrator: Vibrator? by lazy {
|
||||
try {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
val vibratorManager = context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as? VibratorManager
|
||||
vibratorManager?.defaultVibrator
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
context.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to get vibrator service", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行震动反馈
|
||||
*/
|
||||
fun vibrate() {
|
||||
try {
|
||||
vibrator?.let { vib ->
|
||||
if (!vib.hasVibrator()) {
|
||||
PLog.w(TAG, "Device does not have vibrator")
|
||||
return
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
// Android 8.0 及以上使用 VibrationEffect
|
||||
val effect = VibrationEffect.createOneShot(
|
||||
VIBRATION_DURATION_MS,
|
||||
VibrationEffect.DEFAULT_AMPLITUDE
|
||||
)
|
||||
vib.vibrate(effect)
|
||||
} else {
|
||||
// Android 8.0 以下使用旧 API
|
||||
@Suppress("DEPRECATION")
|
||||
vib.vibrate(VIBRATION_DURATION_MS)
|
||||
}
|
||||
|
||||
PLog.d(TAG, "Vibration triggered")
|
||||
} ?: run {
|
||||
PLog.w(TAG, "Vibrator not available")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to vibrate", e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消震动
|
||||
*/
|
||||
fun cancel() {
|
||||
try {
|
||||
vibrator?.cancel()
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to cancel vibration", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import com.hinnka.mycamera.lut.LutInfo
|
||||
import com.hinnka.mycamera.utils.OrientationObserver
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
import com.hinnka.mycamera.utils.ShutterSoundPlayer
|
||||
import com.hinnka.mycamera.utils.VibrationHelper
|
||||
import com.hinnka.mycamera.utils.YuvProcessor
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.*
|
||||
@@ -68,6 +69,9 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
// 快门音效播放器
|
||||
private val shutterSoundPlayer = ShutterSoundPlayer(application)
|
||||
|
||||
// 震动辅助类
|
||||
private val vibrationHelper = VibrationHelper(application)
|
||||
|
||||
val state: StateFlow<CameraState> = cameraController.state
|
||||
|
||||
// 照片保存完成事件
|
||||
@@ -115,10 +119,12 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
// 新增设置项 StateFlow
|
||||
val showLevelIndicator: Flow<Boolean> = userPreferencesRepository.userPreferences.map { it.showLevelIndicator }
|
||||
val shutterSoundEnabled: Flow<Boolean> = userPreferencesRepository.userPreferences.map { it.shutterSoundEnabled }
|
||||
val vibrationEnabled: Flow<Boolean> = userPreferencesRepository.userPreferences.map { it.vibrationEnabled }
|
||||
val volumeKeyCapture: Flow<Boolean> = userPreferencesRepository.userPreferences.map { it.volumeKeyCapture }
|
||||
val autoSaveAfterCapture: Flow<Boolean> = userPreferencesRepository.userPreferences.map { it.autoSaveAfterCapture }
|
||||
|
||||
private var isShutterSoundEnabled = true
|
||||
private var isVibrationEnabled = true
|
||||
|
||||
// 保存当前的 SurfaceTexture 以便切换摄像头时重用
|
||||
private var currentSurfaceTexture: SurfaceTexture? = null
|
||||
@@ -137,18 +143,22 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
}
|
||||
|
||||
// 监听快门声音设置
|
||||
// 监听快门声音和震动设置
|
||||
viewModelScope.launch {
|
||||
userPreferencesRepository.userPreferences.collect {
|
||||
isShutterSoundEnabled = it.shutterSoundEnabled
|
||||
isVibrationEnabled = it.vibrationEnabled
|
||||
}
|
||||
}
|
||||
|
||||
// 设置快门音效回调
|
||||
// 设置快门音效和震动回调
|
||||
cameraController.onPlayShutterSound = {
|
||||
if (isShutterSoundEnabled) {
|
||||
shutterSoundPlayer.play()
|
||||
}
|
||||
if (isVibrationEnabled) {
|
||||
vibrationHelper.vibrate()
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化内容仓库
|
||||
@@ -677,6 +687,15 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否启用拍摄震动
|
||||
*/
|
||||
fun setVibrationEnabled(enabled: Boolean) {
|
||||
viewModelScope.launch {
|
||||
userPreferencesRepository.saveVibrationEnabled(enabled)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否启用音量键拍摄
|
||||
*/
|
||||
|
||||
@@ -89,6 +89,8 @@
|
||||
<string name="settings_section_operation">拍摄操作</string>
|
||||
<string name="settings_shutter_sound">快门声音</string>
|
||||
<string name="settings_shutter_sound_description">拍照时播放快门声音</string>
|
||||
<string name="settings_vibration">拍摄震动</string>
|
||||
<string name="settings_vibration_description">拍照时产生震动反馈</string>
|
||||
<string name="settings_volume_key">音量键拍摄</string>
|
||||
<string name="settings_volume_key_description">使用音量键进行拍摄</string>
|
||||
<string name="settings_auto_save">拍摄后自动保存</string>
|
||||
|
||||
@@ -90,6 +90,8 @@
|
||||
<string name="settings_section_operation">Operations</string>
|
||||
<string name="settings_shutter_sound">Shutter Sound</string>
|
||||
<string name="settings_shutter_sound_description">Play sound when taking a photo</string>
|
||||
<string name="settings_vibration">Vibration</string>
|
||||
<string name="settings_vibration_description">Vibrate when taking a photo</string>
|
||||
<string name="settings_volume_key">Volume Key Capture</string>
|
||||
<string name="settings_volume_key_description">Use volume keys to take photos</string>
|
||||
<string name="settings_auto_save">Auto Save</string>
|
||||
|
||||
Reference in new issue
Block a user