android webview圆角,自定义圆角webview

之前客户要求做圆角webview

试了很多方法最后在stackoverflow找到这样一个代码

可以兼容 8.0 9.0

10 没测试过

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值