java - 如何使我的应用程序延迟并在循环的每次迭代中更新布局的组件
问题描述
我正在制作一个棋盘游戏,如果我有 3 个 AI 玩家和一个用户在玩。当用户按下按钮将其轮到 AI 时,我想添加一个延迟,以便用户可以看到每个 AI 所做的每一个动作。
我已经尝试过线程睡眠和处理程序,但似乎他们无法更新板并在 while 循环的每次迭代中添加延迟。可以在下面找到没有任何尝试添加延迟的代码。内部 while 循环终止后,我需要添加延迟。
public void onClickNextTurn(View view){
boolean turnFinished;
findViewById(R.id.btnChangeTurn).setVisibility(View.INVISIBLE);
while (this.playerTurn<4) {
this.playerTurn++;
turnFinished=false;
while (!turnFinished) {
diceRoll();
MoveTriplet result = ai[playerTurn-2].aiTurn(this.boardPieceLocations, this.homeLocations, this.finishLocations, this.roll, playerTurn);
if (result != null) {
if (result.getMoveType() == 'b') { //The Artificial Opponent is moving from one home location to the board AND gets to roll again
this.homeLocations[playerTurn - 1][result.getPreviousIndex()] = 0;
if (this.boardPieceLocations[result.getNextIndex()] != 0)
returnPieceHome(this.boardPieceLocations[result.getNextIndex()]);
this.boardPieceLocations[result.getNextIndex()] = this.playerTurn;
this.homeButtons[playerTurn - 1][result.getPreviousIndex()].setBackgroundColor(getResources().getColor(R.color.colorHoles));
this.buttons[result.getNextIndex()].setBackgroundColor(colors[playerTurn - 1]);
}
因此,如果有人能指出我如何在外部 while 循环的迭代之间添加延迟并同时更新我的板的正确方向。提前致谢!
解决方案
尝试查看标准的 Android 动画 API。那些应该为你处理时间并且更流畅——另外,他们会更有效地为你使用 GPU。
推荐阅读
- javascript - 在 vuejs 中循环访问本地注册的组件
- instagram - Instagram Location API (V1) 目前不可用
- google-cloud-platform - 在 Google Cloud Platform (GCP) 中将数据导入 HDFS
- javascript - 如何在我的 HTML 表格中提供动态 col-span
- python - 在 Python 3 中使用 BeautifulSoup 抓取 URL
- html - Angular CDK 表:元素“td”不能嵌套在元素“表”内
- python - 在 keras 中使用批量标准化进行微调
- python - Django 自定义用户模型:用户创建问题
- google-chrome-extension - Google 日历页面上未触发 Chrome 扩展程序(
在 manifest.json 中) - sql-server - 加密列上的模式匹配