Android clipRect无法按预期裁剪 [英] Android clipRect not clipping as expected

查看:54
本文介绍了Android clipRect无法按预期裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在碰到图标时截断字符串.

I would like to clip the string when it hits the icon.

这意味着该字符串在命中 iconSize + iconMargin 时将被剪切.

This would mean that the string would be clipped when it hit iconSize + iconMargin.

这是我在 onDraw 中拥有的代码,该代码正是这样做的:

This is the code I have in onDraw, which does that exactly:

canvas.save()
canvas.clipRect((itemIconSize + itemIconMargin).toInt(), 0, 0, 0)
canvas.drawText(
      item.title,
      item.rect.centerX() - (itemIconSize / 2 + itemIconMargin),
      item.rect.centerY() - textHeight, paintText
)
canvas.restore()

问题在于该字符串不会被剪切,其行为类似于上图所示.

The problem is that the string doesn't clip and behaves like it is displayed in the image above.

当字符串打到图标时,如何截断字符串?

How can I clip the string when it hits the icon?

推荐答案

canvas.clipRect 采用四个参数作为输入,以画布坐标表示矩形.

canvas.clipRect takes four parameter as input to represent a rectangle in canvas coordinates.

  • left =矩形左上角的X坐标
  • top =矩形上角的Y坐标
  • right =矩形右上角的X坐标
  • bottom =矩形下角的Y坐标

您的示例中的rect的顶部和底部角Y坐标为0,这将导致一个高度为0的矩形.因此,将您的代码更改为以下内容:

The rect in your example has 0 as top and bottom corner Y coordinate and this results in a rectangle with 0 height. So change your code to something like this:

canvas.clipRect((itemIconSize + itemIconMargin).toInt() //left: right side of icon
, 0 // top: top point of the view
, view.width // right: end of the View
, view.height // bottom: bottom of the View
)

这篇关于Android clipRect无法按预期裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