|
37 | 37 | */ |
38 | 38 | public class Animator { |
39 | 39 | private long mNativeObject; |
40 | | - private Boolean mIsOwner = false; |
| 40 | + private boolean mIsOwner = false; |
41 | 41 |
|
42 | 42 | Animator(long nativeObject) { |
43 | 43 | mNativeObject = nativeObject; |
44 | 44 | } |
45 | 45 |
|
| 46 | + /** |
| 47 | + * Creates an Animator that can manipulate animations in the provided FilamentAsset. |
| 48 | + * <p> |
| 49 | + * <strong>Important:</strong> This Animator manages native resources that must be |
| 50 | + * explicitly freed by calling {@link #destroy()} when no longer needed. Failing to |
| 51 | + * call {@link #destroy()} will result in native memory leaks. |
| 52 | + * </p> |
| 53 | + * |
| 54 | + * @param asset The FilamentAsset containing the animations |
| 55 | + * @param instance The FilamentInstance to animate |
| 56 | + */ |
46 | 57 | public Animator(FilamentAsset asset, FilamentInstance instance) { |
47 | 58 | mNativeObject = nCreateAnimatorFromAssetAndInstance(asset.getNativeObject(), instance.getNativeObject()); |
48 | 59 | mIsOwner = true; |
49 | 60 | } |
50 | | - |
51 | | - @Override |
52 | | - public void finalize() { |
53 | | - try { |
54 | | - super.finalize(); |
55 | | - } catch (Throwable t) { // Ignore |
56 | | - } finally { |
57 | | - if (mIsOwner) { |
58 | | - nDestroyAnimator(mNativeObject); |
59 | | - mNativeObject = 0; |
60 | | - } |
61 | | - } |
62 | | - } |
63 | 61 |
|
64 | 62 | /** |
65 | 63 | * Applies rotation, translation, and scale to entities that have been targeted by the given |
@@ -145,6 +143,19 @@ public String getAnimationName(@IntRange(from = 0) int animationIndex) { |
145 | 143 | return nGetAnimationName(getNativeObject(), animationIndex); |
146 | 144 | } |
147 | 145 |
|
| 146 | + /** |
| 147 | + * Explicitly destroys this Animator and frees all its associated native resources. |
| 148 | + * The object will be unusable after this call. |
| 149 | + */ |
| 150 | + public void destroy() { |
| 151 | + if (mNativeObject != 0) { |
| 152 | + if (mIsOwner) { |
| 153 | + nDestroyAnimator(mNativeObject); |
| 154 | + } |
| 155 | + mNativeObject = 0; |
| 156 | + } |
| 157 | + } |
| 158 | + |
148 | 159 | long getNativeObject() { |
149 | 160 | if (mNativeObject == 0) { |
150 | 161 | throw new IllegalStateException("Using Animator on destroyed asset"); |
|
0 commit comments