Properly cancel looping before triggering another drawing loop

This commit is contained in:
Paul Rouget 2018-11-12 08:19:59 +01:00
parent e94d1c44ab
commit 6b2d9bf140

View file

@ -133,7 +133,7 @@ public class ServoView extends GLSurfaceView
public void animationStateChanged(boolean animating) { public void animationStateChanged(boolean animating) {
if (!mAnimating && animating) { if (!mAnimating && animating) {
post(() -> Choreographer.getInstance().postFrameCallback(ServoView.this)); post(() -> startLooping());
} }
mAnimating = animating; mAnimating = animating;
} }
@ -182,6 +182,13 @@ public class ServoView extends GLSurfaceView
mScroller = new OverScroller(context); mScroller = new OverScroller(context);
} }
private void startLooping() {
// In case we were already drawing.
Choreographer.getInstance().removeFrameCallback(this);
Choreographer.getInstance().postFrameCallback(this);
}
public void doFrame(long frameTimeNanos) { public void doFrame(long frameTimeNanos) {
if (!mRedrawing) { if (!mRedrawing) {
mRedrawing = true; mRedrawing = true;
@ -264,9 +271,10 @@ public class ServoView extends GLSurfaceView
mCurY = (int) e.getY(); mCurY = (int) e.getY();
mLastY = mCurY; mLastY = mCurY;
mScroller.forceFinished(true); mScroller.forceFinished(true);
mFlinging = false;
mServo.scrollStart(0, 0, mCurX, mCurY); mServo.scrollStart(0, 0, mCurX, mCurY);
mScrolling = true; mScrolling = true;
Choreographer.getInstance().postFrameCallback(this); startLooping();
return true; return true;
case (MotionEvent.ACTION_MOVE): case (MotionEvent.ACTION_MOVE):
mCurX = (int) e.getX(); mCurX = (int) e.getX();
@ -305,7 +313,7 @@ public class ServoView extends GLSurfaceView
mZoomFactor = detector.getScaleFactor(); mZoomFactor = detector.getScaleFactor();
mZooming = true; mZooming = true;
mServo.pinchZoomStart(mZoomFactor, 0, 0); mServo.pinchZoomStart(mZoomFactor, 0, 0);
Choreographer.getInstance().postFrameCallback(this); startLooping();
return true; return true;
} else { } else {
return false; return false;