首页 > 解决方案 > 单击时如何在矩阵中获取对象

问题描述

我有一个放置在自定义视图中的单元格矩阵。

我想在单击以将颜色从红色更改为绿色时选择单元格。

矩阵

木板

class Board : View {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet): super(context, attrs)

    // Paint object for coloring and styling
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val board_col = Color.CYAN
    private val size = 1000
    private lateinit var board : Array<Array<Cell>>




    override fun onDraw(canvas: Canvas) {
        // call the super method to keep any drawing from the parent side.
        super.onDraw(canvas)
        board(canvas)
        initGrid(canvas)
        grid(canvas)
    }


    private fun board(canvas: Canvas){
        paint.color = board_col
        paint.style = Paint.Style.FILL
        canvas.save()
        val board_rec = Rect(0,0,size,size)
        canvas.drawRect(board_rec, paint)
        canvas.restore()
    }

    private fun initGrid(canvas: Canvas): Array<Array<Cell>> {
        val array = Array(10) { Array(10){Cell(0,0,0,0,0,0, canvas)} }
        var dim = size / 10
        for(i in 0 until array.size) {
            for (j in 0 until array[i].size) {
                var px = i * dim
                var py = j * dim
                array [i][j] = Cell(i, j, px, py, px + dim, py + dim, canvas)
            }
        }
        Log.i("TAG", "initGrid: " + array.size.toString())
       return array
    }



    private fun grid(canvas: Canvas){
        board = initGrid(canvas)
        for(i in 0 until board.size) {
            for (j in 0 until board.size) {
                board[i][j].show()
            }
        }
    }

    //TODO: 19:48 View Pressed
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                for(i in 0 until board.size) {
                    for (j in 0 until board.size) {
                        if (board[i][j].contain(board[i][j].coorX(), board[i][j].coorY())) {
                            Log.i("TAG", "initGrid: " + board[i][j].coorX())

                        }
                    }
                }
                invalidate()
            }
        }
        return super.onTouchEvent(event)
    }

}

这是我填充单元格的 Board 类。

单元格具有以下属性

i,j:矩阵中的位置

px,py:绘制单元格的位置

px + dim, py + dim:单元格显示的大小

细胞

class Cell(var col : Int , var row : Int, x : Int, y: Int, w: Int, h: Int,canvas: Canvas){
    var x = x
    var y = y
    var w= w
    var canvas = canvas

    // Paint object for coloring and styling
    val paint = Paint(Paint.ANTI_ALIAS_FLAG)

    var r = Rect(x,y,w,h)

    fun show(){
        paint.color = Color.BLACK
        paint.style = Paint.Style.FILL
        canvas.save()
        canvas.drawRect(r,paint)
        canvas.restore()
        paint.color = Color.WHITE
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 4.0f
        canvas.save()
        canvas.drawRect(r,paint)
        canvas.restore()
    }

    fun contain(x: Int, y: Int) : Boolean{
        Log.i("TAG", "Location: " + x)

        return (x > this.x && x < this.x + this.w && y > this.y && y < this.y + this.w)
    }

    fun changeBtn(){
        paint.color = Color.RED
        paint.style = Paint.Style.FILL
        canvas.drawRect(r,paint)
        canvas.restore()
    }

    fun coorX(): Int {
        return x
    }

    fun coorY(): Int {
        return y
    }

}

我正在关注本教程,但它是在 JS 中,我所在的时间是 19:30

https://www.youtube.com/watch?v=LFU5ZlrR21E&t=1188s

当 myonTouchEvent()被调用时,它会检查单元格函数contains()并应该返回单元格的坐标(根据视频),然后我可以使用它来更改按钮的颜色。但它在 Kotlin 中不起作用这里是前 10 个坐标的列表。

单击时如何更改单元格颜色?

坐标

标签: androidkotlin

解决方案


推荐阅读