如何创建像FaceBook这样的图像的拼贴预览?

时间:2018-04-24 10:54:49

标签: android

我正在尝试在发布图像as shown in image

时实施像Facebook这样的图像拼贴预览

到目前为止,我根据多个图像计数创建了5个布局,然后我根据所选图像列表和在forloop中设置图像来扩充布局,如果有任何选择,我还需要显示文档图标。

这是我实施的代码。

private void displayCollage() {
    rlLayout.setVisibility(View.VISIBLE);
    rlLayout.removeAllViews();
    imagelayout5Binding.tvCount.setVisibility(View.GONE);
    if (attachList.size() > 0) {
        int count = attachList.size();
        if (count > 5) {
            count = 6;
        }

        switch (count) {
            case 1:
                DisplayCollageImage(imagelayout1Binding.ivImage, 0);
                rlLayout.addView(imagelayout1Binding.getRoot());
                break;
            case 2:
                for (int i = 0; i <= attachList.size(); i++) {
                    switch (i) {
                        case 0:
                            DisplayCollageImage(imagelayout2Binding.ivImage1, i);
                            break;
                        case 1:
                            DisplayCollageImage(imagelayout2Binding.ivImage2, i);
                            break;
                    }

                }
                rlLayout.addView(imagelayout2Binding.getRoot());
                break;
            case 3:
                for (int i = 0; i <= attachList.size(); i++) {
                    switch (i) {
                        case 0:
                            DisplayCollageImage(imagelayout3Binding.ivImage1, i);
                            break;
                        case 1:
                            DisplayCollageImage(imagelayout3Binding.ivImage2, i);
                            break;
                        case 2:
                            DisplayCollageImage(imagelayout3Binding.ivImage3, i);
                            break;
                    }

                }
                rlLayout.addView(imagelayout3Binding.getRoot());
                break;
            case 4:
                for (int i = 0; i <= attachList.size(); i++) {
                    switch (i) {
                        case 0:
                            DisplayCollageImage(imagelayout4Binding.ivImage1, i);
                            break;
                        case 1:
                            DisplayCollageImage(imagelayout4Binding.ivImage2, i);
                            break;
                        case 2:
                            DisplayCollageImage(imagelayout4Binding.ivImage3, i);
                            break;
                        case 3:
                            DisplayCollageImage(imagelayout4Binding.ivImage4, i);
                            break;
                    }

                }
                rlLayout.addView(imagelayout4Binding.getRoot());
                break;
            case 5:
                for (int i = 0; i <= attachList.size(); i++) {
                    switch (i) {
                        case 0:
                            DisplayCollageImage(imagelayout5Binding.ivImage1, i);
                            break;
                        case 1:
                            DisplayCollageImage(imagelayout5Binding.ivImage2, i);
                            break;
                        case 2:
                            DisplayCollageImage(imagelayout5Binding.ivImage3, i);
                            break;
                        case 3:
                            DisplayCollageImage(imagelayout5Binding.ivImage4, i);
                            break;
                        case 4:
                            DisplayCollageImage(imagelayout5Binding.ivImage5, i);
                            break;
                    }

                }
                rlLayout.addView(imagelayout5Binding.getRoot());
                break;
            case 6:
                for (int i = 0; i <= attachList.size(); i++) {
                    if (i >= attachList.size()) {
                        i = 5;
                    }
                    switch (i) {
                        case 0:
                            DisplayCollageImage(imagelayout5Binding.ivImage1, i);
                            break;
                        case 1:
                            DisplayCollageImage(imagelayout5Binding.ivImage2, i);
                            break;
                        case 2:
                            DisplayCollageImage(imagelayout5Binding.ivImage3, i);
                            break;
                        case 3:
                            DisplayCollageImage(imagelayout5Binding.ivImage4, i);
                            break;
                        case 4:
                            DisplayCollageImage(imagelayout5Binding.ivImage5, i);
                            break;
                        case 5:
                            DisplayCollageImage(imagelayout5Binding.tvCount);
                            break;
                    }

                    if (i == 5) {
                        break;
                    }

                }
                rlLayout.addView(imagelayout5Binding.getRoot());
                break;
        }

    } else {
        rlLayout.removeAllViews();
    }

}

但这样做需要花费太多的处理时间,请帮助我如何缩短处理时间。

1 个答案:

答案 0 :(得分:0)

如果您正在尝试实现上述输出,那么我建议使用带有StaggeredGrid布局的Recyclerview