在画布上的图标下方绘制文本

时间:2019-06-18 11:19:33

标签: android canvas android-drawable

我想在可绘制图标下方添加一个Textview

我的代码是

    override fun onChildDraw(
    c: Canvas,
    recyclerView: RecyclerView,
    viewHolder: RecyclerView.ViewHolder,
    dX: Float,
    dY: Float,
    actionState: Int,
    isCurrentlyActive: Boolean
    ) {

    val itemView = viewHolder.itemView
    val itemHeight = itemView.bottom - itemView.top
    val isCanceled = dX == 0f && !isCurrentlyActive

    if (isCanceled) {
        clearCanvas(
            c,
            itemView.right + dX,
            itemView.top.toFloat(),
            itemView.right.toFloat(),
            itemView.bottom.toFloat()
        )
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
        return
    }

    background.setColor(backgroundColor)
    background.cornerRadius = 10f
    background.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
    background.draw(c)

    // Calculate position of delete icon
    val deleteIconTop = itemView.top + (itemHeight - intrinsicHeight) / 2
    val deleteIconMargin = (itemHeight - intrinsicHeight) / 4
    val deleteIconLeft = itemView.right - deleteIconMargin - intrinsicWidth
    val deleteIconRight = itemView.right - deleteIconMargin
    val deleteIconBottom = deleteIconTop + intrinsicHeight

    // Draw the delete icon
    deleteIcon?.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom)
    deleteIcon?.draw(c)
   val rect =  RectF(    itemView.right + dX,
        itemView.top.toFloat(),
        itemView.right.toFloat(),
        itemView.bottom.toFloat())
    super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}

0 个答案:

没有答案