Dev Tyagi
Dev Tyagi

Reputation: 17

How to make grid views in android similar to Instagram?

I know how to crop images in android using 3rd party libraries, But I want to split a single picture into multiple pictures as grids for Instagram Feed according to user preference (3x3, 3x1 etc)

Just like: https://play.google.com/store/apps/details?id=com.instagrid.free&hl=en

How to approach this? Is there any particular library?

Upvotes: -2

Views: 366

Answers (1)

Quang Nguyen Ha
Quang Nguyen Ha

Reputation: 51

Assuming that you have a function to crop image like this:

public Image crop(Image src, int topLeftX, int topLeftY, int bottomRightX, int bottomRightY);

This example code will help you split you picture into R rows and C columns:

public List<Image> split(Image src, int r, int c) {
    List<Image> result = new ArrayList<>();

    int topLeftX, topLeftY, bottomRightX, bottomRightY;

    int res_height = src.height / r;
    int res_width = src.width / c;

    for (int i = 0; i < r; i++) {
        topLeftX = i * res_height;
        bottomRightX = (i + 1) * res_height;
        for (int j = 0; j < c; j++) {
            topLeftY = j * res_width;
            bottomRightY = (j + 1) * res_width;

            result.add(crop(src, topLeftX, topLeftY, bottomRightX, bottomRightY));
        }
    }

    return result;
}

Upvotes: 0

Related Questions