Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

android - 有没有一种模块化的方法来初始化ImageView数组(is there a modular way to initialize an ImageView array)

I have started a course on android studio and android development, our first task is to create a simple dodge game using linear layouts containing image views for the "objects to dodge from".

(我已经开始了有关android studio和android开发的课程,我们的首要任务是使用线性布局(其中包含“要躲避的对象”的图像视图)创建简单的躲避游戏。)

part of the task was to make the code as modular as possible and here is the problem that I am experiencing,

(任务的一部分是使代码尽可能模块化,这就是我遇到的问题,)

is there a more modular way to initialize an array\matrix of the ImageView?

(有没有更多的模块化方法来初始化ImageView的数组\矩阵?)

this is what I have found to work:

(这是我发现工作的方式:)


public class GameActivity extends AppCompatActivity {
    private int imgId[] = {
            R.id.rocket_0_0, R.id.rocket_0_1, R.id.rocket_0_2, R.id.rocket_1_0, R.id.rocket_1_1, R.id.rocket_1_2, R.id.rocket_2_0, R.id.rocket_2_1,
            R.id.rocket_2_2, R.id.rocket_3_0, R.id.rocket_3_1, R.id.rocket_3_2, R.id.rocket_4_0, R.id.rocket_4_1, R.id.rocket_4_2, R.id.rocket_5_0, R.id.rocket_5_1,
            R.id.rocket_5_2, R.id.rocket_6_0, R.id.rocket_6_1, R.id.rocket_6_2};
    private ImageView [][] imgMatrix;
    private int rows = R.integer.row;
    private int cols = R.integer.col;
    private MyLogic myLogic = new MyLogic();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        imgMatrix = new ImageView[][]
                        {{findViewById(imgId[0]), findViewById(imgId[1]),findViewById(imgId[2])},{findViewById(imgId[3]),findViewById(imgId[4]),findViewById(imgId[5])},
                        {findViewById(imgId[6]),findViewById(imgId[7]),findViewById(imgId[8])},{findViewById(imgId[9]),findViewById(imgId[10]),findViewById(imgId[11])},
                        {findViewById(imgId[12]),findViewById(imgId[13]),findViewById(imgId[14])},{findViewById(imgId[15]),findViewById(imgId[16]),findViewById(imgId[17])},
                        {findViewById(imgId[18]),findViewById(imgId[19]),findViewById(imgId[20])}};

    }
}

and this is what I would have liked to work :) but it crashes :(

(这就是我本想工作的:)但它崩溃了:()

public class GameActivity extends AppCompatActivity {
    private int imgId[] = {
            R.id.rocket_0_0, R.id.rocket_0_1, R.id.rocket_0_2, R.id.rocket_1_0, R.id.rocket_1_1, R.id.rocket_1_2, R.id.rocket_2_0, R.id.rocket_2_1,
            R.id.rocket_2_2, R.id.rocket_3_0, R.id.rocket_3_1, R.id.rocket_3_2, R.id.rocket_4_0, R.id.rocket_4_1, R.id.rocket_4_2, R.id.rocket_5_0, R.id.rocket_5_1,
            R.id.rocket_5_2, R.id.rocket_6_0, R.id.rocket_6_1, R.id.rocket_6_2};
    private ImageView [][] imgMatrix;
    private int rows = R.integer.row;
    private int cols = R.integer.col;
    private MyLogic myLogic = new MyLogic();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        imgMatrix = new ImageView[rows][cols];

        for(int i = 0; i < rows; i++){
             for(int j = 0; j > cols; j++){
                   imgMatrix[i][j] = findViewById(imgId[i*cols+j]);
             }
        }

thanks for reading and helping out if you can :) !

(如果您可以的话,感谢您的阅读和帮助:)!)

  ask by AlonB translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...