android自定义带圆角的webview,并解决圆角背景有黑边的问题

  1. 创建一个文件,添加如下代码,直接复制即可
package xx.yy.floatwin;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.webkit.WebView;

public class CorWebView extends WebView {
    private Paint paint1;
    private Paint paint2;
    // 弧度
    private float m_radius = 100;
    private int width = 100;
    private int height = 100;
    private int x;
    private int y;

    public CorWebView(Context context) {
        super(context);
        init(context);
    }

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

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

    private void init(Context context){
        paint1 = new Paint();
        setBackgroundColor(0xFFdcdcdc);
        paint1.setColor(Color.WHITE);
        paint1.setAntiAlias(true);
        paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        paint2 = new Paint();
        paint2.setXfermode(null);
    }

    public void setRadius(int w, int h, float radius){
        m_radius = radius;
        width = w;
        height = h;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //判断 避免 width,height值为0,否则Bitmap.createBitmap()报错
        if (getMeasuredWidth() != 0) {
            width = getMeasuredWidth();
        }
        if (getMeasuredHeight() != 0) {
            height = getMeasuredHeight();
        }
    }

    @Override
    public void draw(Canvas canvas) {
        x = this.getScrollX();
        y = this.getScrollY();
        Bitmap bitmap = Bitmap.createBitmap(x + width, y + height,
                Bitmap.Config.ARGB_8888);
        Canvas canvas2 = new Canvas(bitmap);
        super.draw(canvas2);
        drawLeftUp(canvas2);
        drawRightUp(canvas2);
        drawLeftDown(canvas2);
        drawRightDown(canvas2);
        canvas.drawBitmap(bitmap, 0, 0, paint2);
        bitmap.recycle();
    }

    private void drawLeftUp(Canvas canvas) {
        Path path = new Path();
        path.moveTo(x, m_radius);
        path.lineTo(x, y);
        path.lineTo(m_radius, y);
        path.arcTo(new RectF(x, y, x + m_radius * 2, y + m_radius * 2), -90, -90);
        path.close();
        canvas.drawPath(path, paint1);
    }

    private void drawLeftDown(Canvas canvas) {
        Path path = new Path();
        path.moveTo(x, y + height - m_radius);
        path.lineTo(x, y + height);
        path.lineTo(x + m_radius, y + height);
        path.arcTo(new RectF(x, y + height - m_radius * 2,
                x + m_radius * 2, y + height), 90, 90);
        path.close();
        canvas.drawPath(path, paint1);
    }

    private void drawRightDown(Canvas canvas) {
        Path path = new Path();
        path.moveTo(x + width - m_radius, y + height);
        path.lineTo(x + width, y + height);
        path.lineTo(x + width, y + height - m_radius);
        path.arcTo(new RectF(x + width - m_radius * 2, y + height
                - m_radius * 2, x + width, y + height), 0, 90);
        path.close();
        canvas.drawPath(path, paint1);
    }

    private void drawRightUp(Canvas canvas) {
        Path path = new Path();
        path.moveTo(x + width, y + m_radius);
        path.lineTo(x + width, y);
        path.lineTo(x + width - m_radius, y);
        path.arcTo(new RectF(x + width - m_radius * 2, y, x + width,
                y + m_radius * 2), -90, 90);
        path.close();
        canvas.drawPath(path, paint1);
    }
}
  1. 在你的布局文件中添加上述view
    示例如下:
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:id="@+id/web_content"
    划重点!!!!
    这里,webview的父布局的背景色必须为透明,否则webview的圆角部分会被填充为黑色!
    android:background="@android:color/transparent">
    
    <xx.yy.floatwin.CorWebView
        android:id="@+id/bg_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
        
</RelativeLayout>
  1. OK了,这样就实现了一个完美的圆角webview了!对您有帮助的话,麻烦笔芯
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大炮走火

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值