当我在模拟器上运行该应用程序时,我在Android Studio上构建了X和O游戏,该应用程序正常运行,但是当我在手机上运行该应用程序时,它的运行速度非常慢,因此我在类似的名为Tick tac toe的应用程序之前就已经建立了相同的代码和逻辑,但只有不同的图像,但是它在我的手机和模拟器上都能快速运行,我怀疑X和O图像的尺寸很大,因此游戏运行缓慢,但是当我检查尺寸时,它的尺寸几乎相似我不知道为什么用Tick tac toe玩游戏还是很慢的,所以游戏还是很慢。
代码:
首先是Tick tac toe:
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="368dp"
android:layout_height="360dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/board"
android:columnCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:rowCount="3">
<ImageView
android:id="@+id/imageView"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:onClick="dropIn"
android:tag="0"
app:layout_column="0"
app:layout_row="0" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:onClick="dropIn"
android:tag="1"
app:layout_column="1"
app:layout_row="0" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:onClick="dropIn"
android:tag="2"
app:layout_column="2"
app:layout_row="0" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="3"
app:layout_column="0"
app:layout_row="1" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="4"
app:layout_column="1"
app:layout_row="1" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="5"
app:layout_column="2"
app:layout_row="1" />
<ImageView
android:id="@+id/imageView9"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="6"
app:layout_column="0"
app:layout_row="2" />
<ImageView
android:id="@+id/imageView10"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="7"
app:layout_column="1"
app:layout_row="2" />
<ImageView
android:id="@+id/imageView11"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="8"
app:layout_column="2"
app:layout_row="2" />
</GridLayout>
<LinearLayout
android:id="@+id/playAgainLayout"
android:layout_width="189dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="100dp"
android:layout_marginRight="8dp"
android:layout_marginStart="100dp"
android:layout_marginTop="8dp"
android:background="#46d65e"
android:orientation="vertical"
android:padding="30dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/gridLayout"
app:layout_constraintHorizontal_bias="0.011"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/winnerMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView"
android:textSize="30sp" />
<Button
android:id="@+id/playAgainButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="playAgain"
android:text="Play Again" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
JAVA
package com.k_pc.tick_tac_toe;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// 0 = yellow, 1 = red;
int activePlayer = 0;
// 2 means unplayed
int [] gameState= {2, 2, 2, 2, 2, 2, 2, 2, 2};
int [] [] winningPositions = {{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6}};
public void dropIn (View view){
ImageView counter = (ImageView) view;
System.out.println(counter.getTag().toString());
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if (gameState[tappedCounter] == 2) {
gameState[tappedCounter] = activePlayer;
counter.setTranslationY(-1000f);
if (activePlayer == 0) {
counter.setImageResource(R.drawable.yellow);
activePlayer = 1;
} else {
counter.setImageResource(R.drawable.red);
activePlayer = 0;
}
counter.animate().translationYBy(1000f).rotation(360f).setDuration(500);
for (int [] winningPosition : winningPositions){
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] != 2){
//Someone has won
String winner = "Red";
if (gameState[winningPosition[0]] == 0){
winner = "Yellow";
}
TextView winnerMeassage = (TextView) findViewById(R.id.winnerMessage);
winnerMeassage.setText(winner + " has won!");
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(View.VISIBLE);
}
}
}
}
public void playAgain (View view){
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(View.INVISIBLE);
// 0 = yellow, 1 = red;
activePlayer = 0;
// 2 means unplayed
for (int i = 0 ; i < gameState.length ; i++){
gameState[i] = 2;
}
// setting all the images source to nothing
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
for(int i = 0 ; i<gridLayout.getChildCount() ; i++){
((ImageView) gridLayout.getChildAt(i)).setImageResource(0);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Tick tac toe的图像: 1. https://imgur.com/f2UW3ZO 2. https://imgur.com/lxSGOh9 3. https://imgur.com/aUCNtOx
第二个X和O:
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="360dp"
android:layout_height="360dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/board"
android:columnCount="3"
android:rowCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_row="0"
android:onClick="dropIn"
android:tag="0" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="1"
android:layout_row="0"
android:onClick="dropIn"
android:tag="1" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="2"
android:layout_row="0"
android:onClick="dropIn"
android:tag="2" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_row="1"
android:onClick="dropIn"
android:tag="3" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="1"
android:layout_marginTop="10dp"
android:layout_row="1"
android:onClick="dropIn"
android:tag="4" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="2"
android:layout_marginTop="10dp"
android:layout_row="1"
android:onClick="dropIn"
android:tag="5"/>
<ImageView
android:id="@+id/imageView7"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="0"
android:layout_marginLeft="10dp"
android:layout_row="2"
android:onClick="dropIn"
android:tag="6"/>
<ImageView
android:id="@+id/imageView8"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="1"
android:layout_row="2"
android:onClick="dropIn"
android:tag="7"/>
<ImageView
android:id="@+id/imageView9"
android:layout_width="115dp"
android:layout_height="115dp"
android:layout_column="2"
android:layout_row="2"
android:onClick="dropIn"
android:tag="8"/>
</GridLayout>
<LinearLayout
android:id="@+id/playAgainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#4be433"
android:orientation="vertical"
android:padding="30dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/winnerMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="30sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:onClick="playAgain"
android:text="PLAY AGAIN" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
JAVA
package com.k_pc.xando;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// 0 = x, 1 = o
int activePlayer = 0;
// 2 means unplayed
int [] gameState ={2, 2, 2, 2, 2, 2, 2, 2, 2};
int [] [] winningPositions = {{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6}};
public void dropIn (View view){
ImageView counter = (ImageView) view;
System.out.println("Tag: "+counter.getTag().toString());
counter.setTranslationY(-1000f);
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if (gameState[tappedCounter] == 2) {
gameState[tappedCounter] = activePlayer;
if (activePlayer == 0) {
counter.setImageResource(R.drawable.x);
activePlayer = 1;
} else {
counter.setImageResource(R.drawable.o);
activePlayer = 0;
}
}
counter.animate().translationYBy(1000f).rotation(720).setDuration(500);
for (int [] winningPosition : winningPositions){
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] != 2){
//Someone has won
String winner = "O";
if (gameState[winningPosition[0]] == 0){
winner = "X";
}
TextView winnerMeassage = (TextView) findViewById(R.id.winnerMessage);
winnerMeassage.setText(winner + " has won!");
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(View.VISIBLE);
}
}
int played = 0;
for (int i = 0; i < gameState.length; i++){
if (gameState[i] != 2){
played++;
}
}
if(played == gameState.length){
TextView winnerMeassage = (TextView) findViewById(R.id.winnerMessage);
winnerMeassage.setText("No one has won!");
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(View.VISIBLE);
}
}
public void playAgain (View view){
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(View.INVISIBLE);
// 0 = yellow, 1 = red;
activePlayer = 0;
// 2 means unplayed
for (int i = 0 ; i < gameState.length ; i++){
gameState[i] = 2;
}
// setting all the images source to nothing
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
for(int i = 0 ; i<gridLayout.getChildCount() ; i++){
((ImageView) gridLayout.getChildAt(i)).setImageResource(0);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
X和O的图像: 1. https://imgur.com/f2UW3ZO 2. https://imgur.com/EqQl6yk 3. https://imgur.com/LzsT0wG
我认为代码没有帮助,因为两者具有相同的代码,但是一个仅比另一个要快,问题可能出在图像上。
这是一个视频,显示了这两个应用程序以及X和O与Tick tac toe相比有多慢。