Skip to content

Commit

Permalink
improve PieChart bitmap drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Oct 20, 2024
1 parent 15eb62d commit eeed1f1
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ class PieChartView @JvmOverloads constructor(
val res = mutableListOf<Arc>()
val valuesSum = data.map(PiePortion::value).sum()
var segmentPercent: Float
var drawable: Bitmap? = null
var drawable: Lazy<Bitmap>? = null

data.forEach { segment ->
if (drawIcons && segment.iconId != null) {
drawable = getIconDrawable(segment.iconId)
// Not every icon will be drown,
// using lazy to avoid unnecessary resource retrieval.
drawable = lazy { getIconDrawable(segment.iconId) }
}
segmentPercent = if (valuesSum != 0L) {
segment.value.toFloat() / valuesSum
Expand Down Expand Up @@ -447,7 +449,7 @@ class PieChartView @JvmOverloads constructor(
canvas.save()
canvas.translate(x.toFloat(), y.toFloat())
canvas.scale(scale.toFloat(), scale.toFloat())
canvas.drawBitmap(segment.drawable, null, particleBounds, particlePaint)
canvas.drawBitmap(segment.drawable.value, null, particleBounds, particlePaint)
canvas.restore()
}
}
Expand Down Expand Up @@ -486,7 +488,7 @@ class PieChartView @JvmOverloads constructor(
canvas.rotate(rotation)
canvas.translate(0f, -iconPositionFromCenter)
canvas.rotate(-rotation)
canvas.drawBitmap(it.drawable, null, bounds, null)
canvas.drawBitmap(it.drawable.value, null, bounds, null)

currentSweepAngle += sweepAngle
canvas.restoreToCount(center)
Expand Down Expand Up @@ -570,7 +572,7 @@ class PieChartView @JvmOverloads constructor(

private inner class Arc(
val color: Int,
val drawable: Bitmap? = null,
val drawable: Lazy<Bitmap>? = null,
val arcPercent: Float,
)

Expand Down

0 comments on commit eeed1f1

Please sign in to comment.