feat:android supoorts sync animationed values

This commit is contained in:
pengfei.zhou 2019-12-02 19:05:00 +08:00
parent 1cd84d8720
commit 751a385f38
2 changed files with 50 additions and 38 deletions

View File

@ -43,8 +43,10 @@ import pub.doric.utils.DoricUtils;
import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import com.github.pengfeizhou.jscore.JavaValue;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
@ -641,6 +643,14 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
return getNodeView().getPivotY() / getNodeView().getHeight(); return getNodeView().getPivotY() / getNodeView().getHeight();
} }
private String[] animatedKeys = {
"translationX",
"translationY",
"scaleX",
"scaleY",
"rotation",
};
@DoricMethod @DoricMethod
public void doAnimation(JSValue value, final DoricPromise promise) { public void doAnimation(JSValue value, final DoricPromise promise) {
Animator animator = parseAnimator(value); Animator animator = parseAnimator(value);
@ -649,7 +659,11 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
@Override @Override
public void onAnimationEnd(Animator animation) { public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation); super.onAnimationEnd(animation);
promise.resolve(); JSONBuilder jsonBuilder = new JSONBuilder();
for (String key : animatedKeys) {
jsonBuilder.put(key, getAnimatedValue(key));
}
promise.resolve(new JavaValue(jsonBuilder.toJSONObject()));
} }
}); });
animator.start(); animator.start();
@ -673,37 +687,6 @@ public abstract class ViewNode<T extends View> extends DoricContextHolder {
if (delayJS.isNumber()) { if (delayJS.isNumber()) {
animatorSet.setStartDelay(delayJS.asNumber().toLong()); animatorSet.setStartDelay(delayJS.asNumber().toLong());
} }
JSValue fillModeJSVal = value.asObject().getProperty("fillMode");
final int fillMode = fillModeJSVal.asNumber().toInt();
animatorSet.addListener(new AnimatorListenerAdapter() {
private HashMap<String, Float> originVals = new HashMap<>();
private String[] keys = {
"translationX",
"translationY",
"scaleX",
"scaleY",
"rotation",
};
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
for (String key : keys) {
originVals.put(key, getAnimatedValue(key));
}
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if ((fillMode & 1) != 1) {
for (String key : keys) {
setAnimatedValue(key, originVals.get(key));
}
}
}
});
return animatorSet; return animatorSet;
} else if (value.isObject()) { } else if (value.isObject()) {
JSArray changeables = value.asObject().getProperty("changeables").asArray(); JSArray changeables = value.asObject().getProperty("changeables").asArray();

View File

@ -24,6 +24,35 @@ class AnimationDemo extends Panel {
title("Complicated Animation"), title("Complicated Animation"),
vlayout( vlayout(
[ [
hlayout([
thisLabel('reset').apply({
onClick: () => {
const rotation = new RotationAnimation
rotation.duration = 1000
rotation.fromRotation = view.rotation || 0
rotation.toRotation = 0
const translation = new TranslationAnimation
translation.duration = 1000
translation.fromTranslationX = view.translationX || 0
translation.toTranslationX = 0
translation.fromTranslationY = view.translationY || 0
translation.toTranslationY = 0
const scale = new ScaleAnimation
scale.duration = 1000
scale.fromScaleX = view.scaleX || 1
scale.toScaleX = 1
scale.fromScaleY = view.scaleY || 1
scale.toScaleY = 1
const animationSet = new AnimationSet
animationSet.addAnimation(rotation)
animationSet.addAnimation(translation)
animationSet.addAnimation(scale)
view.doAnimation(context, animationSet).then(() => {
modal(context).toast('Resetd')
})
}
}),
]).apply({ space: 10 } as IHLayout),
hlayout([ hlayout([
thisLabel('TranslationX').apply({ thisLabel('TranslationX').apply({
onClick: () => { onClick: () => {
@ -51,8 +80,8 @@ class AnimationDemo extends Panel {
onClick: () => { onClick: () => {
const animation = new ScaleAnimation const animation = new ScaleAnimation
animation.duration = 1000 animation.duration = 1000
animation.fromScaleX = 0 animation.fromScaleX = view.scaleX || 1
animation.toScaleX = 5 animation.toScaleX = animation.fromScaleX + 1
view.doAnimation(context, animation) view.doAnimation(context, animation)
} }
}), }),
@ -60,8 +89,8 @@ class AnimationDemo extends Panel {
onClick: () => { onClick: () => {
const animation = new ScaleAnimation const animation = new ScaleAnimation
animation.duration = 1000 animation.duration = 1000
animation.fromScaleY = 0 animation.fromScaleY = view.scaleY || 1
animation.toScaleY = 5 animation.toScaleY = animation.fromScaleY + 1
view.doAnimation(context, animation) view.doAnimation(context, animation)
} }
}), }),
@ -69,8 +98,8 @@ class AnimationDemo extends Panel {
onClick: () => { onClick: () => {
const animation = new RotationAnimation const animation = new RotationAnimation
animation.duration = 1000 animation.duration = 1000
animation.fromRotation = 0 animation.fromRotation = view.rotation || 0
animation.toRotation = 4 animation.toRotation = animation.fromRotation + 0.25
view.doAnimation(context, animation) view.doAnimation(context, animation)
} }
}), }),