gpt4 book ai didi

android - 单词搜索游戏 : How to Select Multiple Items in a GridView By Dragging? (Android)

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:12:19 24 4
gpt4 key购买 nike

这里是程序的详细信息:所以我正在创建一个单词搜索游戏,比如有一堆字母排列成一个正方形,你必须找到并选择其中的单词垂直、水平或对角线。我正在为看板使用一个字符串数组,并使用一个 ArrayAdapter 将这个字符串数组存储在一个 gridview 中,这是我的主要 Activity 布局的基本元素。

问题来了:我该如何做到这一点,以便用户可以将手指拖过他们的选择,选择单词中的所有字母,而无需多次将手指从屏幕上移开?我希望所选单词上的突出显示在玩家选择了一个单词时留在屏幕上,并且我希望当用户没有正确选择单词时,当用户从屏幕上抬起手指时,字母上的突出显示会消失.

最佳答案

我迟到了这个问题,但最近遇到了类似的问题,我决定创建自己的自定义 board view library来解决这个问题。它为您可以做的事情增加了更多的灵 active ,并导致更直接的实现。

这里有一些代码 fragment 展示了它是如何工作的。首先,很容易在自定义 View 的 onDraw() 方法上绘制棋盘网格:

@Override
protected void onDraw(Canvas canvas) {
for (int i = 0; i <= numRows; i++) {
//For custom grid sizes, y can't be equal the board size or the line drawn won't show
int y = Math.min(boardHeight - 1, i * tileSize);
canvas.drawLine(0, y, boardWidth, y, boardPaint);
}

for (int i = 0; i <= numCols; i++) {
//For custom grid sizes, x can't be equal the board size or the line drawn won't show
int x = Math.min(boardWidth - 1, i * tileSize);
canvas.drawLine(x, 0, x, boardHeight, boardPaint);
}
}

如果您有一个 String[][] 字符串数组,这里是您可以设置字母的方法,假设每个图 block 的 View 都已经放在板上:

public void setLetterBoard(String[][] letterBoard) {
if (letterBoard.length != getNumRows()
|| letterBoard[0].length != getNumCols()) {
setBoardSize(letterBoard.length, letterBoard[0].length);
}

int row, col;
for (int i=0; i < getChildCount(); i++) {
row = i / getNumCols();
col = i % getNumCols();

LetterTileView child = (LetterTileView) getChildAt(i);
child.setLetter(letterBoard[row][col]);
}
}

然后你需要更复杂的工作来实际处理触摸和拖动板 block 以选择单词:

@Override
public boolean onTouchEvent(MotionEvent event) {
float X = event.getX();
float Y = event.getY();
int row = (int) (Y / getTileSize());
int col = (int) (X / getTileSize());

View child = getChildAt(row, col);

//Exit on invalid touches
if (event.getActionMasked() != MotionEvent.ACTION_UP
&& (row >= getNumRows()
|| col >= getNumCols()
|| child == null)) {
return true;
}

switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
Tile currentTile = new Tile(row, col, child);
if (currentSelectedWord == null) {
currentSelectedWord = new SelectedWord(currentTile);
} else if (!currentTile.equals(currentSelectedWord.lastTile)
&& currentSelectedWord.isTileValid(currentTile)) {
if (!currentSelectedWord.isTileAllowed(currentTile)) {
//Clear the status of the old selection
updateTiles(currentSelectedWord.selectedTiles, false, false);
//If the current tile is valid but not allowed for the current word selection,
//start a new selection that matches the tile
currentSelectedWord = new SelectedWord(currentSelectedWord.getInitialTile());
}
List<Tile> tiles = getTilesBetween(currentSelectedWord.lastTile, currentTile);
if (tiles.size() > 0) {
currentSelectedWord.addTiles(tiles);
}
}
updateTiles(currentSelectedWord.selectedTiles, true, false);
break;
case MotionEvent.ACTION_UP:
if (currentSelectedWord != null) {
boolean isValidSelection = (listener != null && listener.onWordSelected(currentSelectedWord.toBoardWord()));
updateTiles(currentSelectedWord.selectedTiles, false, isValidSelection);
if (isValidSelection) {
selectedWords.add(currentSelectedWord);
}
currentSelectedWord = null;
}
break;
default:
return false;
}
return true;
}

isTileValid()isTileAllowed() 方法可确保用户尝试选择的图 block 处于允许的方向并且之前未被选择。最后,getTilesBetween() 返回用户触摸的第一个图 block 和他/她当前触摸的图 block 之间的所有有效图 block 。

希望对您有所帮助!

关于android - 单词搜索游戏 : How to Select Multiple Items in a GridView By Dragging? (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17983482/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com