Guest User

Untitled

a guest
Oct 3rd, 2023
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. @Composable
  2. fun AudioPlayerProgressUI(
  3. progress: Float,
  4. modifier: Modifier = Modifier
  5. ) {
  6.  
  7. val animatedProgress by animateFloatAsState(
  8. targetValue = progress,
  9. animationSpec = tween(500, easing = LinearEasing),
  10. label = "Seek bar progress"
  11. )
  12.  
  13. BoxWithConstraints(
  14. modifier = modifier
  15. .fillMaxSize()
  16. .background(Color.Gray)
  17. ) {
  18.  
  19. val screenWidth = maxWidth.value
  20. val screenHeight = maxHeight.value
  21.  
  22. val dummyHeight = LocalConfiguration.current.screenHeightDp
  23.  
  24. Text(
  25. text = "OuterLayoutHeight --> {${dummyHeight.dp}} {$screenHeight}",
  26. modifier = Modifier.align(Alignment.TopStart)
  27. )
  28.  
  29. val barsCount = remember {
  30. screenWidth / 2
  31. }.toInt()
  32.  
  33. val random = remember {
  34. Random.Default
  35. }
  36.  
  37. val heights = remember {
  38. buildList {
  39. for (i in 0..barsCount) {
  40. add(element = screenHeight * (random.nextFloat() * 0.5f + 0.1f))
  41. }
  42. }.toPersistentList()
  43. }
  44.  
  45. val space = remember {
  46. maxWidth / 35
  47. }.value
  48.  
  49. var startX = remember {
  50. 0f
  51. }
  52.  
  53. val path = remember {
  54. Path().apply {
  55. heights.forEachIndexed { index, barHeight ->
  56. addRoundRect(
  57. RoundRect(
  58. rect = Rect(
  59. offset = Offset(
  60. x = startX,
  61. y = (screenHeight) * 0.5f
  62. ),
  63. size = Size(width = screenWidth * 0.01f, barHeight)
  64. ),
  65. radiusX = 10f,
  66. radiusY = 10f
  67. )
  68. )
  69. startX += space
  70. }
  71. close()
  72. }
  73. }
  74.  
  75. val textMeasurer = rememberTextMeasurer()
  76.  
  77. Canvas(
  78. modifier = Modifier
  79. .padding(top = 40.dp)
  80. .fillMaxSize()
  81. .background(Color.Green)
  82. ) {
  83. val canvasWidth = size.width
  84. val canvasHeight = size.height
  85.  
  86. drawRect(
  87. color = Color.Red,
  88. topLeft = Offset(0f, canvasHeight * 0.25f),
  89. size = Size(
  90. width = canvasWidth * animatedProgress,
  91. height = canvasHeight * 0.5f
  92. )
  93. )
  94.  
  95. clipPath(path = path, clipOp = ClipOp.Difference) {
  96. drawRoundRect(
  97. color = Color.Transparent,
  98. size = Size(width = canvasWidth, height = canvasHeight),
  99. cornerRadius = CornerRadius(screenWidth * 0.05f, screenWidth * 0.05f)
  100. )
  101. }
  102.  
  103. drawText(
  104. textMeasurer.measure(
  105. text = "Canvas Height -> {$canvasHeight}",
  106. style = TextStyle(
  107. color = Color.Blue,
  108. textAlign = TextAlign.Center,
  109. fontSize = 24.sp
  110. ),
  111. maxLines = 2,
  112. overflow = Ellipsis,
  113. )
  114. )
  115.  
  116. }
  117. }
  118. }
  119.  
  120.  
  121. @Preview
  122. @Composable
  123. fun PreviewAudioPlayerProgressUI() {
  124. ScrutinizingTheServiceTheme {
  125. AudioPlayerProgressUI(
  126. progress = 0.0f,
  127. modifier = Modifier.fillMaxSize()
  128. )
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment