如何将图像链接到数字android?

时间:2013-03-03 15:14:54

标签: android android-layout

嗨,我正在搞滑动拼图,到目前为止一直很好。我有一个工作难题,但现在我正在使用数字没有图像。现在我想用图像作为数字的背景。任何人都可以帮我剪切图像并将它们与数字相关联,这样拼图仍然有用吗?

这是我的数字代码,它是9件拼图

    public 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;
}

public void makeMove(final Button b) {
    bad_move=true;
    int b_text,b_pos,zuk_pos;
    b_text=Integer.parseInt((String) b.getText());
    b_pos=find_pos(b_text);
    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)
    {
        feedbackText.setText("Ongeldige zet");
        return;
    }
    feedbackText.setText("Geldige zet");
    cells.remove(b_pos);
    cells.add(b_pos, 0);
    cells.remove(zuk_pos);
    cells.add(zuk_pos,b_text);


    fill_grid();

    for(int i=0;i<9;i++)
    {
        if(cells.get(i)!=goal[i])
        {
            return;
        }
    }
    feedbackText.setText("Voltooid");
    mChronometer.stop();
    long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();   
    tijd = (int) elapsedMillis;
    mScoreDialog = new ScoreDialog(this,this, 0, convertToTimeFormat(elapsedMillis));
    mScoreDialog.show();
}

public 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;
}

正如您所看到的,我正在验证移动并使用图像按钮。

1 个答案:

答案 0 :(得分:0)

将图片保存在res/drawable img_1.png,... img_10.png;

创建地图:

static final HashMap<Integer,Integer> map = new HashMap<~>();
map.put(1,R.drawable.img_1);
. . .
map.put(10,R.drawable.img_10);

获取数字的可绘制ID:

map.get(7);