Skip to content

Commit 12e789c

Browse files
authored
Merge pull request #641 from AppDevNext/CanvasSave
Replace CanvasReplace.save()
2 parents c19722f + a5b9894 commit 12e789c

File tree

5 files changed

+134
-135
lines changed

5 files changed

+134
-135
lines changed

chartLib/src/main/kotlin/info/appdev/charting/components/MarkerImage.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import info.appdev.charting.highlight.Highlight
1010
import info.appdev.charting.utils.FSize
1111
import info.appdev.charting.utils.PointF
1212
import java.lang.ref.WeakReference
13+
import androidx.core.graphics.withTranslation
1314

1415
/**
1516
* View that can be displayed when selecting values in the chart. Extend this class to provide custom layouts for your markers.
@@ -119,11 +120,10 @@ class MarkerImage(private var mContext: Context, drawableResourceId: Int) : IMar
119120
mDrawableBoundsCache.top + height.toInt()
120121
)
121122

122-
val saveId = canvas.save()
123-
// translate to the correct position and draw
124-
canvas.translate(posX + offset.x, posY + offset.y)
125-
drawable!!.draw(canvas)
126-
canvas.restoreToCount(saveId)
123+
canvas.withTranslation(posX + offset.x, posY + offset.y) {
124+
// translate to the correct position and draw
125+
drawable!!.draw(canvas)
126+
}
127127

128128
drawable!!.bounds = mDrawableBoundsCache
129129
}

chartLib/src/main/kotlin/info/appdev/charting/components/MarkerView.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import info.appdev.charting.data.Entry
99
import info.appdev.charting.highlight.Highlight
1010
import info.appdev.charting.utils.PointF
1111
import java.lang.ref.WeakReference
12+
import androidx.core.graphics.withTranslation
1213

1314
/**
1415
* View that can be displayed when selecting values in the chart. Extend this class to provide custom layouts for your markers.
@@ -91,10 +92,9 @@ open class MarkerView(context: Context?, layoutResource: Int) : RelativeLayout(c
9192
override fun draw(canvas: Canvas, posX: Float, posY: Float) {
9293
val offset: PointF = getOffsetForDrawingAtPoint(posX, posY)
9394

94-
val saveId = canvas.save()
95-
// translate to the correct position and draw
96-
canvas.translate(posX + offset.x, posY + offset.y)
97-
draw(canvas)
98-
canvas.restoreToCount(saveId)
95+
canvas.withTranslation(posX + offset.x, posY + offset.y) {
96+
// translate to the correct position and draw
97+
draw(canvas)
98+
}
9999
}
100100
}

chartLib/src/main/kotlin/info/appdev/charting/renderer/PieChartRenderer.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -687,16 +687,16 @@ open class PieChartRenderer(
687687

688688
val layoutHeight = centerTextLayout!!.height.toFloat()
689689

690-
canvas.save()
691-
val path = mDrawCenterTextPathBuffer
692-
path.reset()
693-
path.addOval(holeRect, Path.Direction.CW)
694-
canvas.clipPath(path)
690+
canvas.withSave {
691+
val path = mDrawCenterTextPathBuffer
692+
path.reset()
693+
path.addOval(holeRect, Path.Direction.CW)
694+
clipPath(path)
695695

696-
canvas.translate(boundingRect.left, boundingRect.top + (boundingRect.height() - layoutHeight) / 2f)
697-
centerTextLayout!!.draw(canvas)
696+
translate(boundingRect.left, boundingRect.top + (boundingRect.height() - layoutHeight) / 2f)
697+
centerTextLayout!!.draw(this)
698698

699-
canvas.restore()
699+
}
700700

701701
PointF.recycleInstance(center)
702702
PointF.recycleInstance(offset)

chartLib/src/main/kotlin/info/appdev/charting/renderer/YAxisRenderer.kt

Lines changed: 109 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -228,28 +228,28 @@ open class YAxisRenderer(
228228
* Draws the zero line.
229229
*/
230230
protected open fun drawZeroLine(canvas: Canvas) {
231-
val clipRestoreCount = canvas.save()
232-
zeroLineClippingRect.set(viewPortHandler.contentRect)
233-
zeroLineClippingRect.inset(0f, -yAxis.zeroLineWidth)
234-
canvas.clipRect(zeroLineClippingRect)
231+
canvas.withSave {
232+
zeroLineClippingRect.set(viewPortHandler.contentRect)
233+
zeroLineClippingRect.inset(0f, -yAxis.zeroLineWidth)
234+
canvas.clipRect(zeroLineClippingRect)
235235

236-
// draw zero line
237-
val pos = transformer?.getPixelForValues(0f, 0f)
238-
pos?.let {
239-
zeroLinePaint.color = yAxis.zeroLineColor
240-
zeroLinePaint.strokeWidth = yAxis.zeroLineWidth
236+
// draw zero line
237+
val pos = transformer?.getPixelForValues(0f, 0f)
238+
pos?.let {
239+
zeroLinePaint.color = yAxis.zeroLineColor
240+
zeroLinePaint.strokeWidth = yAxis.zeroLineWidth
241241

242-
val zeroLinePath = drawZeroLinePath
243-
zeroLinePath.reset()
242+
val zeroLinePath = drawZeroLinePath
243+
zeroLinePath.reset()
244244

245-
zeroLinePath.moveTo(viewPortHandler.contentLeft(), it.y.toFloat())
246-
zeroLinePath.lineTo(viewPortHandler.contentRight(), it.y.toFloat())
245+
zeroLinePath.moveTo(viewPortHandler.contentLeft(), it.y.toFloat())
246+
zeroLinePath.lineTo(viewPortHandler.contentRight(), it.y.toFloat())
247247

248-
// draw a path because lines don't support dashing on lower android versions
249-
canvas.drawPath(zeroLinePath, zeroLinePaint)
250-
}
248+
// draw a path because lines don't support dashing on lower android versions
249+
canvas.drawPath(zeroLinePath, zeroLinePaint)
250+
}
251251

252-
canvas.restoreToCount(clipRestoreCount)
252+
}
253253
}
254254

