deal corner

This commit is contained in:
pengfei.zhou 2019-08-01 20:31:12 +08:00
parent c4672fc0af
commit f64340ade7
3 changed files with 55 additions and 10 deletions

View File

@ -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,
};
}
}

View File

@ -117,6 +117,25 @@ public abstract class ViewNode<T extends View> 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;

View File

@ -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')