Skip to content

Commit

Permalink
Merge pull request #5 from sospartan/android_bugfix_animation
Browse files Browse the repository at this point in the history
* [Android] Improve Animation Performance
  • Loading branch information
sospartan authored Jul 19, 2016
2 parents 6ce045e + b4619b5 commit 8488221
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ public class WXDomObject extends CSSNode implements Cloneable {

public static final String TAG = WXDomObject.class.getSimpleName();
public static final String ROOT = "_root";
public static final String TRANSFORM = "transform";
public static final String TRANSFORM_ORIGIN = "transformOrigin";
public AtomicBoolean sDestroy = new AtomicBoolean();
public String ref = ROOT;
public String type = WXBasicComponentType.SCROLLER;
Expand Down
119 changes: 105 additions & 14 deletions android/sdk/src/main/java/com/taobao/weex/dom/WXDomStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
import com.taobao.weex.ui.component.WXComponent;
import com.taobao.weex.ui.component.WXVContainer;
import com.taobao.weex.utils.WXConst;
import com.taobao.weex.utils.WXDataStructureUtil;
import com.taobao.weex.utils.WXLogUtils;
import com.taobao.weex.utils.WXViewUtils;

Expand Down Expand Up @@ -392,15 +393,17 @@ void batch() {
WXSDKManager.getInstance().getSDKInstance(mInstanceId).updateDomObjTime(System.currentTimeMillis() - start);
}

WXLogUtils.d("Batch","animation size :" +animations.size());
parseAnimation();
animations.clear();

WXLogUtils.d("Batch","task size :" +mNormalTasks.size());
int count = mNormalTasks.size();
for (int i = 0; i < count && !mDestroy; ++i) {
mWXRenderManager.runOnThread(mInstanceId, mNormalTasks.get(i));
}
mNormalTasks.clear();
mAddDom.clear();
animations.clear();
mDirty = false;
if(WXSDKManager.getInstance().getSDKInstance(mInstanceId)!=null) {
WXSDKManager.getInstance().getSDKInstance(mInstanceId).batchTime(System.currentTimeMillis() - start0);
Expand Down Expand Up @@ -456,6 +459,11 @@ private void applyUpdate(WXDomObject dom) {
public void execute() {
mWXRenderManager.setLayout(mInstanceId, copy.ref, copy);
}

@Override
public String toString() {
return "setLayout";
}
});
if (dom.getExtra() != null) {
mNormalTasks.add(new IWXRenderTask() {
Expand All @@ -464,6 +472,11 @@ public void execute() {
public void execute() {
mWXRenderManager.setExtra(mInstanceId, copy.ref, copy.getExtra());
}

@Override
public String toString() {
return "setExtra";
}
});
}
}
Expand All @@ -475,16 +488,19 @@ public void execute() {
}

private void parseAnimation() {
WXAnimationBean animationBean;
for(final Pair<String, Map<String, Object>> pair:animations) {
if (!TextUtils.isEmpty(pair.first)) {
animationBean = createAnimationBean(pair.first, pair.second);
final WXAnimationBean animationBean = createAnimationBean(pair.first, pair.second);
if (animationBean != null) {
mRegistry.get(pair.first).style.setAnimationBean(animationBean);
mNormalTasks.add(new IWXRenderTask() {
@Override
public void execute() {
mWXRenderManager.startAnimation(mInstanceId, pair.first, null);
mWXRenderManager.startAnimation(mInstanceId, pair.first, animationBean, null);
}

@Override
public String toString() {
return "startAnimationByStyle";
}
});
}
Expand Down Expand Up @@ -552,7 +568,13 @@ public void execute() {
WXLogUtils.e("create body failed." + e.getMessage());
}
}

@Override
public String toString() {
return "createBody";
}
});
animations.add(new Pair<String, Map<String, Object>>(domObject.ref,domObject.style));
mDirty = true;

if (instance != null) {
Expand Down Expand Up @@ -673,6 +695,11 @@ public void execute() {
WXLogUtils.e("add component failed."+e.getMessage());
}
}

@Override
public String toString() {
return "AddDom";
}
});
animations.add(new Pair<String, Map<String, Object>>(domObject.ref,domObject.style));
mDirty = true;
Expand Down Expand Up @@ -719,6 +746,11 @@ void moveDom(final String ref, final String parentRef, final int index) {
public void execute() {
mWXRenderManager.moveComponent(mInstanceId, ref, parentRef, index);
}

@Override
public String toString() {
return "moveDom";
}
});

mDirty = true;
Expand Down Expand Up @@ -761,6 +793,11 @@ void removeDom(final String ref) {
public void execute() {
mWXRenderManager.removeComponent(mInstanceId, ref);
}

@Override
public String toString() {
return "removeDom";
}
});

mDirty = true;
Expand Down Expand Up @@ -811,6 +848,11 @@ void updateAttrs(String ref, final JSONObject attrs) {
public void execute() {
mWXRenderManager.updateAttrs(mInstanceId, domObject.ref, attrs);
}

@Override
public String toString() {
return "updateAttr";
}
});
mDirty = true;

Expand Down Expand Up @@ -840,11 +882,16 @@ void updateStyle(String ref, JSONObject style) {
return;
}

domObject.updateStyle(style);
transformStyle(domObject, false);
Map<String, Object> animationMap= WXDataStructureUtil.newHashMapWithExpectedSize(2);
animationMap.put(WXDomObject.TRANSFORM, style.remove(WXDomObject.TRANSFORM));
animationMap.put(WXDomObject.TRANSFORM_ORIGIN, style.remove(WXDomObject.TRANSFORM_ORIGIN));
animations.add(new Pair<>(ref, animationMap));

