jeudi 27 avril 2023

Semi-circle diagram

I want to create a semi-circle diagram something like this: Semi-circle diagram.

But can't make it proper work dynamically. I want to use TextView, but can't make it exactly as it is in the photo.
This is what I've tried, but it's now wanted I want. And I don't have any solutions. Do you think you have some resources or code improvements for me?

val numViews = 8
        for (i in 0 until numViews) {
            // Create some quick TextViews that can be placed.
            val v = TextView(requireContext())
            // Set a text and center it in each view.
            v.text = "View $i"
            v.gravity = Gravity.CENTER
            v.setBackgroundColor(-0x10000)
            // Force the views to a nice size (150x100 px) that fits my display.
            // This should of course be done in a display size independent way.
            val lp = FrameLayout.LayoutParams(150, 250)
            // Place all views in the center of the layout. We'll transform them
            // away from there in the code below.
            lp.gravity = Gravity.CENTER
            // Set layout params on view.
            v.layoutParams = lp

            // Calculate the angle of the current view. Adjust by 90 degrees to
            // get View 0 at the top. We need the angle in degrees and radians.
            val angleDeg = i * 200.0f / numViews - 160.0f
            val angleRad = (angleDeg * Math.PI / 180.0f).toFloat()
            // Calculate the position of the view, offset from center (300 px from
            // center). Again, this should be done in a display size independent way.
            v.translationX = 300 * Math.cos(angleRad.toDouble()).toFloat()
            v.translationY = 300 * Math.sin(angleRad.toDouble()).toFloat()
            // Set the rotation of the view.
            v.rotation = angleDeg + 90.0f
            mBinding.main?.addView(v)
        }

Aucun commentaire:

Enregistrer un commentaire