如何在安卓中制作类似于Instagram的网格视图?



我知道如何使用第三方库在 android 中裁剪图像,但我想根据用户偏好将一张图片拆分为多张图片作为 Instagram Feed 的网格(3x3、3x1 等(

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

如何处理这个问题?有什么特别的库吗?

假设你有一个像这样裁剪图像的函数:

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

此示例代码将帮助您将图片拆分为 R 行和 C 列:

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

最新更新