LibGDX绘制圆角矩形时圆弧缺失点,无法平滑显示求助
解决LibGDX圆角矩形圆弧不平滑的问题
你的问题根源是ShapeRenderer的arc方法默认只使用10个分段绘制圆弧,分段数太少导致边缘出现棱角。可以通过以下两种方式解决:
方法一:调用arc时显式指定分段数
在每个arc调用的最后添加分段数参数(建议设置为30及以上,数值越高越平滑,性能消耗会略增),修改后的代码如下:
class RoundedShape : ShapeRenderer() { fun roundedRect(x: Float, y: Float, width: Float, height: Float, radius: Float) { val segments = 30 // 自定义分段数,可根据需求调整 super.rect(x + radius, y + radius, width - 2 * radius, height - 2 * radius) super.rect(x + radius, y, width - 2 * radius, radius) super.rect(x + width - radius, y + radius, radius, height - 2 * radius) super.rect(x + radius, y + height - radius, width - 2 * radius, radius) super.rect(x, y + radius, radius, height - 2 * radius) // 每个arc调用添加分段数参数 super.arc(x + radius, y + radius, radius, 180f, 90f, segments) super.arc(x + width - radius, y + radius, radius, 270f, 90f, segments) super.arc(x + width - radius, y + height - radius, radius, 0f, 90f, segments) super.arc(x + radius, y + height - radius, radius, 90f, 90f, segments) } }
方法二:全局设置ShapeRenderer的默认分段数
如果不想每个arc都单独加参数,可以在初始化ShapeRenderer后,设置默认的圆弧分段数,后续所有arc调用都会自动使用这个数值:
// 初始化RoundedShape实例时设置 val roundedShape = RoundedShape() roundedShape.setDefaultCircleSegments(30)
另外,如果你的绘制模式是ShapeRenderer.ShapeType.Filled,平滑效果会比Line模式更自然,也可以根据需求切换绘制类型。
内容的提问来源于stack exchange,提问作者jdleung
相关产品推荐
相关产品推荐

