I’m confused if this is even the right way to do. Please correct me. So basically i have a text drawn in my custom view with canvas.drawText method. The text is a number (amount). I’m trying to make the text animate to another number (example: 100 => 800) when an event occurs.
I’m aware that this can be done with a for loop counter in onDraw method and calling canvas.drawText and invalidate() until it reaches the final number. But my app is stuck.
UPDATED code:
I have updated the code. Now the app doesn’t get stuck (as per Simon’s advice). But now it just draws all the numbers at one go (1-99). I dont know how to slow down the loop.
public class CustomView extends View implements OnTouchListener{
private Canvas main_canvas;
//...Other codes like, initialization, paint etc!...//
@Override
protected void onDraw(Canvas canvas) {
main_canvas = canvas;
}
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//...Some action...//
case MotionEvent.ACTION_MOVE:
//...Some action...//
case MotionEvent.ACTION_UP:
runTextAnimation();
}
}
private void runTextAnimation() {
for(int i=1; i < 100; i++){
main_canvas.drawText(String.valueOf(i), getMeasuredWidth()/2, 100, amountPaint);
invalidate();
}
}
}
I solved this by using a Runnable and using
postDelayedfor queueing the message for some milliseconds. Though I have to thank Simon for narrowing my search and thoughts. Below is the snippet. Hope this could help someone. Cheers.BTW the snippet above is an infinity counter, watchout to stop it at sometime.