0

i need to create a line with round dots. I try create drawable and set this drawable as background for View component. Bu currently i can create a line with square and I haven't idea how change my code to get rounded dots. Below is my code which i create line with Square.

`<layer-list>
  <item
    android:left = "-600dp"
    android:right = "-600dp">
     
     <rotate
       android:fromDeegres = "90"
       android:toDeegres = "90">

       <shape 
         android:share = "line" >

          <stroke 
            android:width = "3dp"
            android:dashWidth = "3dp"
            android:dashGap = "4dp"/>

        </shape>
       </rotate>
    </item>
</layer-list>`

I try create this using code above. And I need to create something like this enter image description here

Dourado
  • 110
  • 8
  • Does this answer your question? [How do I make a dotted/dashed line in Android?](https://stackoverflow.com/questions/6103713/how-do-i-make-a-dotted-dashed-line-in-android) – dominicoder Apr 26 '23 at 18:54

1 Answers1

0

you can use with library or customise the library code or you can directly use the classes as per your requirement

https://github.com/Comcast/DahDit

I hope it will work for you!!

Thankyou

 class DottedLine
@JvmOverloads constructor(context: Context?, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
    : View(context, attrs, defStyleAttr) {

    var dotRadius: Float
    var minimumDotGap: Float
    var orientation = Orientation.HORIZONTAL

    val paint: Paint = Paint()

    init {
        val metrics = resources.displayMetrics
        val twoDpDefault = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2f, metrics)
        val defaultBlack = Color.argb(255, 0, 0, 0)

        if (attrs != null) {
            val typedArray = context?.theme?.obtainStyledAttributes(attrs, R.styleable.DottedLine, defStyleAttr, 0)
            dotRadius = typedArray?.getDimension(R.styleable.DottedLine_dotRadius, twoDpDefault) ?: twoDpDefault
            minimumDotGap = typedArray?.getDimension(R.styleable.DottedLine_minimumDotGap, twoDpDefault) ?: twoDpDefault
            paint.color = typedArray?.getColor(R.styleable.DottedLine_dotColor, defaultBlack) ?: defaultBlack
            val orientationOrdinal = typedArray?.getInt(R.styleable.DottedLine_orientation, Orientation.HORIZONTAL.ordinal)
                    ?: Orientation.HORIZONTAL.ordinal
            if (orientationOrdinal == Orientation.VERTICAL.ordinal) {
                orientation = Orientation.VERTICAL
            } else {
                orientation = Orientation.HORIZONTAL
            }
            typedArray?.recycle()
        } else {
            dotRadius = twoDpDefault
            minimumDotGap = twoDpDefault
            paint.color = defaultBlack
        }

        paint.style = Paint.Style.FILL
        paint.flags = Paint.ANTI_ALIAS_FLAG
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        if (orientation == Orientation.HORIZONTAL) {
            val widthNeeded = paddingLeft + paddingRight + suggestedMinimumWidth
            val width = resolveSize(widthNeeded, widthMeasureSpec)

            val heightNeeded = paddingTop + paddingBottom + 2 * dotRadius
            val height = resolveSize(heightNeeded.toInt(), heightMeasureSpec)

            setMeasuredDimension(width, height)
        } else {
            val widthNeeded = paddingLeft + paddingRight + 2 * dotRadius
            val width = resolveSize(widthNeeded.toInt(), widthMeasureSpec)

            val heightNeeded = paddingTop + paddingBottom + suggestedMinimumHeight
            val height = resolveSize(heightNeeded, heightMeasureSpec)

            setMeasuredDimension(width, height)
        }
    }

    override fun onDraw(canvas: Canvas) {
        if (orientation == Orientation.HORIZONTAL) {
            val w = canvas.width - paddingLeft - paddingRight
            val d = 2 * dotRadius
            val m = minimumDotGap
            val c: Int = Math.floor(((w - d) / (d + m)).toDouble()).toInt()
            val g: Float = (w - (d * (c + 1))) / c
            for (i in 0..c) {
                canvas.drawCircle(paddingLeft + dotRadius + i * (d + g),
                        paddingTop + dotRadius,
                        dotRadius,
                        paint)
            }
        } else {
            val h = (canvas.height - paddingTop - paddingBottom).toFloat()
            val d = 2 * dotRadius
            val m = minimumDotGap
            val c: Int = Math.floor(((h - d) / (d + m)).toDouble()).toInt()
            val g: Float = (h - (d * (c + 1))) / c
            for (i in 0..c) {
                canvas.drawCircle(paddingLeft + dotRadius,
                        paddingTop + dotRadius + i * (d + g),
                        dotRadius,
                        paint)
            }
        }

    }

    enum class Orientation {
        HORIZONTAL, VERTICAL
    }
}

I have you the above class only from the above library for your excepted result!! You can take reference for the above library

Tanishq Chawda
  • 192
  • 3
  • 13