在我的手机游戏中添加图片

时间:2016-07-08 15:05:35

标签: java android

嗨,伙计们,我正在为我的益智游戏工作,我想添加一个图像,我不知道如何添加,因为我只是一个初学者抱歉,如果我在这里输入我的大部分代码。 如何在我的按钮中为我的滑动益智游戏添加图像?

这是我的代码..

private void makeMove(final Button b) {
    bad_move = true;
    int b_pos , zuk_pos, b1;
    b1 = Integer.parseInt((String) b.getText());
    b_pos=find_pos(b1);
    zuk_pos=find_pos(0);
    switch(zuk_pos){
    case (0):
        if(b_pos == 1|| b_pos == 3)
            bad_move=false;
    break;
    case(1):
        if(b_pos == 0 || b_pos == 2|| b_pos == 4)
            bad_move=false;
    break;
    case(2):
        if(b_pos == 1 || b_pos == 5)
            bad_move=false;
        break;
    case(3):
        if(b_pos == 0 || b_pos == 4 || b_pos == 6)
        bad_move=false;
    break;
    case(4):
        if(b_pos==1||b_pos==3||b_pos==5||b_pos==7)
        bad_move=false;
        break;
    case(5):
        if(b_pos==2||b_pos==4||b_pos==8)
        bad_move=false;
        break;
    case(6):
        if(b_pos==3||b_pos==7)
        bad_move=false;
        break;
    case(7):
        if(b_pos==4||b_pos==6||b_pos==8)
        bad_move=false;
        break;
    case(8):
        if(b_pos==5||b_pos==7)
        bad_move=false;
        break;  
    }
    if(bad_move == true){
        // to be decide
        return;
    }
    cells.remove(b_pos);
    cells.add(b_pos, 0);
    cells.remove(zuk_pos);
    cells.add(zuk_pos, b1);

    fill_grid();
    for(int i = 0;i<9; i++){
        if(cells.get(i) != goal[i]){
            return;
        }
    }
    //to be done for finishing touches 
}
private void fill_grid() {
    for(int i =0; i<9;i++){
        int text = cells.get(i);
AbsoluteLayout.LayoutParams absParams = 
    (AbsoluteLayout.LayoutParams) buttons[text].getLayoutParams();

switch(i){
case(0):
    absParams.x = 5;
absParams.y = 5;
buttons[text].setLayoutParams(absParams);
break;
case(1):

    absParams.x = 110;
    absParams.y = 5;
    buttons[text].setLayoutParams(absParams);
    break;
case(2):

    absParams.x = 215;
    absParams.y = 5;
    buttons[text].setLayoutParams(absParams);
    break;
case(3):

    absParams.x = 5;
    absParams.y = 110;
    buttons[text].setLayoutParams(absParams);
    break;
case(4):

    absParams.x =110;
    absParams.y =110;
    buttons[text].setLayoutParams(absParams);
    break;
case(5):

    absParams.x = 215;
    absParams.y =110;
    buttons[text].setLayoutParams(absParams);
    break;
case(6):

    absParams.x = 5;
    absParams.y = 215;
    buttons[text].setLayoutParams(absParams);
    break;
case(7):

    absParams.x = 110;
    absParams.y = 215;
    buttons[text].setLayoutParams(absParams);
    break;
case(8):

    absParams.x = 215;
    absParams.y = 215;
    buttons[text].setLayoutParams(absParams);
    break;

        }
    }   
}
public int find_pos(int element){
    int i = 0;
    for(i = 0;i<9; i++){
        if(cells.get(i) == element){
            break;
        }
    }
    return i;

}

private Button[] findButtons() {
    Button[] b = new Button[9];

    b[0] = (Button) findViewById(R.id.Button00);
    b[1] = (Button) findViewById(R.id.Button01);
    b[2] = (Button) findViewById(R.id.Button02);
    b[3] = (Button) findViewById(R.id.Button03);
    b[4] = (Button) findViewById(R.id.Button04);
    b[5] = (Button) findViewById(R.id.Button05);
    b[6] = (Button) findViewById(R.id.Button06);
    b[7] = (Button) findViewById(R.id.Button07);
    b[8] = (Button) findViewById(R.id.Button08);
    return b;
}

1 个答案:

答案 0 :(得分:2)

首先将您要使用的任何图像放入可在res文件夹中找到的可绘制文件夹中。

您需要在布局文件夹中使用此代码进入您引用的.XML文件,并使用背景引用此图像。

var green = new SolidColorBrush(Colors.Green);
var transparent = new SolidColorBrush(Colors.Transparent);
private void GridViewRow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
    var row = sender as GridViewRow;
    if (row.Background == green) {
        row.Background = transparent;
    } else {
        row.Background = green;
    }
}

现在您的图片应该在您的按钮上。你可以搞乱这些值,并添加更多以获得你想要的。它应该是相当直接的。