如何解决ShapeDrawable以编程方式绘制切边的圆
我想在PreviewView
上点击画一个相机对焦圈。所以,我写了一些代码来画一个像下面的圆圈
val sd = ShapeDrawable(OvalShape())
sd.paint.color = Color.parseColor("#ffffff")
sd.paint.style = Paint.Style.STROKE
sd.paint.strokeWidth = 20f
var img: ImageView = ImageView(this);
img.background = sd
val params = FrameLayout.LayoutParams(250,250)
params.leftMargin = event.x.toInt() - 125
params.topMargin = event.y.toInt() - 125
idFocusIndicator.removeAllViews()
idFocusIndicator.addView(img,params)
您可以在下面清楚地看到它是如何将圆的四边全部切掉的
我需要一个没有切碎的清晰圆圈!如何实现?
解决方法
这似乎与the one here相同。当您以编程方式而不是用XML创建ShapeDrawable时,它会将笔触宽度的中心放在画布的边缘,因此,您的笔触宽度的一半将被裁剪掉。如果您不能使用XML,则可以使用LayerList为可绘制对象添加一个插图,以免边缘被剪掉。
float strokeWidth = 20f;
val sd = ShapeDrawable(OvalShape()).apply {
paint.color = Color.parseColor("#ffffff")
paint.style = Paint.Style.STROKE
paint.strokeWidth = strokeWidth
}
val layerDrawable = LayerDrawable(arrayOf(sd)).apply {
val inset = strokeWidth / 2f
setLayerInset(0,inset,inset)
}
val img: ImageView = ImageView(this)
img.background = layerDrawable
,
好吧,经过一番研究,我将ShapeDrawable
替换为GradientDrawable
,并且可以正常工作。
但是,我不知道为什么ShapeDrawable
遇到问题。如果有人知道,请在这里评论。
val shape = GradientDrawable()
shape.setShape(GradientDrawable.OVAL)
shape.setColor(Color.TRANSPARENT)
shape.setStroke(20,Color.WHITE)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。