Turok
Turok

Reputation: 49

Is it possible to draw this shape on Android using only xml code?

enter image description here

It is not a half-circle. It is an arc/bow.

Upvotes: 0

Views: 59

Answers (1)

Younes Charfaoui
Younes Charfaoui

Reputation: 1161

It isn't that straight forwad to design an arc in XML directly, I would maybe do that with a custom view like this one:

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF

import android.graphics.drawable.ShapeDrawable

class ArcShape(
  private val color: Int,
  private val startAngle: Float,
  private val sweepAngle: Float
) : ShapeDrawable() {
  private val rectF: RectF = RectF()
  private val shapePaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)

  init {
    shapePaint.color = color
    shapePaint.style = Paint.Style.STROKE
    shapePaint.strokeWidth = 5f
  }

  override fun draw(
    canvas: Canvas
  ) {
    canvas.drawArc(rectF, startAngle, sweepAngle, false, shapePaint)
  }

  override fun onBoundsChange(bounds: Rect) {
    super.onBoundsChange(bounds)
    rectF.set(bounds)
  }
}

The canvas is more flexible and allows you to design whatever you want! And you can do the same with Jetpack compose also!

Upvotes: 1

Related Questions