255255
protected var renderLimitRanges: Path = Path()
@@ -384,105 +384,105 @@ open class YAxisRenderer(
384384
if (!limitRange.isEnabled)
385385
continue
386386

387-
val clipRestoreCount = canvas.save()
388-
limitLineClippingRect.set(viewPortHandler.contentRect)
389-
limitLineClippingRect.inset(0f, -limitRange.lineWidth)
390-
canvas.clipRect(limitLineClippingRect)
391-
392-
limitRangePaint.style = Paint.Style.STROKE
393-
limitRangePaint.color = limitRange.lineColor
394-
limitRangePaint.strokeWidth = limitRange.lineWidth
395-
limitRangePaint.pathEffect = limitRange.dashPathEffect
396-
397-
limitRangePaintFill.style = Paint.Style.FILL
398-
limitRangePaintFill.color = limitRange.rangeColor
399-
400-
ptsr[1] = limitRange.limit.high
401-
ptsr2[1] = limitRange.limit.low
402-
403-
transformer?.pointValuesToPixel(ptsr)
404-
transformer?.pointValuesToPixel(ptsr2)
405-
406-
limitRangePathFill.moveTo(viewPortHandler.contentLeft(), ptsr[1])
407-
limitRangePathFill.addRect(
408-
viewPortHandler.contentLeft(),
409-
ptsr[1],
410-
viewPortHandler.contentRight(),
411-
ptsr2[1],
412-
Path.Direction.CW
413-
)
414-
canvas.drawPath(limitRangePathFill, limitRangePaintFill)
415-
limitRangePathFill.reset()
416-
417-
if (limitRange.lineWidth > 0) {
418-
limitRangePath.moveTo(viewPortHandler.contentLeft(), ptsr[1])
419-
limitRangePath.lineTo(viewPortHandler.contentRight(), ptsr[1])
420-
canvas.drawPath(limitRangePath, limitRangePaint)
421-
422-
limitRangePath.moveTo(viewPortHandler.contentLeft(), ptsr2[1])
423-
limitRangePath.lineTo(viewPortHandler.contentRight(), ptsr2[1])
424-
canvas.drawPath(limitRangePath, limitRangePaint)
425-
}
387+
canvas.withSave {
388+
limitLineClippingRect.set(viewPortHandler.contentRect)
389+
limitLineClippingRect.inset(0f, -limitRange.lineWidth)
390+
canvas.clipRect(limitLineClippingRect)
426391

427-
limitRangePath.reset()
428-
429-
val label = limitRange.label
430-
431-
// if drawing the limit-value label is enabled
432-
if (label != null && label != "") {
433-
limitRangePaint.style = limitRange.textStyle
434-
limitRangePaint.pathEffect = null
435-
limitRangePaint.color = limitRange.textColor
436-
limitRangePaint.typeface = limitRange.typeface
437-
limitRangePaint.strokeWidth = 0.5f
438-
limitRangePaint.textSize = limitRange.textSize
439-
440-
val labelLineHeight = limitRangePaint.calcTextHeight(label).toFloat()
441-
val xOffset = 4f.convertDpToPixel() + limitRange.xOffset
442-
val yOffset = limitRange.lineWidth + labelLineHeight + limitRange.yOffset
443-
444-
val position = limitRange.labelPosition
445-
446-
when (position) {
447-
LimitLabelPosition.RIGHT_TOP -> {
448-
limitRangePaint.textAlign = Align.RIGHT
449-
canvas.drawText(
450-
label,
451-
viewPortHandler.contentRight() - xOffset,
452-
ptsr[1] - yOffset + labelLineHeight, limitRangePaint
453-
)
454-
}
392+
limitRangePaint.style = Paint.Style.STROKE
393+
limitRangePaint.color = limitRange.lineColor
394+
limitRangePaint.strokeWidth = limitRange.lineWidth
395+
limitRangePaint.pathEffect = limitRange.dashPathEffect
396+
397+
limitRangePaintFill.style = Paint.Style.FILL
398+
limitRangePaintFill.color = limitRange.rangeColor
399+
400+
ptsr[1] = limitRange.limit.high
401+
ptsr2[1] = limitRange.limit.low
402+
403+
transformer?.pointValuesToPixel(ptsr)
404+
transformer?.pointValuesToPixel(ptsr2)
405+
406+
limitRangePathFill.moveTo(viewPortHandler.contentLeft(), ptsr[1])
407+
limitRangePathFill.addRect(
408+
viewPortHandler.contentLeft(),
409+
ptsr[1],
410+
viewPortHandler.contentRight(),
411+
ptsr2[1],
412+
Path.Direction.CW
413+
)
414+
canvas.drawPath(limitRangePathFill, limitRangePaintFill)
415+
limitRangePathFill.reset()
416+
417+
if (limitRange.lineWidth > 0) {
418+
limitRangePath.moveTo(viewPortHandler.contentLeft(), ptsr[1])
419+
limitRangePath.lineTo(viewPortHandler.contentRight(), ptsr[1])
420+
canvas.drawPath(limitRangePath, limitRangePaint)
421+
422+
limitRangePath.moveTo(viewPortHandler.contentLeft(), ptsr2[1])
423+
limitRangePath.lineTo(viewPortHandler.contentRight(), ptsr2[1])
424+
canvas.drawPath(limitRangePath, limitRangePaint)
425+
}
455426

456-
LimitLabelPosition.RIGHT_BOTTOM -> {
457-
limitRangePaint.textAlign = Align.RIGHT
458-
canvas.drawText(
459-
label,
460-
viewPortHandler.contentRight() - xOffset,
461-
ptsr[1] + yOffset, limitRangePaint
462-
)
463-
}
427+
limitRangePath.reset()
464428

465-
LimitLabelPosition.LEFT_TOP -> {
466-
limitRangePaint.textAlign = Align.LEFT
467-
canvas.drawText(
468-
label,
469-
viewPortHandler.contentLeft() + xOffset,
470-
ptsr[1] - yOffset + labelLineHeight, limitRangePaint
471-
)
472-
}
429+
val label = limitRange.label
430+
431+
// if drawing the limit-value label is enabled
432+
if (label != null && label != "") {
433+
limitRangePaint.style = limitRange.textStyle
434+
limitRangePaint.pathEffect = null
435+
limitRangePaint.color = limitRange.textColor
436+
limitRangePaint.typeface = limitRange.typeface
437+
limitRangePaint.strokeWidth = 0.5f
438+
limitRangePaint.textSize = limitRange.textSize
439+
440+
val labelLineHeight = limitRangePaint.calcTextHeight(label).toFloat()
441+
val xOffset = 4f.convertDpToPixel() + limitRange.xOffset
442+
val yOffset = limitRange.lineWidth + labelLineHeight + limitRange.yOffset
443+
444+
val position = limitRange.labelPosition
445+
446+
when (position) {
447+
LimitLabelPosition.RIGHT_TOP -> {
448+
limitRangePaint.textAlign = Align.RIGHT
449+
canvas.drawText(
450+
label,
451+
viewPortHandler.contentRight() - xOffset,
452+
ptsr[1] - yOffset + labelLineHeight, limitRangePaint
453+
)
454+
}
455+
456+
LimitLabelPosition.RIGHT_BOTTOM -> {
457+
limitRangePaint.textAlign = Align.RIGHT
458+
canvas.drawText(
459+
label,
460+
viewPortHandler.contentRight() - xOffset,
461+
ptsr[1] + yOffset, limitRangePaint
462+
)
463+
}
473464

474-
else -> {
475-
limitRangePaint.textAlign = Align.LEFT
476-
canvas.drawText(
477-
label,
478-
viewPortHandler.offsetLeft() + xOffset,
479-
ptsr[1] + yOffset, limitRangePaint
480-
)
465+
LimitLabelPosition.LEFT_TOP -> {
466+
limitRangePaint.textAlign = Align.LEFT
467+
canvas.drawText(
468+
label,
469+
viewPortHandler.contentLeft() + xOffset,
470+
ptsr[1] - yOffset + labelLineHeight, limitRangePaint
471+
)
472+
}
473+
474+
else -> {
475+
limitRangePaint.textAlign = Align.LEFT
476+
canvas.drawText(
477+
label,
478+
viewPortHandler.offsetLeft() + xOffset,
479+
ptsr[1] + yOffset, limitRangePaint
480+
)
481+
}
481482
}
482483
}
483-
}
484484

485-
canvas.restoreToCount(clipRestoreCount)
485+
}
486486
}
487487
}
488488
}

