0

我一直在尝试在我的 android 游戏上设置颜色,我使用的代码是:

//画板....

//Define colours for grid lines
    Paint hilite = new Paint();
    hilite.setColor(getResources().getColor(R.color.game_hilite));
    Paint light = new Paint();
    light.setColor(getResources().getColor(R.color.game_light));
    Paint dark = new Paint();
    dark.setColor(getResources().getColor(R.color.game_dark));``

我已将 color.xml 代码中的颜色设置为:

<resources>
  <color name="game_background">#ffe6f0ff</color>
    <color name="game_hilite">#ffffffff</color>
    <color name="game_light">#64c6d4ef</color>
    <color name="game_dark">#6456648f</color>
    <color name="game_foreground">#ff000000</color>
    <color name="game_selected">#64ff8000</color>
</resources>`

只是似乎看不出哪里出了问题,粗体代码是我似乎遇到问题多标记错误的地方。

我拥有的完整代码部分是:

         @Override
        protected void onDraw(Canvas canvas) {
        //Draw the background...
        Paint background = new Paint();
        background.setColor(getResources()
                    .getColor(R.color.game_background));
        canvas.drawRect(0, 0, getWidth(), getHeight(), background);

        //Draw the board
        //Draw the selection
    }

    //Draw the board....
    //Define colours for grid lines
    Paint hilite = new Paint();
    hilite.setColor(getResources().getColor(R.color.game_hilite));
    Paint light = new Paint();
    light.setColor(getResources().getColor(R.color.game_light));
    Paint dark = new Paint();
    dark.setColor(getResources().getColor(R.color.game_dark));


    //Draw the minor grid lines
    Canvas canvas;
    for (int i = 0; i <9; i++) {
        canvas.drawLine(0, i * height, getWidth(), i * height, light);
        canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, hilite);
        canvas.drawLine(i * width, 0, i * width, getHeight(), light);
        canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(), hilite);  
    }
    //Draw the major grid lines
    for (int i = 0; i < 9; i++) {
        if (i % 3 != 0)
            continue;
        canvas.drawLine(0, i * height, getWidth(), i * height, dark);
        canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, hilite);
        canvas.drawLine(i * width, 0, i * width, getHeight(), dark);
        canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(), hilite);
    }

}

4

0 回答 0