Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

[WEEX-107] [android] weex transition support on android platform #851

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions android/sdk/src/main/java/com/taobao/weex/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ public interface Event {
String UNSTICKY = "unsticky";
String STICKY = "sticky";

String ON_TRANSITION_END = "transitionEnd";

interface SLOT_LIFECYCLE{
String CREATE = "create";
String ATTACH = "attach";
Expand Down Expand Up @@ -318,4 +320,13 @@ public interface CodeCache {
String BANNER_DIGEST = "digest";
String SAVE_PATH = "v8";
}

public interface TimeFunction{
String LINEAR = "linear";
String EASE_IN_OUT = "ease-in-out";
String EASE_IN = "ease-in";
String EASE_OUT = "ease-out";
String EASE = "ease";
String CUBIC_BEZIER = "cubic-bezier";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public interface DOMActionContext {

boolean isDestory();

void markDirty();

WXSDKInstance getInstance();

WXDomObject getDomByRef(String ref);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ void batch() {
batchEvent.ph = "X";
WXTracing.submit(batchEvent);
}
if(WXEnvironment.isApkDebugable()){
WXLogUtils.d("mInstanceId " + mInstanceId + " batch used " + (System.currentTimeMillis() - start));
}
}

void layout(WXDomObject rootDom) {
Expand Down Expand Up @@ -212,6 +215,7 @@ public void accept(WXDomObject dom) {
instance.cssLayoutTime(System.currentTimeMillis() - start);
}

start = System.currentTimeMillis();
rootDom.traverseTree( new WXDomObject.Consumer() {
@Override
public void accept(WXDomObject dom) {
Expand All @@ -220,10 +224,8 @@ public void accept(WXDomObject dom) {
}
dom.layoutAfter();
}
});
}, new ApplyUpdateConsumer());

start = System.currentTimeMillis();
rootDom.traverseTree(new ApplyUpdateConsumer());

if (instance != null) {
instance.applyUpdateTime(System.currentTimeMillis() - start);
Expand Down Expand Up @@ -376,6 +378,15 @@ public boolean isDestory() {
return false;
}

@Override
public void markDirty() {
if(!mDestroy){
if(!mDirty){
mDirty = true;
}
}
}

@Override
public WXSDKInstance getInstance() {
return mWXRenderManager.getWXSDKInstance(mInstanceId);
Expand Down
15 changes: 13 additions & 2 deletions android/sdk/src/main/java/com/taobao/weex/dom/WXDomHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class WXDomHandler implements Handler.Callback {
* The batch operation in dom thread will run at most once in 16ms.
*/
public static final int DELAY_TIME = 16;//ms
public static final int TRANSITION_DELAY_TIME = 2;//2ms, start transition as soon as

private WXDomManager mWXDomManager;
private boolean mHasBatch = false;

Expand All @@ -61,8 +63,14 @@ public boolean handleMessage(Message msg) {

if (!mHasBatch) {
mHasBatch = true;
mWXDomManager.sendEmptyMessageDelayed(WXDomHandler.MsgType.WX_DOM_BATCH, DELAY_TIME);
}
if(what != WXDomHandler.MsgType.WX_DOM_BATCH) {
int delayTime = DELAY_TIME;
if(what == MsgType.WX_DOM_TRANSITION_BATCH){
delayTime = TRANSITION_DELAY_TIME;
}
mWXDomManager.sendEmptyMessageDelayed(WXDomHandler.MsgType.WX_DOM_BATCH, delayTime);
}
}
switch (what) {
case MsgType.WX_EXECUTE_ACTION:
mWXDomManager.executeAction(task.instanceId, (DOMAction) task.args.get(0), (boolean) task.args.get(1));
Expand Down Expand Up @@ -125,6 +133,9 @@ public static class MsgType {
public static final int WX_DOM_BATCH = 0xff;
public static final int WX_CONSUME_RENDER_TASKS = 0xfa;


public static final int WX_DOM_TRANSITION_BATCH = 0xfb;

@Deprecated
public static final int WX_COMPONENT_SIZE= 0xff1;

Expand Down
33 changes: 31 additions & 2 deletions android/sdk/src/main/java/com/taobao/weex/dom/WXDomObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.taobao.weex.dom.flex.CSSLayoutContext;
import com.taobao.weex.dom.flex.CSSNode;
import com.taobao.weex.dom.flex.Spacing;
import com.taobao.weex.dom.transition.WXTransition;
import com.taobao.weex.ui.component.WXBasicComponentType;
import com.taobao.weex.utils.WXLogUtils;
import com.taobao.weex.utils.WXViewUtils;
Expand All @@ -43,6 +44,8 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;



/**
* WXDomObject contains all the info about the given node, including style, attribute and event.
* Unlike {@link com.taobao.weex.ui.component.WXComponent}, WXDomObject only contains info about
Expand Down Expand Up @@ -87,6 +90,8 @@ public class WXDomObject extends CSSNode implements Cloneable,ImmutableDomObject

/** package **/ WXEvent mEvents;

private WXTransition transition;;




Expand Down Expand Up @@ -165,6 +170,9 @@ public String getType(){
return mEvents;
}

public WXTransition getTransition() {
return transition;
}

public @NonNull DomContext getDomContext() {
return mDomContext;
Expand Down Expand Up @@ -221,6 +229,7 @@ public void parseFromJson(JSONObject map){
WXStyle styles = new WXStyle();
styles.putAll((JSONObject) style,false);
this.mStyles = styles;
this.transition = WXTransition.fromMap(styles, this);
}
Object attr = map.get("attr");
if (attr != null && attr instanceof JSONObject) {
Expand Down Expand Up @@ -453,18 +462,38 @@ public void updateStyle(Map<String, Object> styles, boolean byPesudo) {
if (styles == null || styles.isEmpty()) {
return;
}
if(transition != null){
if(transition.hasTransitionProperty(styles)){
transition.startTransition(styles);
}
}
if (mStyles == null) {
mStyles = new WXStyle();
}
mStyles.putAll(styles,byPesudo);
if(transition == null){
this.transition = WXTransition.fromMap(mStyles, this);
}
super.dirty();
}

/** package **/ void applyStyleToNode() {

public void applyStyle(Map<String, Object> styles){
applyStyleToNode(styles);
}

void applyStyleToNode() {
applyStyleToNode(getStyles());
}

/** package **/ void applyStyleToNode(Map<String, Object> updates) {
if(updates.size() == 0){
return;
}
WXStyle stylesMap = getStyles();
int vp = getViewPortWidth();
if (!stylesMap.isEmpty()) {
for(Map.Entry<String,Object> item:stylesMap.entrySet()) {
for(Map.Entry<String,Object> item: updates.entrySet()) {
switch (item.getKey()) {
case Constants.Name.ALIGN_ITEMS:
setAlignItems(stylesMap.getAlignItems());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@
import java.util.HashMap;
import java.util.List;

import static com.taobao.weex.common.Constants.TimeFunction.CUBIC_BEZIER;
import static com.taobao.weex.common.Constants.TimeFunction.EASE;
import static com.taobao.weex.common.Constants.TimeFunction.EASE_IN;
import static com.taobao.weex.common.Constants.TimeFunction.EASE_IN_OUT;
import static com.taobao.weex.common.Constants.TimeFunction.EASE_OUT;
import static com.taobao.weex.common.Constants.TimeFunction.LINEAR;


class AnimationAction implements DOMAction, RenderAction {

Expand Down Expand Up @@ -245,29 +252,31 @@ Interpolator createTimeInterpolator() {
String interpolator = mAnimationBean.timingFunction;
if (!TextUtils.isEmpty(interpolator)) {
switch (interpolator) {
case WXAnimationBean.EASE_IN:
return new AccelerateInterpolator();
case WXAnimationBean.EASE_OUT:
return new DecelerateInterpolator();
case WXAnimationBean.EASE_IN_OUT:
return new AccelerateDecelerateInterpolator();
case WXAnimationBean.LINEAR:
return new LinearInterpolator();
case EASE_IN:
return PathInterpolatorCompat.create(0.42f,0f, 1f,1f);
case EASE_OUT:
return PathInterpolatorCompat.create(0f,0f, 0.58f,1f);
case EASE_IN_OUT:
return PathInterpolatorCompat.create(0.42f,0f, 0.58f,1f);
case EASE:
return PathInterpolatorCompat.create(0.25f,0.1f, 0.25f,1f);
case LINEAR:
return PathInterpolatorCompat.create(0.0f,0f, 1f,1f);
default:
//Parse cubic-bezier
try {
SingleFunctionParser<Float> parser = new SingleFunctionParser<>(
mAnimationBean.timingFunction,
new SingleFunctionParser.FlatMapper<Float>() {
@Override
public Float map(String raw) {
return Float.parseFloat(raw);
}
});
List<Float> params = parser.parse(WXAnimationBean.CUBIC_BEZIER);
mAnimationBean.timingFunction,
new SingleFunctionParser.FlatMapper<Float>() {
@Override
public Float map(String raw) {
return Float.parseFloat(raw);
}
});
List<Float> params = parser.parse(CUBIC_BEZIER);
if (params != null && params.size() == WXAnimationBean.NUM_CUBIC_PARAM) {
return PathInterpolatorCompat.create(
params.get(0), params.get(1), params.get(2), params.get(3));
params.get(0), params.get(1), params.get(2), params.get(3));
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,22 @@ public void executeDom(DOMActionContext context) {
mPadding = domObject.getPadding();
mBorder = domObject.getBorder();

Map<String, Object> animationMap = new ArrayMap<>(2);
animationMap.put(WXDomObject.TRANSFORM, mData.get(WXDomObject.TRANSFORM));
animationMap.put(WXDomObject.TRANSFORM_ORIGIN, mData.get(WXDomObject.TRANSFORM_ORIGIN));
if(mData.get(WXDomObject.TRANSFORM) != null || mData.get(WXDomObject.TRANSFORM_ORIGIN) != null){
if(domObject.getTransition() == null) {
Map<String, Object> animationMap = new ArrayMap<>(2);
animationMap.put(WXDomObject.TRANSFORM, mData.get(WXDomObject.TRANSFORM));
animationMap.put(WXDomObject.TRANSFORM_ORIGIN, mData.get(WXDomObject.TRANSFORM_ORIGIN));
context.addAnimationForElement(mRef, animationMap);
}
}

context.addAnimationForElement(mRef, animationMap);

if (!mData.isEmpty()) {
domObject.updateStyle(mData, mIsCausedByPesudo);
domObject.traverseTree(context.getApplyStyleConsumer());
context.postRenderTask(this);
domObject.updateStyle(mData);
domObject.applyStyle(mData);
if(!mData.isEmpty()) {
context.postRenderTask(this);
}
}

if (instance != null) {
Expand Down
Loading