chartLib/src/main/kotlin/info/appdev/charting/utils/Fill.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.graphics.RectF
88
import android.graphics.Shader
99
import android.graphics.drawable.Drawable
1010
import kotlin.math.floor
11+
import androidx.core.graphics.withClip
1112

1213
open class Fill {
1314
enum class Type {
@@ -90,12 +91,11 @@ open class Fill {
9091
}
9192

9293
if (this.isClipPathSupported) {
93-
val save = canvas.save()
94+
canvas.withClip(left, top, right, bottom) {
9495

95-
canvas.clipRect(left, top, right, bottom)
96-
canvas.drawColor(mFinalColor!!)
96+
canvas.drawColor(mFinalColor!!)
9797

98-
canvas.restoreToCount(save)
98+
}
9999
} else {
100100
// save
101101
val previous = paint.style
@@ -171,12 +171,11 @@ open class Fill {
171171
}
172172

173173
if (clipRect != null && this.isClipPathSupported) {
174-
val save = canvas.save()
174+
canvas.withClip(path) {
175175

176-
canvas.clipPath(path)
177-
canvas.drawColor(mFinalColor!!)
176+
canvas.drawColor(mFinalColor!!)
178177

179-
canvas.restoreToCount(save)
178+
}
180179
} else {
181180
// save
182181
val previous = paint.style

0 commit comments

Comments
 (0)