android: fix when animator cannot be released

This commit is contained in:
pengfei.zhou 2021-12-24 10:21:26 +08:00 committed by osborn
parent c6d26c1f49
commit 48afc3acb3
2 changed files with 25 additions and 5 deletions

View File

@ -15,6 +15,7 @@
*/ */
package pub.doric; package pub.doric;
import android.animation.Animator;
import android.animation.AnimatorSet; import android.animation.AnimatorSet;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -32,6 +33,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
@ -62,6 +64,7 @@ public class DoricContext {
private IDoricDriver doricDriver; private IDoricDriver doricDriver;
private final Map<String, Map<String, ViewNode<?>>> mHeadNodes = new HashMap<>(); private final Map<String, Map<String, ViewNode<?>>> mHeadNodes = new HashMap<>();
private final DoricPerformanceProfile performanceProfile; private final DoricPerformanceProfile performanceProfile;
private final Map<String, Animator> animators = new HashMap<>();
public Collection<ViewNode<?>> allHeadNodes(String type) { public Collection<ViewNode<?>> allHeadNodes(String type) {
Map<String, ViewNode<?>> headNode = mHeadNodes.get(type); Map<String, ViewNode<?>> headNode = mHeadNodes.get(type);
@ -187,6 +190,10 @@ public class DoricContext {
} }
public void teardown() { public void teardown() {
for (Animator animator : animators.values()) {
animator.cancel();
}
animators.clear();
callEntity(DoricConstant.DORIC_ENTITY_DESTROY).setCallback(new AsyncResult.Callback<JSDecoder>() { callEntity(DoricConstant.DORIC_ENTITY_DESTROY).setCallback(new AsyncResult.Callback<JSDecoder>() {
@Override @Override
public void onResult(JSDecoder result) { public void onResult(JSDecoder result) {
@ -332,4 +339,16 @@ public class DoricContext {
} }
} }
} }
public void putAnimator(String animatorId, Animator animator) {
animators.put(animatorId, animator);
}
public Animator getAnimator(String animatorId) {
return animators.get(animatorId);
}
public void removeAnimator(String animatorId) {
animators.remove(animatorId);
}
} }

View File

@ -80,7 +80,6 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
protected ViewGroup.LayoutParams mLayoutParams; protected ViewGroup.LayoutParams mLayoutParams;
private String mType; private String mType;
protected JSObject mFlexConfig; protected JSObject mFlexConfig;
private final WeakHashMap<String, Animator> animators = new WeakHashMap<>();
public JSObject getFlexConfig() { public JSObject getFlexConfig() {
return mFlexConfig; return mFlexConfig;
@ -942,18 +941,20 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
public void doAnimation(JSValue value, final DoricPromise promise) { public void doAnimation(JSValue value, final DoricPromise promise) {
Animator animator = parseAnimator(value); Animator animator = parseAnimator(value);
if (animator != null) { if (animator != null) {
String animatorId = value.asObject().getProperty("id").asString().value(); final String animatorId = value.asObject().getProperty("id").asString().value();
animators.put(animatorId, animator); getDoricContext().putAnimator(animatorId, animator);
animator.addListener(new AnimatorListenerAdapter() { animator.addListener(new AnimatorListenerAdapter() {
@Override @Override
public void onAnimationCancel(Animator animation) { public void onAnimationCancel(Animator animation) {
super.onAnimationCancel(animation); super.onAnimationCancel(animation);
getDoricContext().removeAnimator(animatorId);
promise.reject(new JavaValue("Animation cancelled")); promise.reject(new JavaValue("Animation cancelled"));
} }
@Override @Override
public void onAnimationEnd(Animator animation) { public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation); super.onAnimationEnd(animation);
getDoricContext().removeAnimator(animatorId);
JSONBuilder jsonBuilder = new JSONBuilder(); JSONBuilder jsonBuilder = new JSONBuilder();
for (String key : animatedKeys) { for (String key : animatedKeys) {
jsonBuilder.put(key, getAnimatedValue(key)); jsonBuilder.put(key, getAnimatedValue(key));
@ -967,7 +968,7 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
@DoricMethod @DoricMethod
public void clearAnimation(String id, final DoricPromise promise) { public void clearAnimation(String id, final DoricPromise promise) {
Animator animator = animators.get(id); Animator animator = getDoricContext().getAnimator(id);
if (animator != null) { if (animator != null) {
animator.cancel(); animator.cancel();
} }
@ -976,7 +977,7 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
@DoricMethod @DoricMethod
public void cancelAnimation(String id, final DoricPromise promise) { public void cancelAnimation(String id, final DoricPromise promise) {
Animator animator = animators.get(id); Animator animator = getDoricContext().getAnimator(id);
if (animator != null) { if (animator != null) {
animator.cancel(); animator.cancel();
} }