I'm using MPAndroidChart (A wonderfull chart library BTW), to set-up a Pie Chart like in the example given. The generated pie chart can be manually turned to perform rotations over his center. I am currently trying to listen to the rotation event and was wondering if there was a way of doing so using the library. I've unsuccessfully tried to use the following listener for that :
onDrawListener
setOnClickListener
setOnLongClickListener
addOnLayoutChangeListener
Thanks,
It is possible to read the current rotation angle. I run this command in a while(true) loop inside a coroutine with a delay of 10 ms.
GlobalScope.launch {
while (true) {
try {
val rotation = pieChart.rotationAngle
delay(10)
} catch (ex: Exception) {
// do nothing
}
}
}
It is an Easter Egg in my App and if a user spins the PieChart 10 times in 3 seconds I show something funny.
To check if the user did a full rotation I set checkpoints along the rotation and see if the user passed them. After 3 seconds all checkpoints will be reset.
if (rotating && rotation < 100 && checkpoints == 0) {
fullRotations += 1
checkpoints = 1
} else if (rotating && rotation > 100 && rotation < 200 && checkpoints == 1) {
checkpoints = 2
} else if (rotating && rotation > 200 && rotation < 360 && checkpoints == 2) {
checkpoints = 3
} else if (rotating && rotation < 100 && checkpoints == 3) {
fullRotations += 1
checkpoints = 1
}
One could use this code to create events out of it like -> do something if 'n' full rotations happened. As soon as you keep track of the change in rotation angle you can do what you want. If you have your event you could create a ListenerInterface and implement it in your class. Then you have your callback.
The while(true) maybe isn't the most elegant way but in my case it works pretty well. Tell me if you want my code. I didn't want to overload my answer.