【问题标题】:Trouble getting correct correct coordinates in translated Rect无法在翻译的 Rect 中获取正确的坐标
【发布时间】:2022-09-27 10:51:22
【问题描述】:

抱歉,如果这是一个愚蠢的问题,我是 canvas/rect 的新手。

我有以下视图,它使用特定宽度/高度的Bitmap 显示像素网格:

class DrawingView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private lateinit var drawingViewBitmap: Bitmap
    private lateinit var boundingRect: Rect

    private var scaleWidth = 0f
    private var scaleHeight = 0f

    private var bitmapWidth = 15
    private var bitmapHeight = 15

    private val rectPaint = Paint().apply {
        style = Paint.Style.FILL
        color = Color.WHITE
        setShadowLayer(10f,0f, 0f, Color.argb(100, 0, 0, 0))
    }

    private fun setScaleWH() {
        scaleWidth = boundingRect.right.toFloat() / drawingViewBitmap.width.toFloat()
        scaleHeight = boundingRect.bottom.toFloat() / drawingViewBitmap.height.toFloat()
    }

    private fun setBoundingRect() {
        val ratio = if (bitmapWidth > bitmapHeight) {
            bitmapHeight.toDouble() / bitmapWidth.toDouble()
        } else {
            bitmapWidth.toDouble() / bitmapHeight.toDouble()
        }

        val rectW: Int = if (bitmapWidth > bitmapHeight) {
            width
        } else if (bitmapHeight > bitmapWidth) {
            (height * ratio).toInt()
        } else {
            width
        }

        val rectH: Int = if (bitmapWidth > bitmapHeight)  {
            (width * ratio).toInt()
        } else if (bitmapHeight > bitmapWidth) {
            height
        } else {
            width
        }

        val canvasCenter = Point(width / 2, height / 2)

        val left = canvasCenter.x - rectW / 2
        val top = canvasCenter.y - rectH / 2
        val right = canvasCenter.x + rectW / 2
        val bottom = canvasCenter.y + rectH / 2

        boundingRect = Rect(left, top, right, bottom)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)

        if (::drawingViewBitmap.isInitialized) {
            drawingViewBitmap.recycle()
        }

        drawingViewBitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888)

        setBoundingRect()
        setScaleWH()
        requestLayout()
    }

    @SuppressLint(\"ClickableViewAccessibility\")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        val coordinateX = (event.x / scaleWidth).toInt()
        val coordinateY = (event.y / scaleHeight).toInt()

        when (event.action) {
            MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> {
                if (coordinateX in 0 until drawingViewBitmap.width && coordinateY in 0 until drawingViewBitmap.height) {
                    drawingViewBitmap.setPixel(coordinateX, coordinateY, Color.BLACK)
                }
            }

            MotionEvent.ACTION_UP -> {
                drawingViewBitmap.drawTransparent()
            }
        }

        invalidate()

        return true
    }

    override fun onDraw(canvas: Canvas) {
        if (::drawingViewBitmap.isInitialized) {
            canvas.drawRect(boundingRect, rectPaint)
            canvas.drawBitmap(drawingViewBitmap, null, boundingRect, null)
        }
    }
}

每当用户点击屏幕时,我都无法获得正确的坐标。我总是在做这类事情时遇到麻烦

我认为这是因为 Rect 被翻译到中心我只是不确定如何获得它的正确坐标。

例如,当用户点击 (3,3) 时,我得到输出 (2,5)(错误输出)。

有人可以让我知道我到底做错了什么吗?我不知道我在代码中做错了什么。我试图自己找到解决方案/在网上查看,但对于如何将触摸坐标转换为矩形坐标,我一直没有成功。

    标签: android kotlin android-layout android-canvas android-bitmap


    【解决方案1】:

    我通过执行以下操作来修复它:

        override fun onTouchEvent(event: MotionEvent): Boolean {
            when (event.actionMasked) {
                MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> {
                    if (event.x.toInt() in boundingRect.left..boundingRect.right && event.y.toInt() in boundingRect.top..boundingRect.bottom) {
                        val coordinateX = ((event.x - boundingRect.left) / scaleWidth).toInt()
                        val coordinateY = ((event.y - boundingRect.top) / scaleHeight).toInt()
    
                        drawingViewBitmap.setPixel(coordinateX, coordinateY, Color.BLACK)
                        invalidate()
                    }
                }
            }
    
            return true
        }
    
        private fun setScaleWH() {
            scaleWidth = boundingRect.width() / drawingViewBitmap.width.toFloat()
            scaleHeight = boundingRect.height() / drawingViewBitmap.height.toFloat()
        }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 2021-05-16
      • 2014-10-22
      相关资源
      最近更新 更多