像布局view那样使用canvas

625 阅读1分钟
原文链接: github.com

use canvas like view&viewgroup

now support shapeGroup:
FrameGroup,LinearGroup,RelativeGroup
now support shape:
OvalShape,TextShape,BitmapShape

for more shape

you can extends class ExShapeand override onMeasure or( measureWidth&measureHeight) to support wrap_content,but it's not necessary.
you need to override ondraw to draw the shape content;(see how the other shape work);

for more shapeGroup

just extends the class shapegroup and override layoutChild,if you want to support wrap_content then override onMeasure;(see how LinearGroup work);

how to use

see the class TestCanvas
and this is some code to guide you:
use ExShape.debug to enable/disable draw debug rect;

        LinearGroup group2 = new LinearGroup();
        group2.setOrientation(LinearGroup.VERTICAL);
        group2.setGravity(LinearGroup.CENTER);
        group2.setSize(MeasueType.WRAP_PARENT,MeasueType.WRAP_PARENT);
        RelativeGroup group=new RelativeGroup();
        group.addShape(bitmapShape,20);
        group.addRule(bitmapShape,textShape, RelativeGroup.Rule.TO_BELOW);
        group.addRule(bitmapShape,group, RelativeGroup.Rule.CENTER_V);
 @Override
    protected void onDraw(Canvas canvas) {
        group.setSize(getWidth(),getHeight());
        group.asRoot(canvas);
    }

image