之前客户要求做圆角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;
}
}
网友评论