Guest User

Untitled

a guest
Sep 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. class FlyingBackgroundView(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
  2.  
  3. companion object {
  4. private const val SMOOTH_ANIMATION_DURATION = 10000L
  5. private const val SCREEN_WIDTH_FACTOR = 3
  6. private const val SCREEN_HEIGHT_FACTOR = 3
  7. private const val ICON_SIZE_DP = 60
  8. }
  9.  
  10. private val iconSubstrateColor = Color.parseColor("#eeeeee")
  11. private val columnContainer: LinearLayout
  12.  
  13. private val icons = ArrayList<ImageView>()
  14.  
  15. init {
  16. val iconSize = dpToPx(ICON_SIZE_DP)
  17. val iconSubstrateSize = iconSize * 1.5f
  18. val itemSize = iconSubstrateSize * 1.3f
  19.  
  20. val screenMetrics = createDisplayMetrics(context as Activity)
  21. val screenWidth = screenMetrics.widthPixels
  22. val screenHeight = screenMetrics.heightPixels
  23.  
  24. var columnCount = (screenWidth / itemSize).toInt() * SCREEN_WIDTH_FACTOR
  25. val itemCount = (screenHeight / itemSize).toInt() * SCREEN_HEIGHT_FACTOR
  26.  
  27. if (columnCount.isEven()) columnCount++
  28.  
  29. val canvasWidth = columnCount * itemSize.toInt()
  30. val canvasHeight = itemCount * itemSize.toInt()
  31.  
  32. columnContainer = LinearLayout(context)
  33. columnContainer.layoutParams = LinearLayout.LayoutParams(canvasWidth, canvasHeight)
  34. columnContainer.clipChildren = false
  35.  
  36. for (column in 0 until columnCount) {
  37. val columnLayout = LinearLayout(context)
  38. columnLayout.layoutParams = LinearLayout.LayoutParams(itemSize.toInt(),
  39. ViewGroup.LayoutParams.MATCH_PARENT)
  40. columnLayout.clipChildren = false
  41. columnLayout.orientation = VERTICAL
  42. columnLayout.gravity = Gravity.CENTER
  43.  
  44. val countOfItems = if (column.isEven()) itemCount else itemCount - 1
  45. for (item in 0 until countOfItems) {
  46. val itemLayout = LinearLayout(context)
  47. itemLayout.layoutParams = LinearLayout.LayoutParams(itemSize.toInt(), itemSize.toInt())
  48. itemLayout.gravity = Gravity.CENTER
  49.  
  50. val substrateLayout = LinearLayout(context)
  51. substrateLayout.layoutParams = LinearLayout.LayoutParams(iconSubstrateSize.toInt(),
  52. iconSubstrateSize.toInt())
  53. substrateLayout.gravity = Gravity.CENTER
  54. val circleBackground = GradientDrawable()
  55. circleBackground.shape = GradientDrawable.OVAL
  56. circleBackground.setColor(iconSubstrateColor)
  57. substrateLayout.placeBackgroundDrawable(circleBackground)
  58.  
  59. val iconView = ImageView(context)
  60. iconView.layoutParams = LinearLayout.LayoutParams(iconSize, iconSize)
  61. icons.add(iconView)
  62.  
  63. substrateLayout.addView(iconView)
  64. itemLayout.addView(substrateLayout)
  65. columnLayout.addView(itemLayout)
  66. }
  67. columnContainer.addView(columnLayout)
  68. }
  69.  
  70. val centerX = canvasWidth / SCREEN_WIDTH_FACTOR
  71. val centerY = canvasHeight / SCREEN_HEIGHT_FACTOR
  72. columnContainer.x = -centerX.toFloat()
  73. columnContainer.y = -centerY.toFloat()
  74. addView(columnContainer)
  75.  
  76. (object : Runnable {
  77. override fun run() {
  78. val maxX = canvasWidth / SCREEN_WIDTH_FACTOR * (SCREEN_WIDTH_FACTOR - 1)
  79. val maxY = canvasHeight / SCREEN_HEIGHT_FACTOR * (SCREEN_HEIGHT_FACTOR - 1)
  80. val randomX = Random().nextInt(maxX)
  81. val randomY = Random().nextInt(maxY)
  82.  
  83. ViewCompat.animate(columnContainer)
  84. .x(-randomX.toFloat())
  85. .y(-randomY.toFloat())
  86. .setDuration(SMOOTH_ANIMATION_DURATION)
  87. .withEndAction(this)
  88. .start()
  89. }
  90. }).run()
  91. }
  92.  
  93. fun setIcon(@DrawableRes iconResId: Int) {
  94. icons.forEach { icon ->
  95. ViewCompat.animate(icon)
  96. .scaleX(0f)
  97. .scaleY(0f)
  98. .alpha(0f)
  99. .setDuration(1000).withEndAction {
  100. icon.setImageResource(iconResId)
  101. ViewCompat.animate(icon)
  102. .scaleX(1f)
  103. .scaleY(1f)
  104. .alpha(1f)
  105. .setDuration(1000)
  106. .start()
  107. }.start()
  108. }
  109. }
Add Comment
Please, Sign In to add comment