我的CustomView中有OnTouchEvent,当我按一下框并移动手指时,它会绘制线到ACTION_MOVE的坐标,当父视图裁剪孩子时,绘画工作很顺利,但是当我添加clipChildren =“ false” {时, {3}}。 我尝试运行探查器,但Activity looks like this是相同的。
“ notifyCellSelected”选择框以保存该行并开始绘制新行。我认为问题出在“ dispatchDraw”中。
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent?): Boolean {
when(event?.action) {
MotionEvent.ACTION_DOWN ->{
val hitCell = getHitCell(event.x.toInt(), event.y.toInt())
if (hitCell != null) {
onPatternListener?.onStarted()
notifyCellSelected(hitCell)
}
}
MotionEvent.ACTION_MOVE -> {
handleActionMove(event)
}
MotionEvent.ACTION_UP -> onFinish()
MotionEvent.ACTION_CANCEL -> reset()
else -> return false
}
return true
}
private fun handleActionMove(event: MotionEvent) {
val hitCell = getHitCell(event.x.toInt(), event.y.toInt())
if (hitCell != null) {
if (!selectedCells.contains(hitCell)) {
notifyCellSelected(hitCell)
}
}
lastX = event.x
lastY = event.y
invalidate()
}
private fun notifyCellSelected(cell: Cell) {
selectedCells.add(cell)
onPatternListener?.onProgress(generateSelectedIds())
cell.setState(State.SELECTED)
val center = cell.getCenter()
if (selectedCells.size == 1) {
if (lineStyle == LINE_STYLE_COMMON) {
linePath.moveTo(center.x.toFloat(), center.y.toFloat())
}
} else {
if (lineStyle == LINE_STYLE_COMMON) {
linePath.lineTo(center.x.toFloat(), center.y.toFloat())
} else if (lineStyle == LINE_STYLE_INDICATOR) {
val previousCell = selectedCells[selectedCells.size - 2]
val previousCellCenter = previousCell.getCenter()
val diffX = center.x - previousCellCenter.x
val diffY = center.y - previousCellCenter.y
val radius = cell.getRadius()
val length = Math.sqrt((diffX * diffX + diffY * diffY).toDouble())
linePath.moveTo((previousCellCenter.x + radius * diffX / length).toFloat(), (previousCellCenter.y + radius * diffY / length).toFloat())
linePath.lineTo((center.x - radius * diffX / length).toFloat(), (center.y - radius * diffY / length).toFloat())
val degree = Math.toDegrees(Math.atan2(diffY.toDouble(), diffX.toDouble())) + 90
previousCell.setDegree(degree.toFloat())
previousCell.invalidate()
}
}
}
override fun dispatchDraw(canvas: Canvas?) {
super.dispatchDraw(canvas)
canvas?.drawPath(linePath, linePaint)
if (selectedCells.size > 0) {
if (lineStyle == LINE_STYLE_COMMON) {
val center = selectedCells[selectedCells.size - 1].getCenter()
canvas?.drawLine(center.x.toFloat(), center.y.toFloat(), lastX, lastY, linePaint)
} else if (lineStyle == LINE_STYLE_INDICATOR) {
val lastCell = selectedCells[selectedCells.size - 1]
val lastCellCenter = lastCell.getCenter()
val radius = lastCell.getRadius()
if (!(lastX >= lastCellCenter.x - radius &&
lastX <= lastCellCenter.x + radius &&
lastY >= lastCellCenter.y - radius &&
lastY <= lastCellCenter.y + radius)) {
val diffX = lastX - lastCellCenter.x
val diffY = lastY - lastCellCenter.y
val length = Math.sqrt((diffX * diffX + diffY * diffY).toDouble())
canvas?.drawLine((lastCellCenter.x + radius * diffX / length).toFloat(),
(lastCellCenter.y + radius * diffY / length).toFloat(),
lastX, lastY, linePaint)
}
}
}