From f64340ade79aebd212363cf0ebcb8e4da62bfef9 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Thu, 1 Aug 2019 20:31:12 +0800 Subject: [PATCH] deal corner --- .../penfeizhou/doric/shader/DoricLayer.java | 37 +++++++++++++++---- .../penfeizhou/doric/shader/ViewNode.java | 19 ++++++++++ js-framework/demo.ts | 9 ++++- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/shader/DoricLayer.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/shader/DoricLayer.java index b0208734..5802c8c5 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/shader/DoricLayer.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/shader/DoricLayer.java @@ -2,9 +2,9 @@ package com.github.penfeizhou.doric.shader; import android.content.Context; import android.graphics.Canvas; -import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; +import android.graphics.RectF; import android.os.Build; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -19,11 +19,11 @@ import android.widget.FrameLayout; * @CreateDate: 2019-07-31 */ public class DoricLayer extends FrameLayout { - private int mCornerRadius; - private Path mCornerPath = new Path(); private Paint shadowPaint = new Paint(); private Paint mBorderPaint; + private RectF mRect = new RectF(); + private float[] mCornerRadii; public DoricLayer(@NonNull Context context) { super(context); @@ -59,10 +59,25 @@ public class DoricLayer extends FrameLayout { @Override protected void dispatchDraw(Canvas canvas) { + mRect.left = 0; + mRect.right = getWidth(); + mRect.top = 0; + mRect.bottom = getHeight(); + if (mCornerRadii != null) { + mCornerPath.reset(); + mCornerPath.addRoundRect(mRect, mCornerRadii, Path.Direction.CW); + canvas.clipPath(mCornerPath); + } + + super.dispatchDraw(canvas); // draw border if (mBorderPaint != null) { - canvas.drawRect(0, 0, getWidth(), getHeight(), mBorderPaint); + if (mCornerRadii != null) { + canvas.drawRoundRect(mRect, mCornerRadii[0], mCornerRadii[1], mBorderPaint); + } else { + canvas.drawRect(mRect, mBorderPaint); + } } } @@ -80,10 +95,16 @@ public class DoricLayer extends FrameLayout { } public void setCornerRadius(int corner) { - if (mCornerRadius != corner) { - this.mCornerRadius = corner; - mCornerPath.reset(); - } + setCornerRadius(corner, corner, corner, corner); + } + + public void setCornerRadius(int leftTop, int rightTop, int rightBottom, int leftBottom) { + mCornerRadii = new float[]{ + leftTop, leftTop, + rightTop, rightTop, + rightBottom, rightBottom, + leftBottom, leftBottom, + }; } } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/shader/ViewNode.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/shader/ViewNode.java index c6787284..6a928190 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/shader/ViewNode.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/shader/ViewNode.java @@ -117,6 +117,25 @@ public abstract class ViewNode extends DoricContextHolder { doricLayer.setBorder(DoricUtils.dp2px(prop.asObject().getProperty("width").asNumber().toFloat()), prop.asObject().getProperty("color").asNumber().toInt()); } + break; + case "corners": + if (doricLayer != null) { + if (prop.isNumber()) { + doricLayer.setCornerRadius(DoricUtils.dp2px(prop.asNumber().toFloat())); + } else if (prop.isObject()) { + JSValue lt = prop.asObject().getProperty("leftTop"); + JSValue rt = prop.asObject().getProperty("rightTop"); + JSValue rb = prop.asObject().getProperty("rightBottom"); + JSValue lb = prop.asObject().getProperty("leftBottom"); + doricLayer.setCornerRadius( + DoricUtils.dp2px(lt.isNumber() ? lt.asNumber().toFloat() : 0), + DoricUtils.dp2px(rt.isNumber() ? rt.asNumber().toFloat() : 0), + DoricUtils.dp2px(rb.isNumber() ? rb.asNumber().toFloat() : 0), + DoricUtils.dp2px(lb.isNumber() ? lb.asNumber().toFloat() : 0) + ); + } + } + break; default: break; diff --git a/js-framework/demo.ts b/js-framework/demo.ts index 53ab8645..c6950d27 100644 --- a/js-framework/demo.ts +++ b/js-framework/demo.ts @@ -21,15 +21,20 @@ class CounterView extends ViewHolder { this.counter = new Text this.counter.text = "点击计数" this.counter.border = { - width: 1, + width: 10, color: Color.parse('#000000'), } this.counter.textSize = 20 - + this.counter.corners = 5 vlayout.space = 20 vlayout.layoutConfig = { alignment: new Gravity().center() } + vlayout.border = { + width: 1, + color: Color.parse("#000000"), + } + vlayout.corners = 10 vlayout.addChild(this.number) vlayout.addChild(this.counter) // root.bgColor = Color.parse('#00ff00')