From 81c1a2a1d0cb80dd91576236d381ac8f464b30e3 Mon Sep 17 00:00:00 2001 From: Jake Roseman <122034773+jakeroseman@users.noreply.github.com> Date: Tue, 5 Nov 2024 14:42:17 +0000 Subject: [PATCH 1/3] Animate image size on scroll (#390) * Add animate image size on scroll example * Add simple comments and rename some variables * Apply Spotless * Add region tags --------- Co-authored-by: jakeroseman --- .../snippets/images/AnimateImageSnippets.kt | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 compose/snippets/src/main/java/com/example/compose/snippets/images/AnimateImageSnippets.kt diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/images/AnimateImageSnippets.kt b/compose/snippets/src/main/java/com/example/compose/snippets/images/AnimateImageSnippets.kt new file mode 100644 index 00000000..2eab0220 --- /dev/null +++ b/compose/snippets/src/main/java/com/example/compose/snippets/images/AnimateImageSnippets.kt @@ -0,0 +1,118 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.compose.snippets.images + +import androidx.compose.foundation.Image +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.offset +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableFloatStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.graphicsLayer +import androidx.compose.ui.graphics.painter.ColorPainter +import androidx.compose.ui.input.nestedscroll.NestedScrollConnection +import androidx.compose.ui.input.nestedscroll.NestedScrollSource +import androidx.compose.ui.input.nestedscroll.nestedScroll +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.IntOffset +import androidx.compose.ui.unit.dp + +// [START android_compose_images_imageresizeonscrollexample] +@Composable +fun ImageResizeOnScrollExample( + modifier: Modifier = Modifier, + maxImageSize: Dp = 300.dp, + minImageSize: Dp = 100.dp +) { + var currentImageSize by remember { mutableStateOf(maxImageSize) } + var imageScale by remember { mutableFloatStateOf(1f) } + + val nestedScrollConnection = remember { + object : NestedScrollConnection { + override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset { + // Calculate the change in image size based on scroll delta + val delta = available.y + val newImageSize = currentImageSize + delta.dp + val previousImageSize = currentImageSize + + // Constrain the image size within the allowed bounds + currentImageSize = newImageSize.coerceIn(minImageSize, maxImageSize) + val consumed = currentImageSize - previousImageSize + + // Calculate the scale for the image + imageScale = currentImageSize / maxImageSize + + // Return the consumed scroll amount + return Offset(0f, consumed.value) + } + } + } + + Box(Modifier.nestedScroll(nestedScrollConnection)) { + LazyColumn( + Modifier + .fillMaxWidth() + .padding(15.dp) + .offset { + IntOffset(0, currentImageSize.roundToPx()) + } + ) { + // Placeholder list items + items(100, key = { it }) { + Text( + text = "Item: $it", + style = MaterialTheme.typography.bodyLarge + ) + } + } + + Image( + painter = ColorPainter(Color.Red), + contentDescription = "Red color image", + Modifier + .size(maxImageSize) + .align(Alignment.TopCenter) + .graphicsLayer { + scaleX = imageScale + scaleY = imageScale + // Center the image vertically as it scales + translationY = -(maxImageSize.toPx() - currentImageSize.toPx()) / 2f + } + ) + } +} +// [END android_compose_images_imageresizeonscrollexample] + +@Preview(showBackground = true) +@Composable +private fun ImageSizeOnScrollScreenPreview() { + ImageResizeOnScrollExample() +} From 1da22a01f7dbe6c610374e0db501827e8ada15df Mon Sep 17 00:00:00 2001 From: Jake Roseman <122034773+jakeroseman@users.noreply.github.com> Date: Tue, 5 Nov 2024 16:33:13 +0000 Subject: [PATCH 2/3] Add basic HTML text styling example (#389) * Add basic HTML text styling example * Apply Spotless * Add region tags --------- Co-authored-by: jakeroseman --- .../compose/snippets/text/HtmlStyling.kt | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 compose/snippets/src/main/java/com/example/compose/snippets/text/HtmlStyling.kt diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/text/HtmlStyling.kt b/compose/snippets/src/main/java/com/example/compose/snippets/text/HtmlStyling.kt new file mode 100644 index 00000000..607fdf46 --- /dev/null +++ b/compose/snippets/src/main/java/com/example/compose/snippets/text/HtmlStyling.kt @@ -0,0 +1,62 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.compose.snippets.text + +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.text.AnnotatedString +import androidx.compose.ui.text.SpanStyle +import androidx.compose.ui.text.TextLinkStyles +import androidx.compose.ui.text.font.FontStyle +import androidx.compose.ui.text.fromHtml +import androidx.compose.ui.text.style.TextDecoration +import androidx.compose.ui.tooling.preview.Preview + +// [START android_compose_text_annotatedhtmlstringwithlink] +@Composable +fun AnnotatedHtmlStringWithLink( + modifier: Modifier = Modifier, + htmlText: String = """ +

Jetpack Compose

+

+ Build better apps faster with Jetpack Compose +

+ """.trimIndent() +) { + Text( + AnnotatedString.fromHtml( + htmlText, + linkStyles = TextLinkStyles( + style = SpanStyle( + textDecoration = TextDecoration.Underline, + fontStyle = FontStyle.Italic, + color = Color.Blue + ) + ) + ), + modifier + ) +} +// [END android_compose_text_annotatedhtmlstringwithlink] + +@Preview(showBackground = true) +@Composable +private fun AnnotatedHtmlStringWithLinkPreview() { + AnnotatedHtmlStringWithLink() +} From f595a0d317fd8aca6b33d4344bf87696cd45c481 Mon Sep 17 00:00:00 2001 From: compose-devrel-github-bot <118755852+compose-devrel-github-bot@users.noreply.github.com> Date: Wed, 6 Nov 2024 13:47:07 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=A4=96=20Update=20Dependencies=20(#39?= =?UTF-8?q?1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🤖 Update Dependencies * Update compileSdk to 35 --------- Co-authored-by: Jolanda Verhoef --- gradle/libs.versions.toml | 38 +++++++++++++++++++------------------- wear/build.gradle.kts | 2 +- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7d649bdc..6d03c099 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,20 +1,20 @@ [versions] accompanist = "0.36.0" -androidGradlePlugin = "8.7.0" -androidx-activity-compose = "1.9.2" +androidGradlePlugin = "8.7.2" +androidx-activity-compose = "1.9.3" androidx-appcompat = "1.7.0" -androidx-compose-bom = "2024.09.03" +androidx-compose-bom = "2024.10.01" androidx-compose-ui-test = "1.7.0-alpha08" -androidx-constraintlayout = "2.1.4" -androidx-constraintlayout-compose = "1.0.1" +androidx-constraintlayout = "2.2.0" +androidx-constraintlayout-compose = "1.1.0" androidx-coordinator-layout = "1.2.0" -androidx-corektx = "1.13.1" +androidx-corektx = "1.15.0" androidx-emoji2-views = "1.5.0" -androidx-fragment-ktx = "1.8.4" +androidx-fragment-ktx = "1.8.5" androidx-glance-appwidget = "1.1.1" -androidx-lifecycle-compose = "2.8.6" -androidx-lifecycle-runtime-compose = "2.8.6" -androidx-navigation = "2.8.2" +androidx-lifecycle-compose = "2.8.7" +androidx-lifecycle-runtime-compose = "2.8.7" +androidx-navigation = "2.8.3" androidx-paging = "3.3.2" androidx-test = "1.6.1" androidx-test-espresso = "3.6.1" @@ -22,8 +22,8 @@ androidx-window = "1.3.0" androidxHiltNavigationCompose = "1.2.0" coil = "2.7.0" # @keep -compileSdk = "34" -compose-latest = "1.7.3" +compileSdk = "35" +compose-latest = "1.7.5" composeUiTooling = "1.4.0" coreSplashscreen = "1.0.1" coroutines = "1.9.0" @@ -33,13 +33,13 @@ gradle-versions = "0.51.0" hilt = "2.52" horologist = "0.6.20" junit = "4.13.2" -kotlin = "2.0.20" +kotlin = "2.0.21" kotlinxSerializationJson = "1.7.3" -ksp = "2.0.20-1.0.25" -maps-compose = "6.1.2" -material = "1.13.0-alpha06" +ksp = "2.0.21-1.0.26" +maps-compose = "6.2.0" +material = "1.13.0-alpha07" material3-adaptive = "1.0.0" -material3-adaptive-navigation-suite = "1.3.0" +material3-adaptive-navigation-suite = "1.3.1" media3 = "1.4.1" # @keep minSdk = "21" @@ -47,7 +47,7 @@ playServicesWearable = "18.2.0" recyclerview = "1.3.2" # @keep targetSdk = "34" -version-catalog-update = "0.8.4" +version-catalog-update = "0.8.5" wearComposeFoundation = "1.4.0" wearComposeMaterial = "1.4.0" @@ -108,7 +108,7 @@ androidx-test-core = { module = "androidx.test:core", version.ref = "androidx-te androidx-test-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "androidx-test-espresso" } androidx-test-runner = "androidx.test:runner:1.6.2" androidx-window-core = { module = "androidx.window:window-core", version.ref = "androidx-window" } -androidx-work-runtime-ktx = "androidx.work:work-runtime-ktx:2.9.1" +androidx-work-runtime-ktx = "androidx.work:work-runtime-ktx:2.10.0" coil-kt-compose = { module = "io.coil-kt:coil-compose", version.ref = "coil" } compose-foundation = { module = "androidx.wear.compose:compose-foundation", version.ref = "wearComposeFoundation" } compose-material = { module = "androidx.wear.compose:compose-material", version.ref = "wearComposeMaterial" } diff --git a/wear/build.gradle.kts b/wear/build.gradle.kts index 9725e539..13fff794 100644 --- a/wear/build.gradle.kts +++ b/wear/build.gradle.kts @@ -6,7 +6,7 @@ plugins { android { namespace = "com.example.wear" - compileSdk = 34 + compileSdk = 35 defaultConfig { applicationId = "com.example.wear"