Skip to content

Commit 8f8e2ae

Browse files
authored
Merge pull request #451 from AppDevNext/KotlinJobs
Kotlin jobs
2 parents aeb13df + 33a92cd commit 8f8e2ae

File tree

12 files changed

+442
-501
lines changed

12 files changed

+442
-501
lines changed

MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedMoveViewJob.java

Lines changed: 0 additions & 72 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.github.mikephil.charting.jobs
2+
3+
import android.animation.ValueAnimator
4+
import android.annotation.SuppressLint
5+
import android.view.View
6+
import com.github.mikephil.charting.utils.ObjectPool
7+
import com.github.mikephil.charting.utils.ObjectPool.Poolable
8+
import com.github.mikephil.charting.utils.Transformer
9+
import com.github.mikephil.charting.utils.ViewPortHandler
10+
11+
@SuppressLint("NewApi")
12+
class AnimatedMoveViewJob(
13+
viewPortHandler: ViewPortHandler?,
14+
xValue: Float,
15+
yValue: Float,
16+
trans: Transformer?,
17+
v: View?,
18+
xOrigin: Float,
19+
yOrigin: Float,
20+
duration: Long
21+
) : AnimatedViewPortJob(viewPortHandler, xValue, yValue, trans, v, xOrigin, yOrigin, duration) {
22+
override fun onAnimationUpdate(animation: ValueAnimator) {
23+
pts[0] = xOrigin + (xValue - xOrigin) * phase
24+
pts[1] = yOrigin + (yValue - yOrigin) * phase
25+
26+
transformer!!.pointValuesToPixel(pts)
27+
viewPortHandler!!.centerViewPort(pts, view!!)
28+
}
29+
30+
override fun recycleSelf() {
31+
recycleInstance(this)
32+
}
33+
34+
protected override fun instantiate(): Poolable {
35+
return AnimatedMoveViewJob(null, 0f, 0f, null, null, 0f, 0f, 0)
36+
}
37+
38+
companion object {
39+
private val pool: ObjectPool<AnimatedMoveViewJob>
40+
41+
init {
42+
pool = ObjectPool.create(4, AnimatedMoveViewJob(null, 0f, 0f, null, null, 0f, 0f, 0)) as ObjectPool<AnimatedMoveViewJob>
43+
pool.setReplenishPercentage(0.5f)
44+
}
45+
46+
@JvmStatic
47+
fun getInstance(
48+
viewPortHandler: ViewPortHandler?,
49+
xValue: Float,
50+
yValue: Float,
51+
trans: Transformer?,
52+
v: View?,
53+
xOrigin: Float,
54+
yOrigin: Float,
55+
duration: Long
56+
): AnimatedMoveViewJob {
57+
val result: AnimatedMoveViewJob = pool.get()
58+
result.viewPortHandler = viewPortHandler
59+
result.xValue = xValue
60+
result.yValue = yValue
61+
result.transformer = trans
62+
result.view = v
63+
result.xOrigin = xOrigin
64+
result.yOrigin = yOrigin
65+
//result.resetAnimator();
66+
result.animator.duration = duration
67+
return result
68+
}
69+
70+
fun recycleInstance(instance: AnimatedMoveViewJob) {
71+
// Clear reference avoid memory leak
72+
instance.xValue = 0f
73+
instance.yValue = 0f
74+
instance.xOrigin = 0f
75+
instance.yOrigin = 0f
76+
instance.animator.duration = 0
77+
instance.recycle()
78+
pool.recycle(instance)
79+
}
80+
}
81+
}

MPChartLib/src/main/java/com/github/mikephil/charting/jobs/AnimatedViewPortJob.java

Lines changed: 0 additions & 99 deletions
This file was deleted.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.github.mikephil.charting.jobs
2+
3+
import android.animation.Animator
4+
import android.animation.ObjectAnimator
5+
import android.animation.ValueAnimator
6+
import android.animation.ValueAnimator.AnimatorUpdateListener
7+
import android.annotation.SuppressLint
8+
import android.view.View
9+
import com.github.mikephil.charting.utils.Transformer
10+
import com.github.mikephil.charting.utils.ViewPortHandler
11+
12+
@SuppressLint("NewApi")
13+
abstract class AnimatedViewPortJob(
14+
viewPortHandler: ViewPortHandler?,
15+
xValue: Float,
16+
yValue: Float,
17+
trans: Transformer?,
18+
v: View?,
19+
var xOrigin: Float,
20+
var yOrigin: Float,
21+
duration: Long
22+
) : ViewPortJob(viewPortHandler, xValue, yValue, trans, v), AnimatorUpdateListener, Animator.AnimatorListener {
23+
protected var animator: ObjectAnimator = ObjectAnimator.ofFloat(this, "phase", 0f, 1f)
24+
25+
var phase: Float = 0f
26+
27+
init {
28+
animator.duration = duration
29+
animator.addUpdateListener(this)
30+
animator.addListener(this)
31+
}
32+
33+
@SuppressLint("NewApi")
34+
override fun run() {
35+
animator.start()
36+
}
37+
38+
abstract fun recycleSelf()
39+
40+
protected fun resetAnimator() {
41+
animator.removeAllListeners()
42+
animator.removeAllUpdateListeners()
43+
animator.reverse()
44+
animator.addUpdateListener(this)
45+
animator.addListener(this)
46+
}
47+
48+
override fun onAnimationStart(animation: Animator) {
49+
}
50+
51+
override fun onAnimationEnd(animation: Animator) {
52+
try {
53+
recycleSelf()
54+
} catch (_: IllegalArgumentException) {
55+
}
56+
}
57+
58+
override fun onAnimationCancel(animation: Animator) {
59+
try {
60+
recycleSelf()
61+
} catch (_: IllegalArgumentException) {
62+
}
63+
}
64+
65+
override fun onAnimationRepeat(animation: Animator) = Unit
66+
67+
override fun onAnimationUpdate(animation: ValueAnimator) = Unit
68+
}

0 commit comments

Comments
 (0)