Advertisement
Guest User

Untitled

a guest
Jun 16th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. package com.example.myapplication
  2.  
  3. import android.content.Context
  4. import android.util.AttributeSet
  5. import android.view.View
  6. import android.view.ViewGroup
  7.  
  8. class RoundPanel(context: Context?, attrs: AttributeSet?) : ViewGroup(context, attrs) {
  9.  
  10. override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
  11. super.onMeasure(widthMeasureSpec, heightMeasureSpec)
  12. forEachChild {
  13. it.measure(
  14. MeasureSpec.makeMeasureSpec(50.dip(), MeasureSpec.EXACTLY),
  15. MeasureSpec.makeMeasureSpec(50.dip(), MeasureSpec.EXACTLY))
  16. }
  17. }
  18.  
  19. override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
  20. forEachChildIndexed { i, child ->
  21. val angel = (180.0 * i / (childCount - 1))
  22. val R = (r - l) / 2f - child.height / 2
  23. val x = Math.sin(Math.toRadians(angel)) * R + (r - l) / 2
  24. val y = Math.cos(Math.toRadians(angel)) * R + (b - t) / 2
  25.  
  26. child.layout(
  27. (x - child.measuredWidth / 2).toInt(),
  28. (y - child.measuredHeight / 2).toInt(),
  29. (x + child.measuredWidth / 2).toInt(),
  30. (y + child.measuredHeight / 2).toInt())
  31.  
  32. child.rotation = -angel.toFloat()
  33. }
  34. }
  35.  
  36. private fun Int.dip() = (this * resources.displayMetrics.density).toInt()
  37.  
  38. private fun ViewGroup.forEachChild(f: (View) -> Unit) {
  39. return (0..childCount - 1).forEach { f(getChildAt(it)) }
  40. }
  41.  
  42. private fun ViewGroup.forEachChildIndexed(f: (Int, View) -> Unit) {
  43. return (0..childCount - 1).forEach { f(it, getChildAt(it)) }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement