Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. class PagerDecorator : RecyclerView.ItemDecoration() {
  2.  
  3. private var paintStroke: Paint = Paint().apply {
  4. style = Paint.Style.STROKE
  5. strokeWidth = 4f
  6. color = Color.WHITE
  7. }
  8.  
  9. private val paintFill = Paint().apply {
  10. style = Paint.Style.FILL
  11. color = Color.WHITE
  12. }
  13.  
  14. // save the center coordinates of all indicators
  15. private val indicators = mutableListOf<Pair<Float, Float>>()
  16.  
  17. private val indicatorRadius = 30f
  18. private val indicatorPadding = 180f
  19.  
  20. private var activeIndicator = 0
  21. private var isInitialized = false
  22.  
  23. // create three indicators for three slides
  24. override fun onDrawOver(canvas: Canvas,
  25. parent: RecyclerView,
  26. state: RecyclerView.State) {
  27.  
  28. if(!isInitialized) {
  29. setupIndicators(parent)
  30. }
  31.  
  32. // draw three indicators with stroke style
  33. parent.adapter?.let {
  34. with(canvas) {
  35. drawCircle(indicators[0].first, indicators[0].second)
  36. drawCircle(indicators[1].first, indicators[1].second)
  37. drawCircle(indicators[2].first, indicators[2].second)
  38. }
  39.  
  40. val visibleItem = (parent.layoutManager as LinearLayoutManager)
  41. .findFirstCompletelyVisibleItemPosition()
  42.  
  43. if(visibleItem >= 0) {
  44. activeIndicator = visibleItem
  45. }
  46.  
  47. // paint over the needed circle
  48. when (activeIndicator) {
  49. 0 -> canvas.drawCircle(indicators[0].first, indicators[0].second, true)
  50. 1 -> canvas.drawCircle(indicators[1].first, indicators[1].second, true)
  51. 2 -> canvas.drawCircle(indicators[2].first, indicators[2].second, true)
  52. }
  53. }
  54. }
  55.  
  56. private fun Canvas.drawCircle(x: Float, y: Float, isFill: Boolean = false) {
  57. drawCircle(x, y, indicatorRadius, if(isFill) paintFill else paintStroke)
  58. }
  59.  
  60. private fun setupIndicators(recyclerView: RecyclerView) {
  61. isInitialized = true
  62.  
  63. val indicatorTotalWidth = indicatorRadius * 6 + indicatorPadding
  64. val indicatorPosX = (recyclerView.width - indicatorTotalWidth) / 2f
  65. val indicatorPosY = recyclerView.height - (indicatorRadius * 6 / 2f)
  66.  
  67. indicators.add(Pair(indicatorPosX, indicatorPosY))
  68. indicators.add(Pair(indicatorPosX + indicatorRadius, indicatorPosY))
  69. indicators.add(Pair(indicatorPosX + indicatorRadius * 2, indicatorPosY))
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement