自定义圆形ViewGroup的步骤记录下(其他形状类似)

2017-01-11  本文已影响0人  茶And狗狗

主要就是先把画布canvas,先按照自己的需求进行裁剪,我现在需要一个圆形的viewgroup:

public class MyCircleView extends RelativeLayout {
    public MyCircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    protected void onLayout(boolean c, int l, int t, int r, int b) {
        super.onLayout(c, l, t, r, b);
    }
    @Override
    protected void dispatchDraw(Canvas canvas) {
        Path path = new Path();
        //添加一个圆形的路径,Path.Direction.CW代表顺时针方向
        path.addRoundRect(new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight()), getMeasuredWidth()/2, getMeasuredWidth()/2, Path.Direction.CW);
        //画布按照路线裁剪
//        Region.Op.DIFFERENCE
//        -- 得到的区域    ->在A中,与B不相同的部分
//        Region.Op.REVERSE_DIFFERENCE
//        -- 得到的区域    ->在B中,与A不相同的部分
//        Region.Op.INTERSECT
//        -- 得到的区域    ->A与B相同的部分(交集)
//        Region.Op.XOR
//        -- 得到的区域    ->不包含A与B相同的部分(全集减去交集)
//        Region.Op.UNION
//        -- 得到的区域    ->A与B的所有部分(全集)
//        Region.Op.REPLACE
//        -- 得到的区域    ->将A的区域用B替换
        canvas.clipPath(path, Region.Op.REPLACE);        super.dispatchDraw(canvas);
    }
    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }}
上一篇 下一篇

猜你喜欢

热点阅读