From 33a92cde55fdbceb78c942c32af31aa27d1dcdb0 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Tue, 9 Dec 2025 07:22:48 +0100 Subject: [PATCH] Kotlin jobs --- .../charting/jobs/AnimatedMoveViewJob.java | 72 ---------- .../charting/jobs/AnimatedMoveViewJob.kt | 81 ++++++++++++ .../charting/jobs/AnimatedViewPortJob.java | 99 -------------- .../charting/jobs/AnimatedViewPortJob.kt | 68 ++++++++++ .../charting/jobs/AnimatedZoomJob.java | 123 ------------------ .../mikephil/charting/jobs/AnimatedZoomJob.kt | 120 +++++++++++++++++ .../mikephil/charting/jobs/MoveViewJob.java | 60 --------- .../mikephil/charting/jobs/MoveViewJob.kt | 51 ++++++++ .../mikephil/charting/jobs/ViewPortJob.java | 53 -------- .../mikephil/charting/jobs/ViewPortJob.kt | 43 ++++++ .../mikephil/charting/jobs/ZoomJob.java | 94 ------------- .../github/mikephil/charting/jobs/ZoomJob.kt | 79 +++++++++++ 12 files changed, 442 insertions(+), 501 deletions(-) delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.kt delete mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.java create mode 100644 MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.java b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.java deleted file mode 100644 index e6903cf854..0000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.github.mikephil.charting.jobs; - -import android.animation.ValueAnimator; -import android.annotation.SuppressLint; -import android.view.View; - -import com.github.mikephil.charting.utils.ObjectPool; -import com.github.mikephil.charting.utils.Transformer; -import com.github.mikephil.charting.utils.ViewPortHandler; - -/** - * Created by Philipp Jahoda on 19/02/16. - */ -@SuppressLint("NewApi") -public class AnimatedMoveViewJob extends AnimatedViewPortJob { - - private static ObjectPool pool; - - static { - pool = ObjectPool.create(4, new AnimatedMoveViewJob(null,0,0,null,null,0,0,0)); - pool.setReplenishPercentage(0.5f); - } - - public static AnimatedMoveViewJob getInstance(ViewPortHandler viewPortHandler, float xValue, float yValue, Transformer trans, View v, float xOrigin, float yOrigin, long duration){ - AnimatedMoveViewJob result = pool.get(); - result.mViewPortHandler = viewPortHandler; - result.xValue = xValue; - result.yValue = yValue; - result.mTrans = trans; - result.view = v; - result.xOrigin = xOrigin; - result.yOrigin = yOrigin; - //result.resetAnimator(); - result.animator.setDuration(duration); - return result; - } - - public static void recycleInstance(AnimatedMoveViewJob instance){ - // Clear reference avoid memory leak - instance.xValue = 0f; - instance.yValue = 0f; - instance.xOrigin = 0f; - instance.yOrigin = 0f; - instance.animator.setDuration(0); - instance.recycle(); - pool.recycle(instance); - } - - - public AnimatedMoveViewJob(ViewPortHandler viewPortHandler, float xValue, float yValue, Transformer trans, View v, float xOrigin, float yOrigin, long duration) { - super(viewPortHandler, xValue, yValue, trans, v, xOrigin, yOrigin, duration); - } - - @Override - public void onAnimationUpdate(ValueAnimator animation) { - - pts[0] = xOrigin + (xValue - xOrigin) * phase; - pts[1] = yOrigin + (yValue - yOrigin) * phase; - - mTrans.pointValuesToPixel(pts); - mViewPortHandler.centerViewPort(pts, view); - } - - public void recycleSelf(){ - recycleInstance(this); - } - - @Override - protected ObjectPool.Poolable instantiate() { - return new AnimatedMoveViewJob(null,0,0,null,null,0,0,0); - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt new file mode 100644 index 0000000000..08f02edf81 --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.kt @@ -0,0 +1,81 @@ +package com.github.mikephil.charting.jobs + +import android.animation.ValueAnimator +import android.annotation.SuppressLint +import android.view.View +import com.github.mikephil.charting.utils.ObjectPool +import com.github.mikephil.charting.utils.ObjectPool.Poolable +import com.github.mikephil.charting.utils.Transformer +import com.github.mikephil.charting.utils.ViewPortHandler + +@SuppressLint("NewApi") +class AnimatedMoveViewJob( + viewPortHandler: ViewPortHandler?, + xValue: Float, + yValue: Float, + trans: Transformer?, + v: View?, + xOrigin: Float, + yOrigin: Float, + duration: Long +) : AnimatedViewPortJob(viewPortHandler, xValue, yValue, trans, v, xOrigin, yOrigin, duration) { + override fun onAnimationUpdate(animation: ValueAnimator) { + pts[0] = xOrigin + (xValue - xOrigin) * phase + pts[1] = yOrigin + (yValue - yOrigin) * phase + + transformer!!.pointValuesToPixel(pts) + viewPortHandler!!.centerViewPort(pts, view!!) + } + + override fun recycleSelf() { + recycleInstance(this) + } + + protected override fun instantiate(): Poolable { + return AnimatedMoveViewJob(null, 0f, 0f, null, null, 0f, 0f, 0) + } + + companion object { + private val pool: ObjectPool + + init { + pool = ObjectPool.create(4, AnimatedMoveViewJob(null, 0f, 0f, null, null, 0f, 0f, 0)) as ObjectPool + pool.setReplenishPercentage(0.5f) + } + + @JvmStatic + fun getInstance( + viewPortHandler: ViewPortHandler?, + xValue: Float, + yValue: Float, + trans: Transformer?, + v: View?, + xOrigin: Float, + yOrigin: Float, + duration: Long + ): AnimatedMoveViewJob { + val result: AnimatedMoveViewJob = pool.get() + result.viewPortHandler = viewPortHandler + result.xValue = xValue + result.yValue = yValue + result.transformer = trans + result.view = v + result.xOrigin = xOrigin + result.yOrigin = yOrigin + //result.resetAnimator(); + result.animator.duration = duration + return result + } + + fun recycleInstance(instance: AnimatedMoveViewJob) { + // Clear reference avoid memory leak + instance.xValue = 0f + instance.yValue = 0f + instance.xOrigin = 0f + instance.yOrigin = 0f + instance.animator.duration = 0 + instance.recycle() + pool.recycle(instance) + } + } +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.java b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.java deleted file mode 100644 index f8b520a419..0000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.github.mikephil.charting.jobs; - -import android.animation.Animator; -import android.animation.ObjectAnimator; -import android.animation.ValueAnimator; -import android.annotation.SuppressLint; -import android.view.View; - -import com.github.mikephil.charting.utils.Transformer; -import com.github.mikephil.charting.utils.ViewPortHandler; - -/** - * Created by Philipp Jahoda on 19/02/16. - */ -@SuppressLint("NewApi") -public abstract class AnimatedViewPortJob extends ViewPortJob implements ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener { - - protected ObjectAnimator animator; - - protected float phase; - - protected float xOrigin; - protected float yOrigin; - - public AnimatedViewPortJob(ViewPortHandler viewPortHandler, float xValue, float yValue, Transformer trans, View v, float xOrigin, float yOrigin, long duration) { - super(viewPortHandler, xValue, yValue, trans, v); - this.xOrigin = xOrigin; - this.yOrigin = yOrigin; - animator = ObjectAnimator.ofFloat(this, "phase", 0f, 1f); - animator.setDuration(duration); - animator.addUpdateListener(this); - animator.addListener(this); - } - - @SuppressLint("NewApi") - @Override - public void run() { - animator.start(); - } - - public float getPhase() { - return phase; - } - - public void setPhase(float phase) { - this.phase = phase; - } - - public float getXOrigin() { - return xOrigin; - } - - public float getYOrigin() { - return yOrigin; - } - - public abstract void recycleSelf(); - - protected void resetAnimator(){ - animator.removeAllListeners(); - animator.removeAllUpdateListeners(); - animator.reverse(); - animator.addUpdateListener(this); - animator.addListener(this); - } - - @Override - public void onAnimationStart(Animator animation) { - - } - - @Override - public void onAnimationEnd(Animator animation) { - try{ - recycleSelf(); - }catch (IllegalArgumentException e){ - // don't worry about it. - } - } - - @Override - public void onAnimationCancel(Animator animation) { - try{ - recycleSelf(); - }catch (IllegalArgumentException e){ - // don't worry about it. - } - } - - @Override - public void onAnimationRepeat(Animator animation) { - - } - - @Override - public void onAnimationUpdate(ValueAnimator animation) { - - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt new file mode 100644 index 0000000000..daa1ca3382 --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.kt @@ -0,0 +1,68 @@ +package com.github.mikephil.charting.jobs + +import android.animation.Animator +import android.animation.ObjectAnimator +import android.animation.ValueAnimator +import android.animation.ValueAnimator.AnimatorUpdateListener +import android.annotation.SuppressLint +import android.view.View +import com.github.mikephil.charting.utils.Transformer +import com.github.mikephil.charting.utils.ViewPortHandler + +@SuppressLint("NewApi") +abstract class AnimatedViewPortJob( + viewPortHandler: ViewPortHandler?, + xValue: Float, + yValue: Float, + trans: Transformer?, + v: View?, + var xOrigin: Float, + var yOrigin: Float, + duration: Long +) : ViewPortJob(viewPortHandler, xValue, yValue, trans, v), AnimatorUpdateListener, Animator.AnimatorListener { + protected var animator: ObjectAnimator = ObjectAnimator.ofFloat(this, "phase", 0f, 1f) + + var phase: Float = 0f + + init { + animator.duration = duration + animator.addUpdateListener(this) + animator.addListener(this) + } + + @SuppressLint("NewApi") + override fun run() { + animator.start() + } + + abstract fun recycleSelf() + + protected fun resetAnimator() { + animator.removeAllListeners() + animator.removeAllUpdateListeners() + animator.reverse() + animator.addUpdateListener(this) + animator.addListener(this) + } + + override fun onAnimationStart(animation: Animator) { + } + + override fun onAnimationEnd(animation: Animator) { + try { + recycleSelf() + } catch (_: IllegalArgumentException) { + } + } + + override fun onAnimationCancel(animation: Animator) { + try { + recycleSelf() + } catch (_: IllegalArgumentException) { + } + } + + override fun onAnimationRepeat(animation: Animator) = Unit + + override fun onAnimationUpdate(animation: ValueAnimator) = Unit +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.java b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.java deleted file mode 100644 index d442a2ceeb..0000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.java +++ /dev/null @@ -1,123 +0,0 @@ -package com.github.mikephil.charting.jobs; - -import android.animation.Animator; -import android.animation.ValueAnimator; -import android.annotation.SuppressLint; -import android.graphics.Matrix; -import android.view.View; - -import com.github.mikephil.charting.charts.BarLineChartBase; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.utils.ObjectPool; -import com.github.mikephil.charting.utils.Transformer; -import com.github.mikephil.charting.utils.ViewPortHandler; - -/** - * Created by Philipp Jahoda on 19/02/16. - */ -@SuppressLint("NewApi") -public class AnimatedZoomJob extends AnimatedViewPortJob implements Animator.AnimatorListener { - - private static ObjectPool pool; - - static { - pool = ObjectPool.create(8, new AnimatedZoomJob(null,null,null,null,0,0,0,0,0,0,0,0,0,0)); - } - - public static AnimatedZoomJob getInstance(ViewPortHandler viewPortHandler, View v, Transformer trans, YAxis axis, float xAxisRange, float scaleX, float scaleY, float xOrigin, float yOrigin, float zoomCenterX, float zoomCenterY, float zoomOriginX, float zoomOriginY, long duration) { - AnimatedZoomJob result = pool.get(); - result.mViewPortHandler = viewPortHandler; - result.xValue = scaleX; - result.yValue = scaleY; - result.mTrans = trans; - result.view = v; - result.xOrigin = xOrigin; - result.yOrigin = yOrigin; - result.yAxis = axis; - result.xAxisRange = xAxisRange; - result.zoomCenterX = zoomCenterX; - result.zoomCenterY = zoomCenterY; - result.zoomOriginX = zoomOriginX; - result.zoomOriginY = zoomOriginY; - result.resetAnimator(); - result.animator.setDuration(duration); - return result; - } - - protected float zoomOriginX; - protected float zoomOriginY; - - protected float zoomCenterX; - protected float zoomCenterY; - - protected YAxis yAxis; - - protected float xAxisRange; - - @SuppressLint("NewApi") - public AnimatedZoomJob(ViewPortHandler viewPortHandler, View v, Transformer trans, YAxis axis, float xAxisRange, float scaleX, float scaleY, float xOrigin, float yOrigin, float zoomCenterX, float zoomCenterY, float zoomOriginX, float zoomOriginY, long duration) { - super(viewPortHandler, scaleX, scaleY, trans, v, xOrigin, yOrigin, duration); - - this.zoomCenterX = zoomCenterX; - this.zoomCenterY = zoomCenterY; - this.zoomOriginX = zoomOriginX; - this.zoomOriginY = zoomOriginY; - this.animator.addListener(this); - this.yAxis = axis; - this.xAxisRange = xAxisRange; - } - - protected Matrix mOnAnimationUpdateMatrixBuffer = new Matrix(); - @Override - public void onAnimationUpdate(ValueAnimator animation) { - - float scaleX = xOrigin + (xValue - xOrigin) * phase; - float scaleY = yOrigin + (yValue - yOrigin) * phase; - - Matrix save = mOnAnimationUpdateMatrixBuffer; - mViewPortHandler.setZoom(scaleX, scaleY, save); - mViewPortHandler.refresh(save, view, false); - - float valsInView = yAxis.mAxisRange / mViewPortHandler.getScaleY(); - float xsInView = xAxisRange / mViewPortHandler.getScaleX(); - - pts[0] = zoomOriginX + ((zoomCenterX - xsInView / 2f) - zoomOriginX) * phase; - pts[1] = zoomOriginY + ((zoomCenterY + valsInView / 2f) - zoomOriginY) * phase; - - mTrans.pointValuesToPixel(pts); - - mViewPortHandler.translate(pts, save); - mViewPortHandler.refresh(save, view, true); - } - - @Override - public void onAnimationEnd(Animator animation) { - ((BarLineChartBase) view).calculateOffsets(); - view.postInvalidate(); - } - - @Override - public void onAnimationCancel(Animator animation) { - - } - - @Override - public void onAnimationRepeat(Animator animation) { - - } - - @Override - public void recycleSelf() { - - } - - @Override - public void onAnimationStart(Animator animation) { - - } - - @Override - protected ObjectPool.Poolable instantiate() { - return new AnimatedZoomJob(null,null,null,null,0,0,0,0,0,0,0,0,0,0); - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt new file mode 100644 index 0000000000..89b239931e --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedZoomJob.kt @@ -0,0 +1,120 @@ +package com.github.mikephil.charting.jobs + +import android.animation.Animator +import android.animation.ValueAnimator +import android.annotation.SuppressLint +import android.graphics.Matrix +import android.view.View +import com.github.mikephil.charting.charts.BarLineChartBase +import com.github.mikephil.charting.components.YAxis +import com.github.mikephil.charting.utils.ObjectPool +import com.github.mikephil.charting.utils.ObjectPool.Poolable +import com.github.mikephil.charting.utils.Transformer +import com.github.mikephil.charting.utils.ViewPortHandler + +@SuppressLint("NewApi") +open class AnimatedZoomJob @SuppressLint("NewApi") constructor( + viewPortHandler: ViewPortHandler?, + v: View?, + trans: Transformer?, + axis: YAxis?, + xAxisRange: Float, + scaleX: Float, + scaleY: Float, + xOrigin: Float, + yOrigin: Float, + protected var zoomCenterX: Float, + protected var zoomCenterY: Float, + protected var zoomOriginX: Float, + protected var zoomOriginY: Float, + duration: Long +) : AnimatedViewPortJob(viewPortHandler, scaleX, scaleY, trans, v, xOrigin, yOrigin, duration), Animator.AnimatorListener { + protected var yAxis: YAxis? = null + + protected var xAxisRange: Float + + protected var mOnAnimationUpdateMatrixBuffer: Matrix = Matrix() + + init { + this.animator.addListener(this) + this.yAxis = axis + this.xAxisRange = xAxisRange + } + + override fun onAnimationUpdate(animation: ValueAnimator) { + val scaleX = xOrigin + (xValue - xOrigin) * phase + val scaleY = yOrigin + (yValue - yOrigin) * phase + + val save = mOnAnimationUpdateMatrixBuffer + viewPortHandler!!.setZoom(scaleX, scaleY, save) + viewPortHandler!!.refresh(save, view!!, false) + + val valsInView = yAxis?.let { it.mAxisRange / viewPortHandler!!.scaleY } + val xsInView = xAxisRange / viewPortHandler!!.scaleX + + pts[0] = zoomOriginX + ((zoomCenterX - xsInView / 2f) - zoomOriginX) * phase + valsInView?.let { pts[1] = zoomOriginY + ((zoomCenterY + it / 2f) - zoomOriginY) * phase } + + transformer!!.pointValuesToPixel(pts) + + viewPortHandler!!.translate(pts, save) + viewPortHandler!!.refresh(save, view!!, true) + } + + override fun onAnimationEnd(animation: Animator) { + (view as BarLineChartBase<*>).calculateOffsets() + view!!.postInvalidate() + } + + override fun onAnimationCancel(animation: Animator) = Unit + + override fun onAnimationRepeat(animation: Animator) = Unit + + override fun recycleSelf() = Unit + + override fun onAnimationStart(animation: Animator) = Unit + protected override fun instantiate(): Poolable { + return AnimatedZoomJob(null, null, null, null, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0) + } + + companion object { + private val pool: ObjectPool = + ObjectPool.create(8, AnimatedZoomJob(null, null, null, null, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0)) as ObjectPool + + @JvmStatic + fun getInstance( + viewPortHandler: ViewPortHandler?, + v: View?, + trans: Transformer?, + axis: YAxis, + xAxisRange: Float, + scaleX: Float, + scaleY: Float, + xOrigin: Float, + yOrigin: Float, + zoomCenterX: Float, + zoomCenterY: Float, + zoomOriginX: Float, + zoomOriginY: Float, + duration: Long + ): AnimatedZoomJob { + val result: AnimatedZoomJob = pool.get() + result.viewPortHandler = viewPortHandler + result.xValue = scaleX + result.yValue = scaleY + result.transformer = trans + result.view = v + result.xOrigin = xOrigin + result.yOrigin = yOrigin + result.yAxis = axis + result.xAxisRange = xAxisRange + result.zoomCenterX = zoomCenterX + result.zoomCenterY = zoomCenterY + result.zoomOriginX = zoomOriginX + result.zoomOriginY = zoomOriginY + result.resetAnimator() + result.animator.setDuration(duration) + return result + } + } +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.java b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.java deleted file mode 100644 index e24d746f4a..0000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.java +++ /dev/null @@ -1,60 +0,0 @@ - -package com.github.mikephil.charting.jobs; - -import android.view.View; - -import com.github.mikephil.charting.utils.ObjectPool; -import com.github.mikephil.charting.utils.Transformer; -import com.github.mikephil.charting.utils.ViewPortHandler; - -/** - * Created by Philipp Jahoda on 19/02/16. - */ -public class MoveViewJob extends ViewPortJob { - - private static ObjectPool pool; - - static { - pool = ObjectPool.create(2, new MoveViewJob(null,0,0,null,null)); - pool.setReplenishPercentage(0.5f); - } - - public static MoveViewJob getInstance(ViewPortHandler viewPortHandler, float xValue, float yValue, Transformer trans, View v){ - MoveViewJob result = pool.get(); - result.mViewPortHandler = viewPortHandler; - result.xValue = xValue; - result.yValue = yValue; - result.mTrans = trans; - result.view = v; - return result; - } - - public static void recycleInstance(MoveViewJob instance){ - instance.recycle(); - // Clear reference avoid memory leak - instance.xValue = 0f; - instance.yValue = 0f; - pool.recycle(instance); - } - - public MoveViewJob(ViewPortHandler viewPortHandler, float xValue, float yValue, Transformer trans, View v) { - super(viewPortHandler, xValue, yValue, trans, v); - } - - @Override - public void run() { - - pts[0] = xValue; - pts[1] = yValue; - - mTrans.pointValuesToPixel(pts); - mViewPortHandler.centerViewPort(pts, view); - - this.recycleInstance(this); - } - - @Override - protected ObjectPool.Poolable instantiate() { - return new MoveViewJob(mViewPortHandler, xValue, yValue, mTrans, view); - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt new file mode 100644 index 0000000000..202f2c1cf4 --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/MoveViewJob.kt @@ -0,0 +1,51 @@ +package com.github.mikephil.charting.jobs + +import android.view.View +import com.github.mikephil.charting.utils.ObjectPool +import com.github.mikephil.charting.utils.ObjectPool.Poolable +import com.github.mikephil.charting.utils.Transformer +import com.github.mikephil.charting.utils.ViewPortHandler + +class MoveViewJob(viewPortHandler: ViewPortHandler?, xValue: Float, yValue: Float, trans: Transformer?, v: View?) : + ViewPortJob(viewPortHandler, xValue, yValue, trans, v) { + override fun run() { + pts[0] = xValue + pts[1] = yValue + + transformer!!.pointValuesToPixel(pts) + viewPortHandler!!.centerViewPort(pts, view!!) + + recycleInstance(this) + } + + protected override fun instantiate(): Poolable { + return MoveViewJob(viewPortHandler, xValue, yValue, transformer, view) + } + + companion object { + private val pool: ObjectPool = ObjectPool.create(2, MoveViewJob(null, 0f, 0f, null, null)) as ObjectPool + + init { + pool.setReplenishPercentage(0.5f) + } + + @JvmStatic + fun getInstance(viewPortHandler: ViewPortHandler?, xValue: Float, yValue: Float, trans: Transformer?, v: View?): MoveViewJob { + val result: MoveViewJob = pool.get() + result.viewPortHandler = viewPortHandler + result.xValue = xValue + result.yValue = yValue + result.transformer = trans + result.view = v + return result + } + + fun recycleInstance(instance: MoveViewJob) { + instance.recycle() + // Clear reference avoid memory leak + instance.xValue = 0f + instance.yValue = 0f + pool.recycle(instance) + } + } +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.java b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.java deleted file mode 100644 index a1df37425b..0000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.java +++ /dev/null @@ -1,53 +0,0 @@ - -package com.github.mikephil.charting.jobs; - -import android.view.View; - -import com.github.mikephil.charting.utils.ObjectPool; -import com.github.mikephil.charting.utils.Transformer; -import com.github.mikephil.charting.utils.ViewPortHandler; - -/** - * Runnable that is used for viewport modifications since they cannot be - * executed at any time. This can be used to delay the execution of viewport - * modifications until the onSizeChanged(...) method of the chart-view is called. - * This is especially important if viewport modifying methods are called on the chart - * directly after initialization. - * - * @author Philipp Jahoda - */ -public abstract class ViewPortJob extends ObjectPool.Poolable implements Runnable { - - protected float[] pts = new float[2]; - - protected ViewPortHandler mViewPortHandler; - protected float xValue = 0f; - protected float yValue = 0f; - protected Transformer mTrans; - protected View view; - - public ViewPortJob(ViewPortHandler viewPortHandler, float xValue, float yValue, - Transformer trans, View v) { - - this.mViewPortHandler = viewPortHandler; - this.xValue = xValue; - this.yValue = yValue; - this.mTrans = trans; - this.view = v; - - } - - public float getXValue() { - return xValue; - } - - public float getYValue() { - return yValue; - } - - protected void recycle() { - mViewPortHandler = null; - mTrans = null; - view = null; - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.kt new file mode 100644 index 0000000000..b8bf0b9d53 --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ViewPortJob.kt @@ -0,0 +1,43 @@ +package com.github.mikephil.charting.jobs + +import android.view.View +import com.github.mikephil.charting.utils.ObjectPool.Poolable +import com.github.mikephil.charting.utils.Transformer +import com.github.mikephil.charting.utils.ViewPortHandler + +/** + * Runnable that is used for viewport modifications since they cannot be + * executed at any time. This can be used to delay the execution of viewport + * modifications until the onSizeChanged(...) method of the chart-view is called. + * This is especially important if viewport modifying methods are called on the chart + * directly after initialization. + */ +abstract class ViewPortJob( + @JvmField protected var viewPortHandler: ViewPortHandler?, xValue: Float, yValue: Float, + trans: Transformer?, v: View? +) : Poolable(), Runnable { + @JvmField + protected var pts: FloatArray = FloatArray(2) + + var xValue: Float = 0f + protected set + var yValue: Float = 0f + protected set + @JvmField + protected var transformer: Transformer? + @JvmField + protected var view: View? + + init { + this.xValue = xValue + this.yValue = yValue + this.transformer = trans + this.view = v + } + + protected fun recycle() { + viewPortHandler = null + transformer = null + view = null + } +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.java b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.java deleted file mode 100644 index 8c4c74df96..0000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.java +++ /dev/null @@ -1,94 +0,0 @@ - -package com.github.mikephil.charting.jobs; - -import android.graphics.Matrix; -import android.view.View; - -import com.github.mikephil.charting.charts.BarLineChartBase; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.utils.ObjectPool; -import com.github.mikephil.charting.utils.Transformer; -import com.github.mikephil.charting.utils.ViewPortHandler; - -/** - * Created by Philipp Jahoda on 19/02/16. - */ -public class ZoomJob extends ViewPortJob { - - private static ObjectPool pool; - - static { - pool = ObjectPool.create(1, new ZoomJob(null, 0, 0, 0, 0, null, null, null)); - pool.setReplenishPercentage(0.5f); - } - - public static ZoomJob getInstance(ViewPortHandler viewPortHandler, float scaleX, float scaleY, float xValue, float yValue, - Transformer trans, YAxis.AxisDependency axis, View v) { - ZoomJob result = pool.get(); - result.xValue = xValue; - result.yValue = yValue; - result.scaleX = scaleX; - result.scaleY = scaleY; - result.mViewPortHandler = viewPortHandler; - result.mTrans = trans; - result.axisDependency = axis; - result.view = v; - return result; - } - - public static void recycleInstance(ZoomJob instance) { - // Clear reference avoid memory leak - instance.xValue = 0f; - instance.yValue = 0f; - instance.scaleX = 0f; - instance.scaleY = 0f; - instance.axisDependency = null; - instance.recycle(); - pool.recycle(instance); - } - - protected float scaleX; - protected float scaleY; - - protected YAxis.AxisDependency axisDependency; - - public ZoomJob(ViewPortHandler viewPortHandler, float scaleX, float scaleY, float xValue, float yValue, Transformer trans, - YAxis.AxisDependency axis, View v) { - super(viewPortHandler, xValue, yValue, trans, v); - - this.scaleX = scaleX; - this.scaleY = scaleY; - this.axisDependency = axis; - } - - protected Matrix mRunMatrixBuffer = new Matrix(); - - @Override - public void run() { - - Matrix save = mRunMatrixBuffer; - mViewPortHandler.zoom(scaleX, scaleY, save); - mViewPortHandler.refresh(save, view, false); - - float yValsInView = ((BarLineChartBase) view).getAxis(axisDependency).mAxisRange / mViewPortHandler.getScaleY(); - float xValsInView = ((BarLineChartBase) view).getXAxis().mAxisRange / mViewPortHandler.getScaleX(); - - pts[0] = xValue - xValsInView / 2f; - pts[1] = yValue + yValsInView / 2f; - - mTrans.pointValuesToPixel(pts); - - mViewPortHandler.translate(pts, save); - mViewPortHandler.refresh(save, view, false); - - ((BarLineChartBase) view).calculateOffsets(); - view.postInvalidate(); - - recycleInstance(this); - } - - @Override - protected ObjectPool.Poolable instantiate() { - return new ZoomJob(null, 0, 0, 0, 0, null, null, null); - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt new file mode 100644 index 0000000000..32bc50b1dd --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.kt @@ -0,0 +1,79 @@ +package com.github.mikephil.charting.jobs + +import android.graphics.Matrix +import android.view.View +import com.github.mikephil.charting.charts.BarLineChartBase +import com.github.mikephil.charting.components.YAxis.AxisDependency +import com.github.mikephil.charting.utils.ObjectPool +import com.github.mikephil.charting.utils.ObjectPool.Poolable +import com.github.mikephil.charting.utils.Transformer +import com.github.mikephil.charting.utils.ViewPortHandler + +class ZoomJob( + viewPortHandler: ViewPortHandler?, protected var scaleX: Float, protected var scaleY: Float, xValue: Float, yValue: Float, trans: Transformer?, + protected var axisDependency: AxisDependency?, v: View? +) : ViewPortJob(viewPortHandler, xValue, yValue, trans, v) { + protected var mRunMatrixBuffer: Matrix = Matrix() + + override fun run() { + val save = mRunMatrixBuffer + viewPortHandler!!.zoom(scaleX, scaleY, save) + viewPortHandler!!.refresh(save, view!!, false) + + val yValsInView = (view as BarLineChartBase<*>).getAxis(axisDependency).mAxisRange / viewPortHandler!!.scaleY + val xValsInView = (view as BarLineChartBase<*>).getXAxis().mAxisRange / viewPortHandler!!.scaleX + + pts[0] = xValue - xValsInView / 2f + pts[1] = yValue + yValsInView / 2f + + transformer!!.pointValuesToPixel(pts) + + viewPortHandler!!.translate(pts, save) + viewPortHandler!!.refresh(save, view!!, false) + + (view as BarLineChartBase<*>).calculateOffsets() + view!!.postInvalidate() + + recycleInstance(this) + } + + protected override fun instantiate(): Poolable { + return ZoomJob(null, 0f, 0f, 0f, 0f, null, null, null) + } + + companion object { + private val pool: ObjectPool = ObjectPool.create(1, ZoomJob(null, 0f, 0f, 0f, 0f, null, null, null)) as ObjectPool + + init { + pool.setReplenishPercentage(0.5f) + } + + @JvmStatic + fun getInstance( + viewPortHandler: ViewPortHandler?, scaleX: Float, scaleY: Float, xValue: Float, yValue: Float, + trans: Transformer?, axis: AxisDependency?, v: View? + ): ZoomJob { + val result: ZoomJob = pool.get() + result.xValue = xValue + result.yValue = yValue + result.scaleX = scaleX + result.scaleY = scaleY + result.viewPortHandler = viewPortHandler + result.transformer = trans + result.axisDependency = axis + result.view = v + return result + } + + fun recycleInstance(instance: ZoomJob) { + // Clear reference avoid memory leak + instance.xValue = 0f + instance.yValue = 0f + instance.scaleX = 0f + instance.scaleY = 0f + instance.axisDependency = null + instance.recycle() + pool.recycle(instance) + } + } +}