Merge pull request #14 from brenard/stable

Allow for uneven grids (columns != rows)
This commit is contained in:
Daniele Verducci 2022-05-13 06:29:44 +00:00 committed by GitHub
commit 333b44cba3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 6 deletions

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="deploymentTargetDropDown">
<targetSelectedWithDropDown>
<Target>
<type value="QUICK_BOOT_TARGET" />
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="$USER_HOME$/.android/avd/Pixel_5_API_30.avd" />
</Key>
</deviceKey>
</Target>
</targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2022-05-02T17:02:45.674283Z" />
</component>
</project>

View File

@ -153,14 +153,14 @@ public class SurveillanceFragment extends Fragment {
Settings settings = Settings.fromDisk(getContext()); Settings settings = Settings.fromDisk(getContext());
List<Camera> cc = settings.getCameras(); List<Camera> cc = settings.getCameras();
int elemsPerSide = calcGridSideElements(cc.size()); int[] gridSize = calcGridDimensionsBasedOnNumberOfElements(cc.size());
int camIdx = 0; int camIdx = 0;
for (int r = 0; r < elemsPerSide; r++) { for (int r = 0; r < gridSize[0]; r++) {
// Create row and add to row container // Create row and add to row container
LinearLayout row = new LinearLayout(getContext()); LinearLayout row = new LinearLayout(getContext());
binding.gridRowContainer.addView(row, rowLayoutParams); binding.gridRowContainer.addView(row, rowLayoutParams);
// Add camera viewers to the row // Add camera viewers to the row
for (int c = 0; c < elemsPerSide; c++) { for (int c = 0; c < gridSize[1]; c++) {
if ( camIdx < cc.size() ) { if ( camIdx < cc.size() ) {
Camera cam = cc.get(camIdx); Camera cam = cc.get(camIdx);
CameraView cv = addCameraView(cam, row); CameraView cv = addCameraView(cam, row);
@ -239,13 +239,22 @@ public class SurveillanceFragment extends Fragment {
} }
/** /**
* Returns the number of elements per side needed to create a grid that can contain the provided elements number. * Returns the dimensions of the grid based on the number of elements.
* Es: to display 3 elements is needed a 4-element grid, with 2 elements per side (a 2x2 grid) * Es: to display 3 elements is needed a 4-element grid, with 2 elements per side (a 2x2 grid)
* Es: to display 6 elements is needed a 9-element grid, with 3 elements per side (a 2x3 grid)
* Es: to display 7 elements is needed a 9-element grid, with 3 elements per side (a 3x3 grid) * Es: to display 7 elements is needed a 9-element grid, with 3 elements per side (a 3x3 grid)
* @param elements * @param elements
*/ */
private int calcGridSideElements(int elements) { private int[] calcGridDimensionsBasedOnNumberOfElements(int elements) {
return (int)(Math.ceil(Math.sqrt(elements))); int rows = 1;
int cols = 1;
while (rows * cols < elements) {
cols += 1;
if (rows * cols >= elements) break;
rows += 1;
}
int[] dimensions = {rows, cols};
return dimensions;
} }
/** /**