【发布时间】:2017-01-30 20:36:04
【问题描述】:
我目前正在制作一个玩益智游戏哈希的安卓应用。我目前正在努力输出游戏网格。我想输出一个像下面这样的二维数组-
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
但是,当我在模拟器中运行应用程序时,它只输出一个空白屏幕。
主要活动-
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new boardView(this));
//sets the view to the board view to show the puzzle on open.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
棋盘类-
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)
public void DrawBoard(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);
}
}
}
}
}
【问题讨论】:
-
你没有在任何地方调用你的 DrawBoard 方法
-
在哪里以及如何称呼它?
-
在您的 boardView 视图中覆盖
void onDraw (Canvas canvas)并将您的代码移到那里 -
这行得通,但是它仍然只输出左上角的第一个椭圆,而没有其他椭圆?
-
请停止重复同样的问题。只需使用您尝试过的任何新信息或代码编辑您的原始帖子,或者解释为什么提供的答案不起作用,它就会被挤到活动队列的顶部。没有必要有多个相同的问题。我已经关闭了其他人作为这个重复,因为你在这里得到了答案和赞成票。
标签: java android android-studio mobile