android - 单击时如何在矩阵中获取对象
问题描述
我有一个放置在自定义视图中的单元格矩阵。
我想在单击以将颜色从红色更改为绿色时选择单元格。
木板
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 个坐标的列表。
单击时如何更改单元格颜色?
解决方案
推荐阅读
- asynchronous - Kotlin 延续没有恢复
- c++ - 模板类中的 C++20 类外定义
- python-3.x - 删除熊猫数据框中的列
- php - 如何指定 Symfony 应该与多个内核一起使用哪个路由文件
- linux - 由于缺少 libsapcrypto.so,Sybase 16 startserver 失败
- angular - 是否可以将 *ngIf 和 *ngFor 等结构指令添加到使用渲染器 2 添加到 DOM 的元素中?
- c# - VB 脚本到 C# - 提取 .arch 文件
- python - 根据需要的条件安排时间戳
- ssl - 无法使用 Charles Proxy 加密 Instagram 请求
- linux - 延迟邮件plesk,邮件无法从服务器发送邮件