我为我的recyclerView创建了一个ItemDecoration。我正在画的东西只是一个小圆圈。圆圈(看起来像项目符号点)将用于在水平recyclerView中划分项目。
项目装饰器如下所示:
class MyItemDecoration constructor(context: Context, resId: Int) : RecyclerView.ItemDecoration() {
private var divider: Drawable? = null
init {
//custom divider
divider = ContextCompat.getDrawable(context, resId)
}
override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
drawStuff(c, parent)
}
private fun drawStuff(c: Canvas, parent: RecyclerView) {
divider?.let {
val top = it.intrinsicHeight
val bottom = it.intrinsicWidth
val childCount = parent.childCount
for (i in 0 until childCount) {
val child = parent.getChildAt(i)
val params = child
.layoutParams as RecyclerView.LayoutParams
val left = child.right + params.rightMargin
val right = left + it.intrinsicHeight
it.setBounds(left, top, right, bottom)
it.draw(c)
}
}
}
}
我正在绘制的形状如下:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="@color/red"/>
<size
android:width="4dp"
android:height="4dp"/>
</shape>
一个简单的红色圆圈。
我遇到的问题是我需要在垂直中心绘制圆。现在略有下降。有什么方法可以将画布平移到中心或在中心正确绘制可绘制对象?
我尝试调整边界,但仍无法弄清楚。