使用 GridLayout 滑动图像 4x4
Sliding image 4x4 with GridLayout
我想问一下滑动图片拼图。
下面是向左或向右滑动的代码。
但我有一个问题。
if (i + 1 < imageviews.size) {
if (imageviews[i + 1]!!.drawable == null) {
mGridLayout.removeView(imageviews[i + 1])
mGridLayout.addView(imageviews[i + 1], i)
val temp = imageviews[i + 1]
imageviews[i + 1] = imageviews[i]
imageviews[i] = temp
}
}
if (i - 1 >= 0) {
if (imageviews[i - 1]!!.drawable == null) {
mGridLayout.removeView(imageviews[i - 1])
mGridLayout.addView(imageviews[i - 1], i)
val temp = imageviews[i - 1]
imageviews[i - 1] = imageviews[i]
imageviews[i] = temp
}
}
做任何事情之前:
我已经更新了 setOnClickListener()
功能,它似乎可以工作,但我还没有完全解决它:
private fun setOnClickListener() {
Log.i(TAG, "${imageviews.size} ")
for (i in 0 until imageviews.size step 1) {
imageviews[i]!!.setOnClickListener {
Log.i(TAG, "${imageviews.size} $i")
val top: Int? = if (i - GRID_NO >= 0) i - GRID_NO else null
val bottom: Int? = if (i + GRID_NO < imageviews.size) i + GRID_NO else null
val left: Int? = if (i % GRID_NO > 0) i - 1 else null
val right: Int? = if (i % GRID_NO < GRID_NO - 1) i + 1 else null
// Log.i(TAG, "Top: $top Bottom: $bottom Left: $left Right: $right")
if (top != null && imageviews[top]!!.drawable == null) {
imageviews[top]!!.setImageDrawable(imageviews[i]!!.drawable)
mGridLayout.removeViewAt(i)
mGridLayout.addView(imageviews[i], i)
imageviews[i]!!.setImageDrawable(null)
} else if (bottom != null && imageviews[bottom]!!.drawable == null) {
imageviews[bottom]!!.setImageDrawable(imageviews[i]!!.drawable)
mGridLayout.removeViewAt(i)
mGridLayout.addView(imageviews[i], i)
imageviews[i]!!.setImageDrawable(null)
} else if (left != null && imageviews[left]!!.drawable == null) {
imageviews[left]!!.setImageDrawable(imageviews[i]!!.drawable)
mGridLayout.removeViewAt(i)
mGridLayout.addView(imageviews[i], i)
imageviews[i]!!.setImageDrawable(null)
} else if (right != null && imageviews[right]!!.drawable == null) {
imageviews[right]!!.setImageDrawable(imageviews[i]!!.drawable)
mGridLayout.removeViewAt(i)
mGridLayout.addView(imageviews[i], i)
imageviews[i]!!.setImageDrawable(null)
}
if (isSolved()) {
Toast.makeText(this, "Success , true", Toast.LENGTH_SHORT).show()
}
}
}
}
根据请求更新 isSolved() 功能
更新后的 isSolved()
如下所示:
private fun isSolved(): Boolean {
var solved = true
for (i in 0 until GRID_NO) {
for (j in 0 until GRID_NO) {
val index: Int = i * GRID_NO + j
if (imageviews[index]!!.drawable == null) {
continue
} else if (imageviews[index]!!.drawable != imageViews[i][j]!!.drawable) {
solved = false
break
}
}
}
return solved
}
请注意,此版本 returns 如果除空单元格以外的所有单元格都位于正确的位置,则为真,这意味着空单元格可以是末尾的任何单元格。如果这类谜题通常有更严格的成功规则,比如空单元格应该在角落,则需要更新。
我想问一下滑动图片拼图。 下面是向左或向右滑动的代码。 但我有一个问题。
if (i + 1 < imageviews.size) {
if (imageviews[i + 1]!!.drawable == null) {
mGridLayout.removeView(imageviews[i + 1])
mGridLayout.addView(imageviews[i + 1], i)
val temp = imageviews[i + 1]
imageviews[i + 1] = imageviews[i]
imageviews[i] = temp
}
}
if (i - 1 >= 0) {
if (imageviews[i - 1]!!.drawable == null) {
mGridLayout.removeView(imageviews[i - 1])
mGridLayout.addView(imageviews[i - 1], i)
val temp = imageviews[i - 1]
imageviews[i - 1] = imageviews[i]
imageviews[i] = temp
}
}
做任何事情之前:
我已经更新了 setOnClickListener()
功能,它似乎可以工作,但我还没有完全解决它:
private fun setOnClickListener() {
Log.i(TAG, "${imageviews.size} ")
for (i in 0 until imageviews.size step 1) {
imageviews[i]!!.setOnClickListener {
Log.i(TAG, "${imageviews.size} $i")
val top: Int? = if (i - GRID_NO >= 0) i - GRID_NO else null
val bottom: Int? = if (i + GRID_NO < imageviews.size) i + GRID_NO else null
val left: Int? = if (i % GRID_NO > 0) i - 1 else null
val right: Int? = if (i % GRID_NO < GRID_NO - 1) i + 1 else null
// Log.i(TAG, "Top: $top Bottom: $bottom Left: $left Right: $right")
if (top != null && imageviews[top]!!.drawable == null) {
imageviews[top]!!.setImageDrawable(imageviews[i]!!.drawable)
mGridLayout.removeViewAt(i)
mGridLayout.addView(imageviews[i], i)
imageviews[i]!!.setImageDrawable(null)
} else if (bottom != null && imageviews[bottom]!!.drawable == null) {
imageviews[bottom]!!.setImageDrawable(imageviews[i]!!.drawable)
mGridLayout.removeViewAt(i)
mGridLayout.addView(imageviews[i], i)
imageviews[i]!!.setImageDrawable(null)
} else if (left != null && imageviews[left]!!.drawable == null) {
imageviews[left]!!.setImageDrawable(imageviews[i]!!.drawable)
mGridLayout.removeViewAt(i)
mGridLayout.addView(imageviews[i], i)
imageviews[i]!!.setImageDrawable(null)
} else if (right != null && imageviews[right]!!.drawable == null) {
imageviews[right]!!.setImageDrawable(imageviews[i]!!.drawable)
mGridLayout.removeViewAt(i)
mGridLayout.addView(imageviews[i], i)
imageviews[i]!!.setImageDrawable(null)
}
if (isSolved()) {
Toast.makeText(this, "Success , true", Toast.LENGTH_SHORT).show()
}
}
}
}
根据请求更新 isSolved() 功能
更新后的 isSolved()
如下所示:
private fun isSolved(): Boolean {
var solved = true
for (i in 0 until GRID_NO) {
for (j in 0 until GRID_NO) {
val index: Int = i * GRID_NO + j
if (imageviews[index]!!.drawable == null) {
continue
} else if (imageviews[index]!!.drawable != imageViews[i][j]!!.drawable) {
solved = false
break
}
}
}
return solved
}
请注意,此版本 returns 如果除空单元格以外的所有单元格都位于正确的位置,则为真,这意味着空单元格可以是末尾的任何单元格。如果这类谜题通常有更严格的成功规则,比如空单元格应该在角落,则需要更新。