Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.myapplication
- import android.content.Context
- import android.util.AttributeSet
- import android.view.View
- import android.view.ViewGroup
- class RoundPanel(context: Context?, attrs: AttributeSet?) : ViewGroup(context, attrs) {
- override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec)
- forEachChild {
- it.measure(
- MeasureSpec.makeMeasureSpec(50.dip(), MeasureSpec.EXACTLY),
- MeasureSpec.makeMeasureSpec(50.dip(), MeasureSpec.EXACTLY))
- }
- }
- override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
- forEachChildIndexed { i, child ->
- val angel = (180.0 * i / (childCount - 1))
- val R = (r - l) / 2f - child.height / 2
- val x = Math.sin(Math.toRadians(angel)) * R + (r - l) / 2
- val y = Math.cos(Math.toRadians(angel)) * R + (b - t) / 2
- child.layout(
- (x - child.measuredWidth / 2).toInt(),
- (y - child.measuredHeight / 2).toInt(),
- (x + child.measuredWidth / 2).toInt(),
- (y + child.measuredHeight / 2).toInt())
- child.rotation = -angel.toFloat()
- }
- }
- private fun Int.dip() = (this * resources.displayMetrics.density).toInt()
- private fun ViewGroup.forEachChild(f: (View) -> Unit) {
- return (0..childCount - 1).forEach { f(getChildAt(it)) }
- }
- private fun ViewGroup.forEachChildIndexed(f: (Int, View) -> Unit) {
- return (0..childCount - 1).forEach { f(it, getChildAt(it)) }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement