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;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.content.Context;
import android.content.Intent;
@ -32,6 +33,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.Callable;
import pub.doric.async.AsyncResult;
@ -62,6 +64,7 @@ public class DoricContext {
private IDoricDriver doricDriver;
private final Map<String, Map<String, ViewNode<?>>> mHeadNodes = new HashMap<>();
private final DoricPerformanceProfile performanceProfile;
private final Map<String, Animator> animators = new HashMap<>();
public Collection<ViewNode<?>> allHeadNodes(String type) {
Map<String, ViewNode<?>> headNode = mHeadNodes.get(type);
@ -187,6 +190,10 @@ public class DoricContext {
}
public void teardown() {
for (Animator animator : animators.values()) {
animator.cancel();
}
animators.clear();
callEntity(DoricConstant.DORIC_ENTITY_DESTROY).setCallback(new AsyncResult.Callback<JSDecoder>() {
@Override
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;
private String mType;
protected JSObject mFlexConfig;
private final WeakHashMap<String, Animator> animators = new WeakHashMap<>();
public JSObject getFlexConfig() {
return mFlexConfig;
@ -942,18 +941,20 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
public void doAnimation(JSValue value, final DoricPromise promise) {
Animator animator = parseAnimator(value);
if (animator != null) {
String animatorId = value.asObject().getProperty("id").asString().value();
animators.put(animatorId, animator);
final String animatorId = value.asObject().getProperty("id").asString().value();
getDoricContext().putAnimator(animatorId, animator);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationCancel(Animator animation) {
super.onAnimationCancel(animation);
getDoricContext().removeAnimator(animatorId);
promise.reject(new JavaValue("Animation cancelled"));
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
getDoricContext().removeAnimator(animatorId);
JSONBuilder jsonBuilder = new JSONBuilder();
for (String key : animatedKeys) {
jsonBuilder.put(key, getAnimatedValue(key));
@ -967,7 +968,7 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
@DoricMethod
public void clearAnimation(String id, final DoricPromise promise) {
Animator animator = animators.get(id);
Animator animator = getDoricContext().getAnimator(id);
if (animator != null) {
animator.cancel();
}
@ -976,7 +977,7 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
@DoricMethod
public void cancelAnimation(String id, final DoricPromise promise) {
Animator animator = animators.get(id);
Animator animator = getDoricContext().getAnimator(id);
if (animator != null) {
animator.cancel();
}