Skip to content

Commit 1ae14b4

Browse files
authored
Merge pull request #342 from android/parallax_effect
Parallax effect
2 parents cd04e0b + 510630f commit 1ae14b4

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compose.snippets.images
18+
19+
import androidx.compose.foundation.Image
20+
import androidx.compose.foundation.ScrollState
21+
import androidx.compose.foundation.background
22+
import androidx.compose.foundation.layout.Column
23+
import androidx.compose.foundation.layout.fillMaxWidth
24+
import androidx.compose.foundation.layout.padding
25+
import androidx.compose.foundation.rememberScrollState
26+
import androidx.compose.foundation.verticalScroll
27+
import androidx.compose.material3.Text
28+
import androidx.compose.runtime.Composable
29+
import androidx.compose.ui.Modifier
30+
import androidx.compose.ui.graphics.Color
31+
import androidx.compose.ui.layout.ContentScale
32+
import androidx.compose.ui.layout.layout
33+
import androidx.compose.ui.res.painterResource
34+
import androidx.compose.ui.res.stringResource
35+
import androidx.compose.ui.unit.dp
36+
import com.example.compose.snippets.R
37+
38+
// [START android_compose_images_parallax]
39+
@Composable
40+
fun ParallaxEffect() {
41+
fun Modifier.parallaxLayoutModifier(scrollState: ScrollState, rate: Int) =
42+
layout { measurable, constraints ->
43+
val placeable = measurable.measure(constraints)
44+
val height = if (rate > 0) scrollState.value / rate else scrollState.value
45+
layout(placeable.width, placeable.height) {
46+
placeable.place(0, height)
47+
}
48+
}
49+
50+
val scrollState = rememberScrollState()
51+
Column(
52+
modifier = Modifier
53+
.fillMaxWidth()
54+
.verticalScroll(scrollState),
55+
) {
56+
57+
Image(
58+
painterResource(id = R.drawable.cupcake),
59+
contentDescription = "Android logo",
60+
contentScale = ContentScale.Fit,
61+
// Reduce scrolling rate by half.
62+
modifier = Modifier.parallaxLayoutModifier(scrollState, 2)
63+
)
64+
65+
Text(
66+
text = stringResource(R.string.detail_placeholder),
67+
modifier = Modifier
68+
.background(Color.White)
69+
.padding(horizontal = 8.dp),
70+
71+
)
72+
}
73+
}
74+
// [END android_compose_images_parallax]

compose/snippets/src/main/res/values-es/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@
5252
<string name="favorites">Favoritos</string>
5353
<string name="shopping">Compras</string>
5454
<string name="profile">Perfil</string>
55+
<string name="detail_placeholder">Esto es sólo un texto de marcador de posición.</string>
5556
</resources>

compose/snippets/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@
5252
<string name="favorites">Favorites</string>
5353
<string name="shopping">Shopping</string>
5454
<string name="profile">Profile</string>
55+
<string name="detail_placeholder">This is just a placeholder.</string>
5556
</resources>

0 commit comments

Comments
 (0)