游戏网格没有在Android工作室中输出?

时间:2017-01-30 20:36:05

标签: java android android-studio mobile

我目前正在制作一款能够玩益智游戏的Android应用程序。我目前正在努力输出游戏网格。我想输出像贝洛一样的二维数组 -

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);

                }


            }
        }

        }

}

1 个答案:

答案 0 :(得分:0)

要使您的电路板可见,您必须将代码从Drawboard移动到覆盖的方法onDraw,并且在onCreate方法中,您必须在类boardView的实例上调用invalidate(),该实例调用onDraw方法来更新屏幕,如果可见。