From 1e112055db31a1b07e718e751fafa981bf0b07a8 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Sat, 11 Apr 2020 12:56:05 +0800 Subject: [PATCH] Android implement maxWidth maxHeight --- .../java/pub/doric/shader/LinearNode.java | 41 +++++++++++++++++- .../java/pub/doric/shader/ScrollerNode.java | 40 ++++++++++++++++- .../main/java/pub/doric/shader/StackNode.java | 43 ++++++++++++++++++- .../main/java/pub/doric/shader/TextNode.java | 14 ++++++ doric-demo/src/FlexDemo.ts | 20 +-------- 5 files changed, 137 insertions(+), 21 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/shader/LinearNode.java b/doric-android/doric/src/main/java/pub/doric/shader/LinearNode.java index d35a8a4e..00f8a987 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/LinearNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/LinearNode.java @@ -15,6 +15,7 @@ */ package pub.doric.shader; +import android.content.Context; import android.graphics.drawable.ShapeDrawable; import android.view.ViewGroup; import android.widget.LinearLayout; @@ -31,10 +32,48 @@ import com.github.pengfeizhou.jscore.JSValue; * @CreateDate: 2019-07-23 */ public class LinearNode extends GroupNode { + + private static class MaximumLinearLayout extends LinearLayout { + private int maxWidth = Integer.MAX_VALUE; + private int maxHeight = Integer.MAX_VALUE; + + + public MaximumLinearLayout(Context context) { + super(context); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + int width = getMeasuredWidth(); + int height = getMeasuredHeight(); + if (width > maxWidth || height > maxHeight) { + width = Math.min(width, maxWidth); + height = Math.min(height, maxHeight); + widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST); + heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + } + } + } + public LinearNode(DoricContext doricContext) { super(doricContext); } + @Override + protected void blendLayoutConfig(JSObject jsObject) { + super.blendLayoutConfig(jsObject); + JSValue maxWidth = jsObject.getProperty("maxWidth"); + if (maxWidth.isNumber()) { + ((MaximumLinearLayout) mView).maxWidth = DoricUtils.dp2px(maxWidth.asNumber().toFloat()); + } + JSValue maxHeight = jsObject.getProperty("maxHeight"); + if (maxHeight.isNumber()) { + ((MaximumLinearLayout) mView).maxHeight = DoricUtils.dp2px(maxHeight.asNumber().toFloat()); + } + } + @Override protected void blendSubLayoutConfig(ViewNode viewNode, JSObject layoutConfig) { super.blendSubLayoutConfig(viewNode, layoutConfig); @@ -55,7 +94,7 @@ public class LinearNode extends GroupNode { @Override protected LinearLayout build() { - return new LinearLayout(getContext()); + return new MaximumLinearLayout(getContext()); } @Override diff --git a/doric-android/doric/src/main/java/pub/doric/shader/ScrollerNode.java b/doric-android/doric/src/main/java/pub/doric/shader/ScrollerNode.java index dd6a6be6..4abd47db 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/ScrollerNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/ScrollerNode.java @@ -15,7 +15,9 @@ */ package pub.doric.shader; +import android.content.Context; import android.text.TextUtils; +import android.widget.FrameLayout; import com.github.pengfeizhou.jscore.JSONBuilder; import com.github.pengfeizhou.jscore.JSObject; @@ -49,11 +51,47 @@ public class ScrollerNode extends SuperNode implements IDoricScrol private String onScrollEndFuncId; private DoricJSDispatcher jsDispatcher = new DoricJSDispatcher(); + private static class MaximumScrollView extends HVScrollView { + private int maxWidth = Integer.MAX_VALUE; + private int maxHeight = Integer.MAX_VALUE; + + + public MaximumScrollView(Context context) { + super(context); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + int width = getMeasuredWidth(); + int height = getMeasuredHeight(); + if (width > maxWidth || height > maxHeight) { + width = Math.min(width, maxWidth); + height = Math.min(height, maxHeight); + widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST); + heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + } + } + } public ScrollerNode(DoricContext doricContext) { super(doricContext); } + @Override + protected void blendLayoutConfig(JSObject jsObject) { + super.blendLayoutConfig(jsObject); + JSValue maxWidth = jsObject.getProperty("maxWidth"); + if (maxWidth.isNumber()) { + ((MaximumScrollView) mView).maxWidth = DoricUtils.dp2px(maxWidth.asNumber().toFloat()); + } + JSValue maxHeight = jsObject.getProperty("maxHeight"); + if (maxHeight.isNumber()) { + ((MaximumScrollView) mView).maxHeight = DoricUtils.dp2px(maxHeight.asNumber().toFloat()); + } + } + @Override public ViewNode getSubNodeById(String id) { return id.equals(mChildNode.getId()) ? mChildNode : null; @@ -68,7 +106,7 @@ public class ScrollerNode extends SuperNode implements IDoricScrol @Override protected HVScrollView build() { - HVScrollView hvScrollView = new HVScrollView(getContext()); + HVScrollView hvScrollView = new MaximumScrollView(getContext()); hvScrollView.setOnScrollChangeListener(new HVScrollView.OnScrollChangeListener() { @Override diff --git a/doric-android/doric/src/main/java/pub/doric/shader/StackNode.java b/doric-android/doric/src/main/java/pub/doric/shader/StackNode.java index 4e235a75..659bd136 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/StackNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/StackNode.java @@ -15,11 +15,14 @@ */ package pub.doric.shader; +import android.content.Context; import android.view.ViewGroup; import android.widget.FrameLayout; +import android.widget.LinearLayout; import pub.doric.DoricContext; import pub.doric.extension.bridge.DoricPlugin; +import pub.doric.utils.DoricUtils; import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSValue; @@ -31,10 +34,48 @@ import com.github.pengfeizhou.jscore.JSValue; */ @DoricPlugin(name = "Stack") public class StackNode extends GroupNode { + private static class MaximumFrameLayout extends FrameLayout { + private int maxWidth = Integer.MAX_VALUE; + private int maxHeight = Integer.MAX_VALUE; + + + public MaximumFrameLayout(Context context) { + super(context); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + int width = getMeasuredWidth(); + int height = getMeasuredHeight(); + if (width > maxWidth || height > maxHeight) { + width = Math.min(width, maxWidth); + height = Math.min(height, maxHeight); + widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.AT_MOST); + heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + } + } + } + + public StackNode(DoricContext doricContext) { super(doricContext); } + @Override + protected void blendLayoutConfig(JSObject jsObject) { + super.blendLayoutConfig(jsObject); + JSValue maxWidth = jsObject.getProperty("maxWidth"); + if (maxWidth.isNumber()) { + ((MaximumFrameLayout) mView).maxWidth = DoricUtils.dp2px(maxWidth.asNumber().toFloat()); + } + JSValue maxHeight = jsObject.getProperty("maxHeight"); + if (maxHeight.isNumber()) { + ((MaximumFrameLayout) mView).maxHeight = DoricUtils.dp2px(maxHeight.asNumber().toFloat()); + } + } + @Override protected void blendSubLayoutConfig(ViewNode viewNode, JSObject jsObject) { super.blendSubLayoutConfig(viewNode, jsObject); @@ -46,7 +87,7 @@ public class StackNode extends GroupNode { @Override protected FrameLayout build() { - return new FrameLayout(getContext()); + return new MaximumFrameLayout(getContext()); } @Override diff --git a/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java b/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java index 79875008..e9bf2dd9 100644 --- a/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java +++ b/doric-android/doric/src/main/java/pub/doric/shader/TextNode.java @@ -21,6 +21,7 @@ import android.util.TypedValue; import android.view.Gravity; import android.widget.TextView; +import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSValue; import pub.doric.DoricContext; @@ -48,6 +49,19 @@ public class TextNode extends ViewNode { return tv; } + @Override + protected void blendLayoutConfig(JSObject jsObject) { + super.blendLayoutConfig(jsObject); + JSValue maxWidth = jsObject.getProperty("maxWidth"); + if (maxWidth.isNumber()) { + mView.setMaxWidth(DoricUtils.dp2px(maxWidth.asNumber().toFloat())); + } + JSValue maxHeight = jsObject.getProperty("maxHeight"); + if (maxHeight.isNumber()) { + mView.setMaxHeight(DoricUtils.dp2px(maxWidth.asNumber().toFloat())); + } + } + @Override protected void blend(TextView view, String name, JSValue prop) { switch (name) { diff --git a/doric-demo/src/FlexDemo.ts b/doric-demo/src/FlexDemo.ts index 0b6d3c70..9c1a3e51 100644 --- a/doric-demo/src/FlexDemo.ts +++ b/doric-demo/src/FlexDemo.ts @@ -27,22 +27,6 @@ class LayoutDemo extends Panel { height: 100, } }), - // stack([], - // { - // backgroundColor: colors[3], - // flexConfig: { - // width: 500, - // height: 300, - // } - // }), - // stack([], - // { - // backgroundColor: colors[4], - // flexConfig: { - // width: 500, - // height: 500, - // } - // }), ], { flexConfig: { @@ -54,8 +38,8 @@ class LayoutDemo extends Panel { layoutConfig: { widthSpec: LayoutSpec.FIT, heightSpec: LayoutSpec.FIT, - minHeight: 300, - maxHeight: 300, + minHeight: 200, + maxHeight: 400, }, backgroundColor: colors[0].alpha(0.3), })