updateStyle(domObject, style);
animations.add(new Pair<String, Map<String, Object>>(ref,style));
if(!style.isEmpty()){
domObject.updateStyle(style);
transformStyle(domObject, false);
updateStyle(domObject, style);
}
mDirty = true;

if (instance != null) {
Expand All @@ -866,6 +913,11 @@ private void updateStyle(final WXDomObject domObject, final Map<String, Object>
public void execute() {
mWXRenderManager.updateStyle(mInstanceId, domObject.ref, update);
}

@Override
public String toString() {
return "updateStyle";
}
});
if (update.containsKey("padding") || update.containsKey("paddingTop") ||
update.containsKey("paddingLeft") ||
Expand All @@ -879,6 +931,11 @@ public void execute() {
Spacing border = domObject.getBorder();
mWXRenderManager.setPadding(mInstanceId, domObject.ref, padding, border);
}

@Override
public String toString() {
return "setPadding";
}
});
}
}
Expand Down Expand Up @@ -913,6 +970,11 @@ void addEvent(final String ref, final String type) {
public void execute() {
mWXRenderManager.addEvent(mInstanceId, ref, type);
}

@Override
public String toString() {
return "Add event";
}
});

mDirty = true;
Expand Down Expand Up @@ -948,6 +1010,11 @@ void removeEvent(final String ref, final String type) {
public void execute() {
mWXRenderManager.removeEvent(mInstanceId, ref, type);
}

@Override
public String toString() {
return "removeEvent";
}
});

mDirty = true;
Expand All @@ -973,6 +1040,11 @@ void scrollToDom(final String ref, final JSONObject options) {
public void execute() {
mWXRenderManager.scrollToComponent(mInstanceId, ref, options);
}

@Override
public String toString() {
return "scrollToPosition";
}
});

mDirty = true;
Expand All @@ -999,6 +1071,11 @@ public void execute() {
(int) root.getLayoutWidth(),
(int) root.getLayoutHeight());
}

@Override
public String toString() {
return "createFinish";
}
});

mDirty = true;
Expand All @@ -1025,6 +1102,11 @@ public void execute() {
int realHeight = (int) root.getLayoutHeight();
mWXRenderManager.refreshFinish(mInstanceId, realWidth, realHeight);
}

@Override
public String toString() {
return "refreshFinish";
}
});

mDirty = true;
Expand Down Expand Up @@ -1099,6 +1181,11 @@ void updateFinish() {
public void execute() {
mWXRenderManager.updateFinish(mInstanceId);
}

@Override
public String toString() {
return "updateFinish";
}
});

mDirty = true;
Expand All @@ -1117,13 +1204,17 @@ void startAnimation(@NonNull final String ref, @NonNull String animation,
if (domObject == null) {
return;
}
WXAnimationBean animationBean=createAnimationBean(ref, animation);
final WXAnimationBean animationBean=createAnimationBean(ref, animation);
if(animationBean!=null) {
domObject.style.setAnimationBean(animationBean);
mNormalTasks.add(new IWXRenderTask() {
@Override
public void execute() {
mWXRenderManager.startAnimation(mInstanceId, ref, callBack);
mWXRenderManager.startAnimation(mInstanceId, ref, animationBean, callBack);
}

@Override
public String toString() {
return "startAnimationByCall";
}
});
}
Expand All @@ -1150,9 +1241,9 @@ private WXAnimationBean createAnimationBean(String ref, String animation){
private WXAnimationBean createAnimationBean(String ref,Map<String, Object> style){
if (style != null) {
try {
Object transform = style.get(WXStyle.TRANSFORM);
Object transform = style.get(WXDomObject.TRANSFORM);
if (transform instanceof String && !TextUtils.isEmpty((String) transform)) {
String transformOrigin = (String) style.get(WXStyle.TRANSFORM_ORIGIN);
String transformOrigin = (String) style.get(WXDomObject.TRANSFORM_ORIGIN);
WXAnimationBean animationBean = new WXAnimationBean();
WXDomObject domObject = mRegistry.get(ref);
int width = (int) domObject.getLayoutWidth();
Expand Down
12 changes: 0 additions & 12 deletions android/sdk/src/main/java/com/taobao/weex/dom/WXStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@
import com.taobao.weex.dom.flex.CSSJustify;
import com.taobao.weex.dom.flex.CSSPositionType;
import com.taobao.weex.dom.flex.CSSWrap;
import com.taobao.weex.ui.animation.WXAnimationBean;
import com.taobao.weex.ui.component.WXText;
import com.taobao.weex.ui.component.WXTextDecoration;
import com.taobao.weex.utils.WXUtils;
Expand All @@ -228,11 +227,8 @@
*/
public class WXStyle extends ConcurrentHashMap<String, Object> {

public static final String TRANSFORM = "transform";
public static final String TRANSFORM_ORIGIN = "transformOrigin";
private static final long serialVersionUID = 611132641365274134L;
static final int UNSET = -1;
private WXAnimationBean animationBean;
/*
* text-decoration
**/
Expand Down Expand Up @@ -623,12 +619,4 @@ public String getOverflow() {
Object obj = get(WXDomPropConstant.WX_OVERFLOW);
return obj == null ? WXDomPropConstant.WX_OVERFLOW_VISIBLE : obj.toString();
}

public WXAnimationBean getAnimationBean() {
return animationBean;
}

public void setAnimationBean(WXAnimationBean animationBean) {
this.animationBean = animationBean;
}
}
Loading

0 comments on commit 8488221

Please sign in to comment.