Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

圆形的图带边框两边会有两条线 #21

Open
Rongrkang opened this issue Oct 24, 2019 · 2 comments
Open

圆形的图带边框两边会有两条线 #21

Rongrkang opened this issue Oct 24, 2019 · 2 comments

Comments

@Rongrkang
Copy link

NiceImageView 设置成圆形图片
如果把scaleType设置成centerCrop 设置的图片的宽高比不等于控件的宽高比并且设置有边框,会漏出来两条线

@fupangzi
Copy link

我也遇到了

@Rongrkang
Copy link
Author

改了下

@Override
protected void onDraw(Canvas canvas) {
    // 使用图形混合模式来显示指定区域的图片
    int save = canvas.saveLayer(srcRectF, null, Canvas.ALL_SAVE_FLAG);
    if (!isCoverSrc) {
        float sx = 1.0f * (width - 2 * borderWidth - 2 * innerBorderWidth) / width;
        float sy = 1.0f * (height - 2 * borderWidth - 2 * innerBorderWidth) / height;
        // 缩小画布,使图片内容不被borders覆盖
        canvas.scale(sx, sy, width / 2.0f, height / 2.0f);
    }
    int cropSave = canvas.saveLayer(srcRectF, null, Canvas.ALL_SAVE_FLAG);
    super.onDraw(canvas);
    paint.reset();
    path.reset();
    if (isCircle) {
        path.addCircle(width / 2.0f, height / 2.0f, radius, Path.Direction.CCW);
    } else {
        path.addRoundRect(srcRectF, srcRadii, Path.Direction.CCW);
    }

    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    paint.setXfermode(xfermode);
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
        canvas.drawPath(path, paint);
    } else {

// srcPath.addRect(srcRectF, Path.Direction.CCW);
// // 计算tempPath和path的差集
// srcPath.op(path, Path.Op.DIFFERENCE);
// canvas.drawPath(srcPath, paint);

        srcPath.reset();
        srcPath.addRect(srcRectF, Path.Direction.CCW);
        // 计算tempPath和path的差集
        srcPath.op(path, Path.Op.DIFFERENCE);

        canvas.drawPath(srcPath, paint);
    }
    paint.setXfermode(null);

    // 绘制遮罩
    if (maskColor != 0) {
        paint.setColor(maskColor);
        canvas.drawPath(path, paint);
    }
    // 恢复画布
    canvas.restoreToCount(cropSave);
    canvas.restoreToCount(save);
    // 绘制边框
    drawBorders(canvas);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants