我正在尝试在基于Hashi的Android Studios中制作拼图应用程序。但是我输出网格游戏地图时遇到问题。我通过二维数组进行双重迭代,然后输出椭圆来代替数字。我希望它能输出带有数字的椭圆形网格 -
1 0 0 0 1
0 2 0 0 2
2 0 3 0 1
0 0 0 0 0
0 0 2 0 2
然而,当我在模拟器中运行我的应用程序时,所有输出都是模拟器设备左上角的第一个椭圆。
public class boardView extends View {
public float IslandX;
public float IslandY;
public int islandDiameter;
private Canvas canvas;
public boardView(Context context) {
super(context);
}
int gameBoard[][] = {{0, 1, 0, 0, 1}, {0, 2, 0, 0, 2}, {2, 0, 3, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 2, 0, 2}};
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onDraw(Canvas canvas) {
Paint Island = new Paint();
Island.setStyle(Paint.Style.FILL);
Island.setStyle(Paint.Style.FILL);
float stepX = canvas.getWidth() / 5.f;
float stepY = canvas.getHeight() / 5.f;
for (int i = 0; i < 5; i++) {
for (int R = 0; R < 5; R++) {
IslandX = i * stepX;
IslandY = R * stepY;
if (gameBoard[i][R] == 0) {
Island.setColor(Color.BLUE);
canvas.drawOval(IslandX, IslandY, 50, 50, Island);
} else if (gameBoard[i][R] == 1) {
Island.setColor(Color.BLACK);
canvas.drawOval(IslandX, IslandY, 50, 50, Island);
} else if (gameBoard[i][R] == 2) {
Island.setColor(Color.BLUE);
canvas.drawOval(IslandX, IslandY, 50, 50, Island);
}
}
}
}
}