From 9c3f0e714a05d5f69cef63b6208d026e2b4c0e3c Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Sat, 27 Dec 2025 08:54:23 +0100 Subject: [PATCH] Move to SizeF --- .../info/appdev/charting/components/Legend.kt | 12 +-- .../appdev/charting/components/MarkerImage.kt | 8 +- .../appdev/charting/renderer/XAxisRenderer.kt | 4 - .../XAxisRendererHorizontalBarChart.kt | 3 - .../info/appdev/charting/utils/CanvasUtils.kt | 8 +- .../info/appdev/charting/utils/FSize.kt | 76 ------------------- .../info/appdev/charting/utils/Utils.kt | 11 +-- .../info/appdev/charting/utils/UtilsKt.kt | 10 +-- 8 files changed, 23 insertions(+), 109 deletions(-) delete mode 100644 chartLib/src/main/kotlin/info/appdev/charting/utils/FSize.kt diff --git a/chartLib/src/main/kotlin/info/appdev/charting/components/Legend.kt b/chartLib/src/main/kotlin/info/appdev/charting/components/Legend.kt index 34f380b59..2dc46b195 100644 --- a/chartLib/src/main/kotlin/info/appdev/charting/components/Legend.kt +++ b/chartLib/src/main/kotlin/info/appdev/charting/components/Legend.kt @@ -2,8 +2,8 @@ package info.appdev.charting.components import android.graphics.DashPathEffect import android.graphics.Paint +import android.util.SizeF import info.appdev.charting.utils.ColorTemplate -import info.appdev.charting.utils.FSize import info.appdev.charting.utils.ViewPortHandler import info.appdev.charting.utils.calcTextHeight import info.appdev.charting.utils.calcTextWidth @@ -332,9 +332,9 @@ class Legend() : ComponentBase() { */ var isWordWrapEnabled: Boolean = false - val calculatedLabelSizes: MutableList = ArrayList(16) + val calculatedLabelSizes: MutableList = ArrayList(16) val calculatedLabelBreakPoints: MutableList = ArrayList(16) - val calculatedLineSizes: MutableList = ArrayList(16) + val calculatedLineSizes: MutableList = ArrayList(16) init { this.mTextSize = 10f.convertDpToPixel() @@ -456,7 +456,7 @@ class Legend() : ComponentBase() { requiredWidth += if (drawingForm) formToTextSpace + formSize else 0f requiredWidth += calculatedLabelSizes.get(i)!!.width } else { - calculatedLabelSizes.add(FSize.getInstance(0f, 0f)) + calculatedLabelSizes.add(SizeF(0f, 0f)) requiredWidth += if (drawingForm) formSize else 0f if (stackedStartIndex == -1) { @@ -480,7 +480,7 @@ class Legend() : ComponentBase() { // Add current line size to array - calculatedLineSizes.add(FSize.getInstance(currentLineWidth, labelLineHeight)) + calculatedLineSizes.add(SizeF(currentLineWidth, labelLineHeight)) maxLineWidth = max(maxLineWidth, currentLineWidth) // Start a new line @@ -495,7 +495,7 @@ class Legend() : ComponentBase() { if (i == entryCount - 1) { // Add last line size to array - calculatedLineSizes.add(FSize.getInstance(currentLineWidth, labelLineHeight)) + calculatedLineSizes.add(SizeF(currentLineWidth, labelLineHeight)) maxLineWidth = max(maxLineWidth, currentLineWidth) } } diff --git a/chartLib/src/main/kotlin/info/appdev/charting/components/MarkerImage.kt b/chartLib/src/main/kotlin/info/appdev/charting/components/MarkerImage.kt index e0a67a832..658e1f0c9 100644 --- a/chartLib/src/main/kotlin/info/appdev/charting/components/MarkerImage.kt +++ b/chartLib/src/main/kotlin/info/appdev/charting/components/MarkerImage.kt @@ -4,10 +4,10 @@ import android.content.Context import android.graphics.Canvas import android.graphics.Rect import android.graphics.drawable.Drawable +import android.util.SizeF import info.appdev.charting.charts.Chart import info.appdev.charting.data.Entry import info.appdev.charting.highlight.Highlight -import info.appdev.charting.utils.FSize import info.appdev.charting.utils.PointF import java.lang.ref.WeakReference @@ -21,7 +21,7 @@ class MarkerImage(private var mContext: Context, drawableResourceId: Int) : IMar private val mOffset2 = PointF() private var mWeakChart: WeakReference?>? = null - private var mSize: FSize? = FSize() + private var mSize: SizeF? = SizeF(0f, 0f) private val mDrawableBoundsCache = Rect() /** @@ -45,13 +45,13 @@ class MarkerImage(private var mContext: Context, drawableResourceId: Int) : IMar mOffset = offset } - var size: FSize? + var size: SizeF? get() = mSize set(size) { mSize = size if (mSize == null) { - mSize = FSize() + mSize = SizeF(0f, 0f) } } diff --git a/chartLib/src/main/kotlin/info/appdev/charting/renderer/XAxisRenderer.kt b/chartLib/src/main/kotlin/info/appdev/charting/renderer/XAxisRenderer.kt index fbd0df20b..6e5ec0881 100644 --- a/chartLib/src/main/kotlin/info/appdev/charting/renderer/XAxisRenderer.kt +++ b/chartLib/src/main/kotlin/info/appdev/charting/renderer/XAxisRenderer.kt @@ -12,7 +12,6 @@ import info.appdev.charting.components.LimitLine import info.appdev.charting.components.LimitLine.LimitLabelPosition import info.appdev.charting.components.XAxis import info.appdev.charting.components.XAxis.XAxisPosition -import info.appdev.charting.utils.FSize import info.appdev.charting.utils.PointD import info.appdev.charting.utils.PointF import info.appdev.charting.utils.Transformer @@ -86,9 +85,6 @@ open class XAxisRenderer( xAxis.mLabelWidth = labelRotatedSize.width.roundToInt() xAxis.mLabelHeight = labelRotatedSize.height.roundToInt() - - FSize.recycleInstance(labelRotatedSize) - FSize.recycleInstance(labelSize) } override fun renderAxisLabels(canvas: Canvas) { diff --git a/chartLib/src/main/kotlin/info/appdev/charting/renderer/XAxisRendererHorizontalBarChart.kt b/chartLib/src/main/kotlin/info/appdev/charting/renderer/XAxisRendererHorizontalBarChart.kt index 71c5884bc..95f74bbea 100644 --- a/chartLib/src/main/kotlin/info/appdev/charting/renderer/XAxisRendererHorizontalBarChart.kt +++ b/chartLib/src/main/kotlin/info/appdev/charting/renderer/XAxisRendererHorizontalBarChart.kt @@ -9,7 +9,6 @@ import androidx.core.graphics.withSave import info.appdev.charting.components.LimitLine.LimitLabelPosition import info.appdev.charting.components.XAxis import info.appdev.charting.components.XAxis.XAxisPosition -import info.appdev.charting.utils.FSize import info.appdev.charting.utils.PointD import info.appdev.charting.utils.PointF import info.appdev.charting.utils.Transformer @@ -72,8 +71,6 @@ open class XAxisRendererHorizontalBarChart( xAxis.mLabelWidth = labelRotatedSize.width.roundToInt() xAxis.mLabelHeight = labelRotatedSize.height.roundToInt() - - FSize.recycleInstance(labelRotatedSize) } override fun renderAxisLabels(canvas: Canvas) { diff --git a/chartLib/src/main/kotlin/info/appdev/charting/utils/CanvasUtils.kt b/chartLib/src/main/kotlin/info/appdev/charting/utils/CanvasUtils.kt index d4dd4b62e..86c54de4b 100644 --- a/chartLib/src/main/kotlin/info/appdev/charting/utils/CanvasUtils.kt +++ b/chartLib/src/main/kotlin/info/appdev/charting/utils/CanvasUtils.kt @@ -5,6 +5,7 @@ import android.graphics.Paint import android.graphics.Paint.Align import android.graphics.Rect import android.graphics.drawable.Drawable +import android.util.SizeF import info.appdev.charting.utils.Utils.FDEG2RAD import kotlin.math.abs import kotlin.math.cos @@ -88,7 +89,6 @@ fun Canvas.drawXAxisValue( translateX -= rotatedSize.width * (anchor.x - 0.5f) translateY -= rotatedSize.height * (anchor.y - 0.5f) - FSize.recycleInstance(rotatedSize) } this.save() @@ -130,7 +130,7 @@ fun Paint.calcTextWidth(demoText: String?): Int { * @param degrees * @return A Recyclable FSize instance */ -fun getSizeOfRotatedRectangleByDegrees(rectangleWidth: Float, rectangleHeight: Float, degrees: Float): FSize { +fun getSizeOfRotatedRectangleByDegrees(rectangleWidth: Float, rectangleHeight: Float, degrees: Float): SizeF { val radians = degrees * FDEG2RAD return getSizeOfRotatedRectangleByRadians(rectangleWidth, rectangleHeight, radians) } @@ -144,8 +144,8 @@ fun getSizeOfRotatedRectangleByDegrees(rectangleWidth: Float, rectangleHeight: F * @param radians * @return A Recyclable FSize instance */ -fun getSizeOfRotatedRectangleByRadians(rectangleWidth: Float, rectangleHeight: Float, radians: Float): FSize { - return FSize.getInstance( +fun getSizeOfRotatedRectangleByRadians(rectangleWidth: Float, rectangleHeight: Float, radians: Float): SizeF { + return SizeF( abs(rectangleWidth * cos(radians.toDouble()).toFloat()) + abs(rectangleHeight * sin(radians.toDouble()).toFloat()), abs(rectangleWidth * sin(radians.toDouble()).toFloat()) + abs(rectangleHeight * cos(radians.toDouble()).toFloat()) ) diff --git a/chartLib/src/main/kotlin/info/appdev/charting/utils/FSize.kt b/chartLib/src/main/kotlin/info/appdev/charting/utils/FSize.kt deleted file mode 100644 index 625943544..000000000 --- a/chartLib/src/main/kotlin/info/appdev/charting/utils/FSize.kt +++ /dev/null @@ -1,76 +0,0 @@ -package info.appdev.charting.utils - -import info.appdev.charting.utils.ObjectPool.Poolable -import kotlin.Any -import kotlin.Boolean -import kotlin.Int -import kotlin.String - -/** - * Class for describing width and height dimensions in some arbitrary - * unit. Replacement for the android.Util.SizeF which is available only on API >= 21. - */ -open class FSize : Poolable { - // TODO : Encapsulate width & height - var width: Float = 0f - var height: Float = 0f - - override fun instantiate(): FSize { - return FSize(0f, 0f) - } - - constructor() - - constructor(width: Float, height: Float) { - this.width = width - this.height = height - } - - override fun equals(other: Any?): Boolean { - if (other == null) { - return false - } - if (this === other) { - return true - } - if (other is FSize) { - return width == other.width && height == other.height - } - return false - } - - override fun toString(): String { - return width.toString() + "x" + height - } - - /** - * {@inheritDoc} - */ - override fun hashCode(): Int { - return width.toBits() xor height.toBits() - } - - companion object { - private val pool: ObjectPool = ObjectPool.create(256, FSize(0f, 0f)) - - init { - pool.replenishPercentage = 0.5f - } - - - fun getInstance(width: Float, height: Float): FSize { - val result: FSize = pool.get() - result.width = width - result.height = height - return result - } - - fun recycleInstance(instance: FSize) { - pool.recycle(instance) - } - - fun recycleInstances(instances: MutableList) { - pool.recycle(instances) - } - } -} diff --git a/chartLib/src/main/kotlin/info/appdev/charting/utils/Utils.kt b/chartLib/src/main/kotlin/info/appdev/charting/utils/Utils.kt index 0bdb44399..e1ce04fc5 100644 --- a/chartLib/src/main/kotlin/info/appdev/charting/utils/Utils.kt +++ b/chartLib/src/main/kotlin/info/appdev/charting/utils/Utils.kt @@ -4,16 +4,13 @@ import android.content.Context import android.graphics.Canvas import android.graphics.Rect import android.graphics.drawable.Drawable +import android.util.SizeF import android.view.MotionEvent import android.view.VelocityTracker import android.view.ViewConfiguration import info.appdev.charting.formatter.DefaultValueFormatter import info.appdev.charting.formatter.IValueFormatter import info.appdev.charting.utils.PointF.Companion.instance -import kotlin.Int -import kotlin.IntArray -import kotlin.Suppress -import kotlin.intArrayOf import kotlin.math.abs import kotlin.math.cos import kotlin.math.sin @@ -153,7 +150,7 @@ object Utils { * @param degrees * @return A Recyclable FSize instance */ - fun getSizeOfRotatedRectangleByDegrees(rectangleWidth: Float, rectangleHeight: Float, degrees: Float): FSize { + fun getSizeOfRotatedRectangleByDegrees(rectangleWidth: Float, rectangleHeight: Float, degrees: Float): SizeF { val radians = degrees * FDEG2RAD return getSizeOfRotatedRectangleByRadians(rectangleWidth, rectangleHeight, radians) } @@ -167,8 +164,8 @@ object Utils { * @param radians * @return A Recyclable FSize instance */ - fun getSizeOfRotatedRectangleByRadians(rectangleWidth: Float, rectangleHeight: Float, radians: Float): FSize { - return FSize.getInstance( + fun getSizeOfRotatedRectangleByRadians(rectangleWidth: Float, rectangleHeight: Float, radians: Float): SizeF { + return SizeF( abs(rectangleWidth * cos(radians.toDouble()).toFloat()) + abs(rectangleHeight * sin(radians.toDouble()).toFloat()), abs(rectangleWidth * sin(radians.toDouble()).toFloat()) + abs(rectangleHeight * cos(radians.toDouble()).toFloat()) ) diff --git a/chartLib/src/main/kotlin/info/appdev/charting/utils/UtilsKt.kt b/chartLib/src/main/kotlin/info/appdev/charting/utils/UtilsKt.kt index b6760d4d2..42aacc361 100644 --- a/chartLib/src/main/kotlin/info/appdev/charting/utils/UtilsKt.kt +++ b/chartLib/src/main/kotlin/info/appdev/charting/utils/UtilsKt.kt @@ -5,6 +5,7 @@ import android.graphics.Paint import android.graphics.Rect import android.os.Build import android.util.DisplayMetrics +import android.util.SizeF import android.view.ViewConfiguration import timber.log.Timber import kotlin.Boolean @@ -195,8 +196,8 @@ fun Paint.getLineSpacing(fontMetrics: Paint.FontMetrics): Float { * * @return A Recyclable FSize instance */ -fun Paint.calcTextSize(demoText: String): FSize { - val result = FSize.getInstance(0f, 0f) +fun Paint.calcTextSize(demoText: String): SizeF { + val result = SizeF(0f, 0f) calcTextSize(demoText, result) return result } @@ -209,10 +210,9 @@ private val mCalcTextSizeRect = Rect() * * @param outputFSize An output variable, modified by the function. */ -fun Paint.calcTextSize(demoText: String, outputFSize: FSize) { +fun Paint.calcTextSize(demoText: String, outputFSize: SizeF): SizeF { val r = mCalcTextSizeRect r.set(0, 0, 0, 0) this.getTextBounds(demoText, 0, demoText.length, r) - outputFSize.width = r.width().toFloat() - outputFSize.height = r.height().toFloat() + return SizeF(r.width().toFloat(),r.height().toFloat()) }