package com.abc.view;
import android.content.Context;
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.util.AttributeSet;
import android.view.View;
import android.webkit.WebView;
import com.abc.util.Utils;
import com.abc.util.LogUtil;
public class RoundWebView extends WebView {
private int mWidth;
private int mHeight;
private float[] mRadiusArray;
private Path mPath = new Path();
public RoundWebView(Context context, AttributeSet attrs) {
super(context, attrs);
setLayerType(View.LAYER_TYPE_SOFTWARE, null); // 关闭硬件加速
int radius = Utils.dp2px(15f);
mRadiusArray = new float[] { radius, radius, radius, radius, radius, radius, radius, radius };
}
@Override
protected void onSizeChanged(int newWidth, int newHeight, int oldWidth, int oldHeight) {
super.onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
mWidth = newWidth;
mHeight = newHeight;
}
@Override
protected void onDraw(Canvas canvas) {
int x = getScrollX();
int y = getScrollY();
mPath.reset(); // 需要设置, 否则滑动到最顶端或最底端时圆角会变直角
mPath.addRoundRect(0, y, x + mWidth, y + mHeight, mRadiusArray, Path.Direction.CW);
canvas.clipPath(mPath);
super.onDraw(canvas);
}
}