美文网首页自定义view
自定义圆角webview

自定义圆角webview

作者: 大丸蛇 | 来源:发表于2020-02-18 16:02 被阅读0次

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

    相关文章

      网友评论

        本文标题:自定义圆角webview

        本文链接:https://www.haomeiwen.com/subject/cpwgfhtx.html