Android 圆角framelayout的实现

需求如下:百度地图的mapview需要做成圆角矩形的,但是mapview被final修饰了,百度api也没有提供相应的接口来达到圆角效果,只能从外套的ViewGroup下手了.这里提供一个成果.话不多说上代码.


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import cn.cnmobi.xitu.widget.welfare_progress.DisplayUtils;

/**
 * 圆角framelayout
 * Created by wuyoujin on 2017/8/8/008.
 */

public class FilletFrameLayout extends FrameLayout {
    public FilletFrameLayout(@NonNull Context context) {
        super(context);
    }

    public FilletFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FilletFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        Path path = new Path();
        path.addRoundRect(new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight()), DisplayUtils.dip2px(getContext(),8),DisplayUtils.dip2px(getContext(),8), Path.Direction.CW);
        canvas.clipPath(path, Region.Op.REPLACE);
        super.dispatchDraw(canvas);
    }
}

你可能感兴趣的:(Android 圆角framelayout的实现)