Home > Software engineering >  Is it possible to draw this shape on Android using only xml code?
Is it possible to draw this shape on Android using only xml code?

Time:01-30

enter image description here

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

CodePudding user response:

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!

CodePudding user response:

No, it is not possible to draw an arc or bow in XML in Android Studio. XML in Android is used for defining the layout of an activity or fragment, but does not support complex drawings or shapes. To create a custom shape or draw an arc or bow, you can use the Canvas class and override the onDraw() method in a custom View. This allows you to draw shapes programmatically and dynamically.

  • Related