Android Canvas Rect.contains(x, y) 只触发左上角的 Rect
Android Canvas Rect.contains(x, y) only firing for top left Rect
我在 Canvas
中有一个像素网格(矩形),如下所示:
代码:
class MyCanvasView(context: Context) : View(context) {
private lateinit var extraCanvas: Canvas
private lateinit var extraBitmap: Bitmap
private val spanCount = 10.0
private val rectangles = mutableListOf<RectF>()
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
if (::extraBitmap.isInitialized) extraBitmap.recycle()
extraBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
extraCanvas = Canvas(extraBitmap)
val scale = (w / spanCount)
for (i in 0..spanCount.toInt()) {
for (i_2 in 0..spanCount.toInt()) {
val rect = RectF(
((i * scale).toFloat()),
((i_2 * scale).toFloat()),
scale.toFloat(),
scale.toFloat()
)
Log.d("MY_LOG", "LEFT: ${((i * scale).toFloat())} TOP: ${((i_2 * scale).toFloat())} ")
rectangles.add(rect)
extraCanvas.drawRect(
rect,
Paint().apply {
style = Paint.Style.FILL
color = Color.WHITE
})
}
}
for (i in 0..spanCount.toInt()) {
for (i_2 in 0..spanCount.toInt()) {
extraCanvas.drawRect(
(i * scale).toFloat(),
(i_2 * scale).toFloat(),
scale.toFloat(),
scale.toFloat(),
Paint().apply {
color = Color.GRAY
style = Paint.Style.STROKE
})
}
}
}
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
val x = event.rawX
val y = event.rawY
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
for (rect in rectangles) {
if (rect.contains(x, y)) {
Toast.makeText(context, "Touched", Toast.LENGTH_SHORT).show()
}
}
}
}
return true
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.drawBitmap(extraBitmap, 0f, 0f, null)
}
}
出于某种原因,dispatchTouchEvent
仅针对此处左上角的矩形触发:
...但其他人则不然。
如果有人知道为什么会这样,请告诉我,我是所有这些 Canvas 的新手,所以我知道我的代码中可能有问题。
我相信,因为你的底坐标和右坐标总是一样的:
val left = (i * scale).toFloat()
val top = (i_2 * scale).toFloat()
val rect = RectF(
left,
top,
left + scale.toFloat(),
top + scale.toFloat()
)
我在 Canvas
中有一个像素网格(矩形),如下所示:
代码:
class MyCanvasView(context: Context) : View(context) {
private lateinit var extraCanvas: Canvas
private lateinit var extraBitmap: Bitmap
private val spanCount = 10.0
private val rectangles = mutableListOf<RectF>()
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
if (::extraBitmap.isInitialized) extraBitmap.recycle()
extraBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
extraCanvas = Canvas(extraBitmap)
val scale = (w / spanCount)
for (i in 0..spanCount.toInt()) {
for (i_2 in 0..spanCount.toInt()) {
val rect = RectF(
((i * scale).toFloat()),
((i_2 * scale).toFloat()),
scale.toFloat(),
scale.toFloat()
)
Log.d("MY_LOG", "LEFT: ${((i * scale).toFloat())} TOP: ${((i_2 * scale).toFloat())} ")
rectangles.add(rect)
extraCanvas.drawRect(
rect,
Paint().apply {
style = Paint.Style.FILL
color = Color.WHITE
})
}
}
for (i in 0..spanCount.toInt()) {
for (i_2 in 0..spanCount.toInt()) {
extraCanvas.drawRect(
(i * scale).toFloat(),
(i_2 * scale).toFloat(),
scale.toFloat(),
scale.toFloat(),
Paint().apply {
color = Color.GRAY
style = Paint.Style.STROKE
})
}
}
}
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
val x = event.rawX
val y = event.rawY
when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
for (rect in rectangles) {
if (rect.contains(x, y)) {
Toast.makeText(context, "Touched", Toast.LENGTH_SHORT).show()
}
}
}
}
return true
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.drawBitmap(extraBitmap, 0f, 0f, null)
}
}
出于某种原因,dispatchTouchEvent
仅针对此处左上角的矩形触发:
...但其他人则不然。
如果有人知道为什么会这样,请告诉我,我是所有这些 Canvas 的新手,所以我知道我的代码中可能有问题。
我相信,因为你的底坐标和右坐标总是一样的:
val left = (i * scale).toFloat()
val top = (i_2 * scale).toFloat()
val rect = RectF(
left,
top,
left + scale.toFloat(),
top + scale.toFloat()
)