0

所以我有一个android应用程序的功能,它应该拍摄任意数量的图片并为每行显示5张图片,由于某种原因,当它进入第二行时,它只是重复第一行的图片,例如如果我有 7 张编号为 1-7 的图片,它们将显示:12345 12 这是我的功能,感谢您的建议。

public void generateImageView(int number, String path){
        //ImageView[] imageViewArray = new ImageView[number];
        int ROW_ITEMS = 5; // 5 images per row
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.RelativeLayout1);
        int limit = number;//limits the number of created imageViews to number
        int rows = limit / ROW_ITEMS;//number of rows
        int leftOver = limit % ROW_ITEMS; //see if we have incomplete rows
        if (leftOver != 0){
            rows += 1;
        }
        int id = 1000;
        int belowId = R.id.send;
        while (rows > 0){
            int realItemsPerRow = ROW_ITEMS;
            if (leftOver != 0 & rows == 1){
                realItemsPerRow = Math.min(ROW_ITEMS,  leftOver);
            }

        for (int i = 0; i < realItemsPerRow; i++){

            Bitmap myBitmap = BitmapFactory.decodeFile(path + i + ".png");
            ImageView imageViewArray = new ImageView(MainActivity.this);
            imageViewArray.setId(id);
            imageViewArray.setImageBitmap(myBitmap);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            imageViewArray.setPadding(10,10,0,0);
            imageViewArray.setAdjustViewBounds(true);
            imageViewArray.setMaxHeight(80);
            imageViewArray.setMaxWidth(80);
            if (i==0) {
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            } else {
                lp.addRule(RelativeLayout.RIGHT_OF, imageViewArray.getId() -1);
            }
            lp.addRule(RelativeLayout.BELOW, belowId);

            imageViewArray.setLayoutParams(lp);

            rl.addView(imageViewArray);
            id++;
        }
        belowId = id - 1;
        rows--;
4

1 回答 1

1

这是因为您只是为行中的每个位置拉取相同的图像:

Bitmap myBitmap = BitmapFactory.decodeFile(path + i + ".png");

添加前while( rows > 0 )

int j = 0;

然后将上面的内容更改为:

Bitmap myBitmap = BitmapFactory.decodeFile(path + ( j * ROW_ITEMS + i ) + ".png");

并在rows--;添加下:

j++;
于 2012-09-12T20:52:13.950 回答