生成平滑圆角

请添加图片描述

package com.example.round;


import android.graphics.Path;
import android.graphics.RectF;

public class RoundPath {

    private static Path mRoundPath = new Path();

    public static float coerceAtMost(float value, float maximumValue) {
        if (value > maximumValue) {
            return maximumValue;
        }
        return value;
    }

    public static float coerceAtLeast(float value, float minimumValue) {
        if (value < minimumValue) {
            return minimumValue;
        }
        return value;
    }


    public static Path getSmoothRoundPath(RectF rectF, float radius) {
        radius = Math.max(radius, 0);

        float left = rectF.left;
        float top = rectF.top;
        float width = rectF.width();
        float height = rectF.height();


        // 开始画圆角的位置对比圆角大小的偏移比例
        float radiusOffsetRatio = 128f / 100f;
        // 靠近圆弧两个端点的点的xy坐标比例
        float endPointRatio = 83f / 100f;
        // 左上角第一条曲线第二个点x坐标的比例(其他三个点通过矩阵转换可以使用同样的比例)
        float firstCSecondPXRatio = 67f / 100f;
        // 左上角第一条曲线第二个点Y坐标的比例(其他三个点通过矩阵转换可以使用同样的比例)
        float firstCSecondPYRatio = 4f / 100f;
        // 左上角第一条曲线第三个点x坐标的比例(其他三个点通过矩阵转换可以使用同样的比例)
        float firstCThirdPXRatio = 51f / 100f;
        // 左上角第一条曲线第三个点Y坐标的比例(其他三个点通过矩阵转换可以使用同样的比例)
        float firstCThirdPYRatio = 13f / 100f;
        // 左上角第二条曲线第一个点X坐标的比例(其他三个点通过矩阵转换可以使用同样的比例)
        float secondCFirstPXRatio = 34f / 100f;
        // 左上角第二条曲线第一个点Y坐标的比例(其他三个点通过矩阵转换可以使用同样的比例)
        float secondCFirstPYRatio = 22f / 100f;

        mRoundPath.reset();

        mRoundPath.moveTo((width / 2.0f) + left, top);//顶部直线和右上角圆角

        // 顶部直线和右上角圆角
        mRoundPath.lineTo(
                coerceAtLeast((width / 2.0f), (width - radius * radiusOffsetRatio) + left),
                top
        );
        mRoundPath.cubicTo(
                left + width - radius * endPointRatio, top,
                left + width - radius * firstCSecondPXRatio,
                top + radius * firstCSecondPYRatio,
                left + width - radius * firstCThirdPXRatio,
                top + radius * firstCThirdPYRatio
        );
        mRoundPath.cubicTo(
                left + width - radius * secondCFirstPXRatio,
                top + radius * secondCFirstPYRatio,
                left + width - radius * secondCFirstPYRatio,
                top + radius * secondCFirstPXRatio,
                left + width - radius * firstCThirdPYRatio,
                top + radius * firstCThirdPXRatio
        );
        mRoundPath.cubicTo(
                left + width - radius * firstCSecondPYRatio,
                top + radius * firstCSecondPXRatio,
                left + width,
                top + radius * endPointRatio,
                left + width,
                top + coerceAtMost((height / 2.0f), radius * radiusOffsetRatio)
        );

        //右边直线和右下角圆角
        mRoundPath.lineTo(left + width, coerceAtLeast((height / 2.0f), height - radius * radiusOffsetRatio) + top);
        mRoundPath.cubicTo(
                left + width,
                top + height - radius * endPointRatio,
                left + width - radius * firstCSecondPYRatio,
                top + height - radius * firstCSecondPXRatio,
                left + width - radius * firstCThirdPYRatio,
                top + height - radius * firstCThirdPXRatio
        );
        mRoundPath.cubicTo(
                left + width - radius * secondCFirstPYRatio,
                top + height - radius * secondCFirstPXRatio,
                left + width - radius * secondCFirstPXRatio,
                top + height - radius * secondCFirstPYRatio,
                left + width - radius * firstCThirdPXRatio,
                top + height - radius * firstCThirdPYRatio
        );

        mRoundPath.cubicTo(
                left + width - radius * firstCSecondPXRatio,
                top + height - radius * firstCSecondPYRatio,
                left + width - radius * endPointRatio,
                top + height,
                left + coerceAtLeast((width / 2.0f), width - radius * radiusOffsetRatio),
                top + height
        );

        // 底部直线和左下角圆角
        mRoundPath.lineTo(coerceAtMost((width / 2.0f), radius * radiusOffsetRatio) + left, top + height);
        mRoundPath.cubicTo(
                left + radius * endPointRatio,
                top + height,
                left + radius * firstCSecondPXRatio,
                top + height - radius * firstCSecondPYRatio,
                left + radius * firstCThirdPXRatio,
                top + height - radius * firstCThirdPYRatio
        );
        mRoundPath.cubicTo(
                left + radius * secondCFirstPXRatio,
                top + height - radius * secondCFirstPYRatio,
                left + radius * secondCFirstPYRatio,
                top + height - radius * secondCFirstPXRatio,
                left + radius * firstCThirdPYRatio,
                top + height - radius * firstCThirdPXRatio
        );
        mRoundPath.cubicTo(
                left + radius * firstCSecondPYRatio,
                top + height - radius * firstCSecondPXRatio,
                left,
                top + height - radius * endPointRatio,
                left,
                top + coerceAtLeast((height / 2.0f), height - radius * radiusOffsetRatio)
        );
        // 左边直线和左上角圆角
        mRoundPath.lineTo(left, coerceAtMost((height / 2.0f), radius * radiusOffsetRatio) + top);
        mRoundPath.cubicTo(
                left,
                top + radius * endPointRatio,
                left + radius * firstCSecondPYRatio,
                top + radius * firstCSecondPXRatio,
                left + radius * firstCThirdPYRatio,
                top + radius * firstCThirdPXRatio
        );
        mRoundPath.cubicTo(
                left + radius * secondCFirstPYRatio,
                top + radius * secondCFirstPXRatio,
                left + radius * secondCFirstPXRatio,
                top + radius * secondCFirstPYRatio,
                left + radius * firstCThirdPXRatio,
                top + radius * firstCThirdPYRatio
        );

        mRoundPath.cubicTo(
                left + radius * firstCSecondPXRatio,
                top + radius * firstCSecondPYRatio,
                left + radius * endPointRatio,
                top,
                left + coerceAtMost((width / 2.0f), radius * radiusOffsetRatio),
                top
        );
        mRoundPath.close();
        return mRoundPath;
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值