Android 自定义圆角 Webview

试了很多方法没有找到去掉Webview圆角的方法,最后在stackoverflow找到这样的代码。

public class RoundedWebView extends WebView
{
    private int width;

    private int height;

    private int mRadius = 0;

    public RoundedWebView(Context context)
    {
        super(context);
    }

    public RoundedWebView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public RoundedWebView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
    }

    public void setRadius(int radius)
    {
        mRadius = radius;
    }

    // This method gets called when the view first loads, and also whenever the
    // view changes. Use this opportunity to save the view's width and height.
    @Override protected void onSizeChanged(int newWidth, int newHeight, int oldWidth, int oldHeight)
    {
        super.onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);

        width = newWidth;

        height = newHeight;

    }

    @SuppressLint("DrawAllocation")
    @Override protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

        if (mRadius == 0)
            return;

        Path path = new Path();

        path.setFillType(Path.FillType.INVERSE_WINDING);

        path.addRoundRect(new RectF(getScrollX(), getScrollY(),getScrollX()+ width, getScrollY() + height), mRadius, mRadius, Path.Direction.CW);

        canvas.drawPath(path, createPorterDuffClearPaint());
    }

    private Paint createPorterDuffClearPaint()
    {
        Paint paint = new Paint();

        paint.setStyle(Paint.Style.FILL);

        paint.setAntiAlias(true);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

        return paint;
    }
}

你可能感兴趣的:(Android 自定义圆角 Webview)