Skip to content

Instantly share code, notes, and snippets.

@mayojava
Created October 14, 2024 16:32
Show Gist options
  • Save mayojava/879d03b849d0f98bb6efd4abb9fdf24b to your computer and use it in GitHub Desktop.
Save mayojava/879d03b849d0f98bb6efd4abb9fdf24b to your computer and use it in GitHub Desktop.
package com.example.kormics
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PageSize
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.coerceIn
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.lerp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import coil.compose.rememberImagePainter
import kotlin.math.absoluteValue
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun OntoPager() {
val pagerState = rememberPagerState(initialPage = 0, pageCount = { 10 })
val screenWidth = LocalConfiguration.current.screenWidthDp.dp
val remainingSpace = remember { screenWidth - 220.dp }
val contentPadding = remember { remainingSpace / 2 }
Box(modifier = Modifier.fillMaxSize()) {
HorizontalPager(
state = pagerState,
pageSize = PageSize.Fixed(220.dp),
contentPadding = PaddingValues(horizontal = contentPadding),
modifier = Modifier.fillMaxWidth().align(Alignment.Center),
) { page: Int ->
Box(
modifier = Modifier
.size(
width = 220.dp,
height = lerp(
300.dp,
250.dp,
pagerState.getOffsetFractionForPage(page).absoluteValue
).coerceIn(250.dp, 300.dp)
)
.graphicsLayer {
androidx.compose.ui.util.lerp(
1f,
0.9f,
fraction = pagerState.getOffsetFractionForPage(page).absoluteValue
).also { scale ->
scaleX = scale
scaleY = scale
}
}
.rotate(
if (page < pagerState.currentPage) {
androidx.compose.ui.util.lerp(0f, -10f, pagerState.getOffsetFractionForPage(page).absoluteValue).coerceAtLeast(-10f)
} else if (page > pagerState.currentPage) {
androidx.compose.ui.util.lerp(0f, 10f, pagerState.getOffsetFractionForPage(page).absoluteValue).coerceAtMost(10f)
}
else {
0f
}
)
.clip(shape = RoundedCornerShape(16.dp))
.zIndex(if (page == pagerState.currentPage) 1f else 0f)
.shadow(
if (page == pagerState.currentPage) 10.dp else 0.dp,
shape = RoundedCornerShape(16.dp)
)
.background(
brush = Brush.linearGradient(listOf(Color.Cyan, Color.Blue)),
shape = RoundedCornerShape(
4.dp
)
)
) {
Image(
modifier = Modifier.matchParentSize(),
contentScale = ContentScale.Crop,
contentDescription = null,
painter = rememberImagePainter(
data = rememberRandomSampleImageUrl(width = 600)
)
)
}
}
}
}
@Composable
fun rememberRandomSampleImageUrl(
seed: Int = rangeForRandom.random(),
width: Int = 300,
height: Int = width,
): String = remember { randomSampleImageUrl(seed, width, height) }
private val rangeForRandom = (0..100000)
fun randomSampleImageUrl(
seed: Int = rangeForRandom.random(),
width: Int = 300,
height: Int = width,
): String {
return "https://picsum.photos/seed/$seed/$width/$height"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment