Преглед изворни кода

[android] Fixes #2682, remove frame callback if view is no longer attached to window.

Mario Zechner пре 7 месеци
родитељ
комит
8c37b4cc68

+ 15 - 2
spine-android/spine-android/src/main/java/com/esotericsoftware/spine/android/SpineView.java

@@ -181,6 +181,8 @@ public class SpineView extends View implements Choreographer.FrameCallback {
 	private Alignment alignment = Alignment.CENTER;
 	private Alignment alignment = Alignment.CENTER;
 	private ContentMode contentMode = ContentMode.FIT;
 	private ContentMode contentMode = ContentMode.FIT;
 
 
+	private boolean isAttached = false;
+
 	/** Constructs a new {@link SpineView}.
 	/** Constructs a new {@link SpineView}.
 	 *
 	 *
 	 * After initialization is complete, the provided {@code SpineController} is invoked as per the {@link SpineController}
 	 * After initialization is complete, the provided {@code SpineController} is invoked as per the {@link SpineController}
@@ -208,6 +210,18 @@ public class SpineView extends View implements Choreographer.FrameCallback {
 		// Set properties by view id
 		// Set properties by view id
 	}
 	}
 
 
+	@Override
+	protected void onAttachedToWindow () {
+		super.onAttachedToWindow();
+		isAttached = true;
+	}
+
+	@Override
+	protected void onDetachedFromWindow () {
+		super.onDetachedFromWindow();
+		isAttached = false;
+	}
+
 	/** Constructs a new {@link SpineView} from files in your app assets. The {@code atlasFileName} specifies the `.atlas` file to
 	/** Constructs a new {@link SpineView} from files in your app assets. The {@code atlasFileName} specifies the `.atlas` file to
 	 * be loaded for the images used to render the skeleton. The {@code skeletonFileName} specifies either a Skeleton `.json` or
 	 * be loaded for the images used to render the skeleton. The {@code skeletonFileName} specifies either a Skeleton `.json` or
 	 * `.skel` file containing the skeleton data.
 	 * `.skel` file containing the skeleton data.
@@ -407,10 +421,9 @@ public class SpineView extends View implements Choreographer.FrameCallback {
 		controller.setCoordinateTransform(x + offsetX / scaleX, y + offsetY / scaleY, scaleX, scaleY);
 		controller.setCoordinateTransform(x + offsetX / scaleX, y + offsetY / scaleY, scaleX, scaleY);
 	}
 	}
 
 
-	// Choreographer.FrameCallback
-
 	@Override
 	@Override
 	public void doFrame (long frameTimeNanos) {
 	public void doFrame (long frameTimeNanos) {
+		if (!isAttached) return;
 		if (lastTime != 0) delta = (frameTimeNanos - lastTime) / 1e9f;
 		if (lastTime != 0) delta = (frameTimeNanos - lastTime) / 1e9f;
 		lastTime = frameTimeNanos;
 		lastTime = frameTimeNanos;
 		invalidate();
 		invalidate();