Skip to content

Commit

Permalink
Zoom Text
Browse files Browse the repository at this point in the history
A text above the capture button that appears for 3 seconds every time zoom scale is changed.
  • Loading branch information
davidjiagoogle committed Jul 18, 2023
1 parent 885b37d commit 7d81506
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ interface CameraUseCase {

fun stopVideoRecording()

fun setZoomScale(scale: Float)
fun setZoomScale(scale: Float): Float

fun setFlashMode(flashModeStatus: FlashModeStatus)
suspend fun flipCamera(isFrontFacing: Boolean)

companion object {
const val INVALID_ZOOM_SCALE = -1f
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import androidx.camera.video.VideoCapture
import androidx.concurrent.futures.await
import androidx.core.content.ContextCompat
import androidx.core.util.Consumer
import com.google.jetpackcamera.domain.camera.CameraUseCase.Companion.INVALID_ZOOM_SCALE
import com.google.jetpackcamera.settings.SettingsRepository
import com.google.jetpackcamera.settings.model.CameraAppSettings
import com.google.jetpackcamera.settings.model.FlashModeStatus
Expand Down Expand Up @@ -168,11 +169,12 @@ class CameraXCameraUseCase @Inject constructor(
recording?.stop()
}

override fun setZoomScale(scale: Float) {
val zoomState = getZoomState() ?: return
override fun setZoomScale(scale: Float): Float {
val zoomState = getZoomState() ?: return INVALID_ZOOM_SCALE
val finalScale =
(zoomState.zoomRatio * scale).coerceIn(zoomState.minZoomRatio, zoomState.maxZoomRatio)
camera?.cameraControl?.setZoomRatio(finalScale)
return finalScale
}

private fun getZoomState(): ZoomState? = camera?.cameraInfo?.zoomState?.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class FakeCameraUseCase : CameraUseCase {
recordingInProgress = false
}

override fun setZoomScale(scale: Float) {
override fun setZoomScale(scale: Float): Float {
return -1f
}

override fun setFlashMode(flashModeStatus: FlashModeStatus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@

package com.google.jetpackcamera.feature.preview

import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.camera.core.Preview.SurfaceProvider
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.border
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.gestures.rememberTransformableState
import androidx.compose.foundation.gestures.transformable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
Expand All @@ -40,14 +45,18 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.repeatOnLifecycle
Expand Down Expand Up @@ -77,8 +86,13 @@ fun PreviewScreen(
Log.d(TAG, "onSurfaceProviderReady")
deferredSurfaceProvider.complete(it)
}
var zoomScale by remember { mutableStateOf(1f) }
var zoomScaleShow by remember { mutableStateOf(false) }
val zoomHandler = Handler(Looper.getMainLooper())
val transformableState = rememberTransformableState(onTransformation = { zoomChange, _, _ ->
viewModel.setZoomScale(zoomChange)
zoomScale = viewModel.setZoomScale(zoomChange)
zoomScaleShow = true
zoomHandler.postDelayed({ zoomScaleShow = false }, 3000)
})

LaunchedEffect(lifecycleOwner) {
Expand All @@ -97,7 +111,9 @@ fun PreviewScreen(
Text(text = stringResource(R.string.camera_not_ready))
} else if (previewUiState.cameraState == CameraState.READY) {
Box(
modifier = Modifier.fillMaxSize().transformable(state = transformableState)
modifier = Modifier
.fillMaxSize()
.transformable(state = transformableState)
) {
CameraPreview(
modifier = Modifier
Expand Down Expand Up @@ -140,23 +156,45 @@ fun PreviewScreen(
)
}

Row(
modifier = Modifier.align(Alignment.BottomCenter),
horizontalArrangement = Arrangement.SpaceAround
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.align(Alignment.BottomCenter)
) {
Row {
CaptureButton(
onClick = { viewModel.captureImage() },
onLongPress = { viewModel.startVideoRecording() },
onRelease = { viewModel.stopVideoRecording() },
state = previewUiState.videoRecordingState
)
ZoomScaleText(
zoomScale = zoomScale,
show = zoomScaleShow
)
Row(
horizontalArrangement = Arrangement.SpaceAround
) {
Row {
CaptureButton(
onClick = { viewModel.captureImage() },
onLongPress = { viewModel.startVideoRecording() },
onRelease = { viewModel.stopVideoRecording() },
state = previewUiState.videoRecordingState
)
}
}
}
}
}
}

@Composable
fun ZoomScaleText(zoomScale: Float, show: Boolean) {
val contentAlpha = animateFloatAsState(
targetValue = if (show) 1f else 0f, label = "zoomScaleAlphaAnimation",
animationSpec = tween()
)
Text(
modifier = Modifier.alpha(contentAlpha.value),
text = String.format("%.1fx", zoomScale),
fontSize = 20.sp,
color = Color.White
)
}

@Composable
fun CaptureButton(
onClick: () -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ class PreviewViewModel @Inject constructor(
recordingJob?.cancel()
}

fun setZoomScale(scale: Float) {
cameraUseCase.setZoomScale(scale = scale)
fun setZoomScale(scale: Float): Float {
return cameraUseCase.setZoomScale(scale = scale)
}

// modify ui values
Expand Down

0 comments on commit 7d81506

Please sign in to comment.