Skip to content

Commit 7684369

Browse files
Google AI Edge Gallerycopybara-github
authored andcommitted
Make the home screen title font size adapt to screen width dynamically
PiperOrigin-RevId: 791414269
1 parent 5a20d9a commit 7684369

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

Android/src/app/src/main/java/com/google/ai/edge/gallery/ui/common/Utils.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import androidx.compose.ui.graphics.Color
4545
import androidx.compose.ui.graphics.CompositingStrategy
4646
import androidx.compose.ui.graphics.graphicsLayer
4747
import androidx.compose.ui.text.TextStyle
48+
import androidx.compose.ui.unit.Dp
4849
import androidx.compose.ui.unit.dp
4950
import androidx.core.content.ContextCompat
5051
import androidx.core.content.FileProvider
@@ -247,6 +248,7 @@ fun RevealingText(
247248
animationDelay: Long = 0,
248249
animationDurationMs: Int = 300,
249250
edgeGradientRelativeSize: Float = 0.5f,
251+
extraTextPadding: Dp = 16.dp,
250252
) {
251253
val progress =
252254
rememberDelayedAnimationProgress(
@@ -273,7 +275,7 @@ fun RevealingText(
273275
},
274276
contentAlignment = Alignment.Center,
275277
) {
276-
Text(text, style = style, modifier = modifier.padding(horizontal = 16.dp))
278+
Text(text, style = style, modifier = Modifier.padding(horizontal = extraTextPadding))
277279
}
278280
}
279281

Android/src/app/src/main/java/com/google/ai/edge/gallery/ui/home/HomeScreen.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ import androidx.compose.ui.draw.clip
7878
import androidx.compose.ui.graphics.Brush.Companion.linearGradient
7979
import androidx.compose.ui.graphics.graphicsLayer
8080
import androidx.compose.ui.input.nestedscroll.nestedScroll
81+
import androidx.compose.ui.platform.LocalConfiguration
8182
import androidx.compose.ui.platform.LocalContext
83+
import androidx.compose.ui.platform.LocalDensity
8284
import androidx.compose.ui.platform.LocalUriHandler
8385
import androidx.compose.ui.res.stringResource
8486
import androidx.compose.ui.text.LinkAnnotation
@@ -420,6 +422,9 @@ private fun AppTitle() {
420422
val firstLineText = stringResource(R.string.app_name_first_part)
421423
val secondLineText = stringResource(R.string.app_name_second_part)
422424
val titleColor = MaterialTheme.customColors.appTitleGradientColors[1]
425+
val screenWidthInDp = LocalConfiguration.current.screenWidthDp.dp
426+
val fontSize = with(LocalDensity.current) { (screenWidthInDp.toPx() * 0.12f).toSp() }
427+
val titleStyle = homePageTitleStyle.copy(fontSize = fontSize, lineHeight = fontSize)
423428

424429
// First line text "Google AI" and its animation.
425430
//
@@ -433,15 +438,15 @@ private fun AppTitle() {
433438
var delay = ANIMATION_INIT_DELAY
434439
SwipingText(
435440
text = firstLineText,
436-
style = homePageTitleStyle,
441+
style = titleStyle,
437442
color = titleColor,
438443
animationDelay = delay,
439444
animationDurationMs = TITLE_FIRST_LINE_ANIMATION_DURATION,
440445
)
441446
delay += (TITLE_FIRST_LINE_ANIMATION_DURATION * 0.3).toLong()
442447
SwipingText(
443448
text = firstLineText,
444-
style = homePageTitleStyle,
449+
style = titleStyle,
445450
color = MaterialTheme.colorScheme.onSurface,
446451
animationDelay = delay,
447452
animationDurationMs = TITLE_FIRST_LINE_ANIMATION_DURATION,
@@ -455,7 +460,7 @@ private fun AppTitle() {
455460
var delay = TITLE_SECOND_LINE_ANIMATION_START
456461
SwipingText(
457462
text = secondLineText,
458-
style = homePageTitleStyle,
463+
style = titleStyle,
459464
color = titleColor,
460465
modifier = Modifier.offset(y = (-16).dp),
461466
animationDelay = delay,
@@ -464,7 +469,7 @@ private fun AppTitle() {
464469
delay += (TITLE_SECOND_LINE_ANIMATION_DURATION * 0.3).toInt()
465470
SwipingText(
466471
text = secondLineText,
467-
style = homePageTitleStyle,
472+
style = titleStyle,
468473
color = MaterialTheme.colorScheme.onSurface,
469474
modifier = Modifier.offset(y = (-16).dp),
470475
animationDelay = delay,
@@ -474,10 +479,10 @@ private fun AppTitle() {
474479
RevealingText(
475480
text = secondLineText,
476481
style =
477-
homePageTitleStyle.copy(
482+
titleStyle.copy(
478483
brush = linearGradient(colors = MaterialTheme.customColors.appTitleGradientColors)
479484
),
480-
modifier = Modifier.offset(y = (-8).dp, x = (-8).dp),
485+
modifier = Modifier.offset(x = (-16).dp, y = (-16).dp),
481486
animationDelay = delay,
482487
animationDurationMs = TITLE_SECOND_LINE_ANIMATION_DURATION2,
483488
)

0 commit comments

Comments
 (0)