From 776238a5df94b0fc8d5b5d98bfd329238ef15d65 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Fri, 11 Aug 2017 21:34:15 +0200 Subject: [PATCH] Moved Grid to enums --- README.md | 20 +++---- .../cameraview/CameraConstants.java | 8 --- .../otaliastudios/cameraview/CameraView.java | 14 ++--- .../com/otaliastudios/cameraview/Facing.java | 2 +- .../com/otaliastudios/cameraview/Flash.java | 2 +- .../com/otaliastudios/cameraview/Grid.java | 58 +++++++++++++++---- cameraview/src/main/res/values/attrs.xml | 6 +- .../cameraview/GridLinesLayout.java | 17 +++--- .../cameraview/demo/MainActivity.java | 11 ++-- 9 files changed, 84 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 760c118a..c40c4e65 100644 --- a/README.md +++ b/README.md @@ -294,7 +294,7 @@ Most camera parameters can be controlled through XML attributes or linked method |[`cameraSessionType`](#camerasessiontype)|`setSessionType()`|`picture` `video`|`picture`| |[`cameraFacing`](#camerafacing)|`setFacing()`|`back` `front`|`back`| |[`cameraFlash`](#cameraflash)|`setFlash()`|`off` `on` `auto` `torch`|`off`| -|[`cameraGrid`](#cameragrid)|`setGrid()`|`off` `grid3x3` `grid4x4` `phi`|`off`| +|[`cameraGrid`](#cameragrid)|`setGrid()`|`off` `draw3x3` `draw4x4` `drawPhi`|`off`| |[`cameraCropOutput`](#cameracropoutput)|`setCropOutput()`|`true` `false`|`false`| |[`cameraJpegQuality`](#camerajpegquality)|`setJpegQuality()`|`0 <= n <= 100`|`100`| |[`cameraVideoQuality`](#cameravideoquality)|`setVideoQuality()`|`max480p` `max720p` `max1080p` `max2160p` `highest` `lowest`|`max480p`| @@ -328,21 +328,21 @@ cameraView.setFacing(Facing.FRONT); Flash mode, either off, on, auto or *torch*. ```java -cameraView.setFlash(CameraConstants.FLASH_OFF); -cameraView.setFlash(CameraConstants.FLASH_ON); -cameraView.setFlash(CameraConstants.FLASH_AUTO); -cameraView.setFlash(CameraConstants.FLASH_TORCH); +cameraView.setFlash(Flash.OFF); +cameraView.setFlash(Flash.ON); +cameraView.setFlash(Flash.AUTO); +cameraView.setFlash(Flash.TORCH); ``` #### cameraGrid -Lets you draw grids over the camera preview. Supported values are `off`, `grid3x3` and `grid4x4` for regular grids, and `phi` for a grid based on the golden ratio constant, often used in photography. +Lets you draw grids over the camera preview. Supported values are `off`, `draw3x3` and `draw4x4` for regular grids, and `drawPhi` for a grid based on the golden ratio constant, often used in photography. ```java -cameraView.setGrid(CameraConstants.GRID_OFF); -cameraView.setGrid(CameraConstants.GRID_3X3); -cameraView.setGrid(CameraConstants.GRID_4X4); -cameraView.setGrid(CameraConstants.GRID_PHI); +cameraView.setGrid(Grid.OFF); +cameraView.setGrid(Grid.DRAW_3X3); +cameraView.setGrid(Grid.DRAW_4X4); +cameraView.setGrid(Grid.DRAW_PHI); ``` #### cameraCropOutput diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java index 848c5e72..645a4dc0 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java @@ -5,11 +5,6 @@ public class CameraConstants { public static final int PERMISSION_REQUEST_CODE = 16; - public static final int GRID_OFF = 0; - public static final int GRID_3X3 = 1; - public static final int GRID_4X4 = 2; - public static final int GRID_PHI = 3; - public static final int SESSION_TYPE_PICTURE = 0; public static final int SESSION_TYPE_VIDEO = 1; @@ -30,13 +25,10 @@ public class CameraConstants { static class Defaults { - // TODO: Move to enum classes - static final int DEFAULT_VIDEO_QUALITY = VIDEO_QUALITY_480P; static final int DEFAULT_WHITE_BALANCE = WHITE_BALANCE_AUTO; static final int DEFAULT_SESSION_TYPE = SESSION_TYPE_PICTURE; static final int DEFAULT_JPEG_QUALITY = 100; - static final int DEFAULT_GRID = GRID_OFF; static final boolean DEFAULT_CROP_OUTPUT = false; diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index 31994183..ef022947 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -94,10 +94,10 @@ public class CameraView extends FrameLayout { TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CameraView, 0, 0); Facing facing = Facing.fromValue(a.getInteger(R.styleable.CameraView_cameraFacing, Facing.DEFAULT.value())); Flash flash = Flash.fromValue(a.getInteger(R.styleable.CameraView_cameraFlash, Flash.DEFAULT.value())); + Grid grid = Grid.fromValue(a.getInteger(R.styleable.CameraView_cameraGrid, Grid.DEFAULT.value())); int sessionType = a.getInteger(R.styleable.CameraView_cameraSessionType, Defaults.DEFAULT_SESSION_TYPE); int whiteBalance = a.getInteger(R.styleable.CameraView_cameraWhiteBalance, Defaults.DEFAULT_WHITE_BALANCE); int videoQuality = a.getInteger(R.styleable.CameraView_cameraVideoQuality, Defaults.DEFAULT_VIDEO_QUALITY); - int grid = a.getInteger(R.styleable.CameraView_cameraGrid, Defaults.DEFAULT_GRID); mJpegQuality = a.getInteger(R.styleable.CameraView_cameraJpegQuality, Defaults.DEFAULT_JPEG_QUALITY); mCropOutput = a.getBoolean(R.styleable.CameraView_cameraCropOutput, Defaults.DEFAULT_CROP_OUTPUT); GestureAction tapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureTap, GestureAction.DEFAULT_TAP.value())); @@ -621,13 +621,14 @@ public class CameraView extends FrameLayout { /** * Controls the grids to be drawn over the current layout. * - * @see CameraConstants#GRID_OFF - * @see CameraConstants#GRID_3X3 - * @see CameraConstants#GRID_4X4 + * @see Grid#OFF + * @see Grid#DRAW_3X3 + * @see Grid#DRAW_4X4 + * @see Grid#DRAW_PHI * * @param gridMode desired grid mode */ - public void setGrid(@Grid int gridMode) { + public void setGrid(Grid gridMode) { mGridLinesLayout.setGridMode(gridMode); } @@ -636,8 +637,7 @@ public class CameraView extends FrameLayout { * Gets the current grid mode. * @return the current grid mode */ - @Grid - public int getGrid() { + public Grid getGrid() { return mGridLinesLayout.getGridMode(); } diff --git a/cameraview/src/main/options/com/otaliastudios/cameraview/Facing.java b/cameraview/src/main/options/com/otaliastudios/cameraview/Facing.java index d20c9912..879a9b7c 100644 --- a/cameraview/src/main/options/com/otaliastudios/cameraview/Facing.java +++ b/cameraview/src/main/options/com/otaliastudios/cameraview/Facing.java @@ -4,7 +4,7 @@ package com.otaliastudios.cameraview; /** * Facing value indicates which camera sensor should be used for the current session. * - * @see CameraView#setFacing(int) + * @see CameraView#setFacing(Facing) */ public enum Facing { diff --git a/cameraview/src/main/options/com/otaliastudios/cameraview/Flash.java b/cameraview/src/main/options/com/otaliastudios/cameraview/Flash.java index 28d2a27f..9ef9c337 100644 --- a/cameraview/src/main/options/com/otaliastudios/cameraview/Flash.java +++ b/cameraview/src/main/options/com/otaliastudios/cameraview/Flash.java @@ -4,7 +4,7 @@ package com.otaliastudios.cameraview; /** * Flash value indicates the flash mode to be used. * - * @see CameraView#setFlash(int) + * @see CameraView#setFlash(Flash) */ public enum Flash { diff --git a/cameraview/src/main/options/com/otaliastudios/cameraview/Grid.java b/cameraview/src/main/options/com/otaliastudios/cameraview/Grid.java index 415a2ccc..6475da87 100644 --- a/cameraview/src/main/options/com/otaliastudios/cameraview/Grid.java +++ b/cameraview/src/main/options/com/otaliastudios/cameraview/Grid.java @@ -1,16 +1,54 @@ package com.otaliastudios.cameraview; -import android.support.annotation.IntDef; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; +/** + * Grid values can be used to draw grid lines over the camera preview. + * + * @see CameraView#setGrid(Grid) + */ +public enum Grid { -import static com.otaliastudios.cameraview.CameraConstants.GRID_OFF; -import static com.otaliastudios.cameraview.CameraConstants.GRID_3X3; -import static com.otaliastudios.cameraview.CameraConstants.GRID_4X4; -import static com.otaliastudios.cameraview.CameraConstants.GRID_PHI; -@IntDef({GRID_OFF, GRID_3X3, GRID_4X4, GRID_PHI}) -@Retention(RetentionPolicy.SOURCE) -public @interface Grid { + /** + * No grid is drawn. + */ + OFF(0), + + /** + * Draws a regular, 3x3 grid. + */ + DRAW_3X3(1), + + /** + * Draws a regular, 4x4 grid. + */ + DRAW_4X4(2), + + /** + * Draws a grid respecting the 'phi' constant proportions, + * often referred as to the golden ratio. + */ + DRAW_PHI(3); + + static final Grid DEFAULT = OFF; + + private int value; + + Grid(int value) { + this.value = value; + } + + int value() { + return value; + } + + static Grid fromValue(int value) { + Grid[] list = Grid.values(); + for (Grid action : list) { + if (action.value() == value) { + return action; + } + } + return null; + } } \ No newline at end of file diff --git a/cameraview/src/main/res/values/attrs.xml b/cameraview/src/main/res/values/attrs.xml index 54580969..6578e5bd 100644 --- a/cameraview/src/main/res/values/attrs.xml +++ b/cameraview/src/main/res/values/attrs.xml @@ -49,9 +49,9 @@ - - - + + + diff --git a/cameraview/src/main/views/com/otaliastudios/cameraview/GridLinesLayout.java b/cameraview/src/main/views/com/otaliastudios/cameraview/GridLinesLayout.java index b5a8bde3..d8f16e4f 100644 --- a/cameraview/src/main/views/com/otaliastudios/cameraview/GridLinesLayout.java +++ b/cameraview/src/main/views/com/otaliastudios/cameraview/GridLinesLayout.java @@ -14,7 +14,7 @@ import android.view.View; // TODO animate lines! class GridLinesLayout extends View { - @Grid private int gridMode; + private Grid gridMode; private final Drawable horiz; private final Drawable vert; @@ -40,29 +40,28 @@ class GridLinesLayout extends View { vert.setBounds(0, top, (int) width, bottom); } - @Grid - public int getGridMode() { + public Grid getGridMode() { return gridMode; } - public void setGridMode(@Grid int gridMode) { + public void setGridMode(Grid gridMode) { this.gridMode = gridMode; invalidate(); } private int getLineCount() { switch (gridMode) { - case CameraConstants.GRID_OFF: return 0; - case CameraConstants.GRID_3X3: return 2; - case CameraConstants.GRID_PHI: return 2; - case CameraConstants.GRID_4X4: return 3; + case OFF: return 0; + case DRAW_3X3: return 2; + case DRAW_PHI: return 2; + case DRAW_4X4: return 3; } return 0; } private float getLinePosition(int lineNumber) { int lineCount = getLineCount(); - if (gridMode == CameraConstants.GRID_PHI) { + if (gridMode == Grid.DRAW_PHI) { // 1 = 2x + GRIx // 1 = x(2+GRI) // x = 1/(2+GRI) diff --git a/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java b/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java index 3eea3845..135352fc 100644 --- a/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java +++ b/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java @@ -17,6 +17,7 @@ import com.otaliastudios.cameraview.CameraConstants; import com.otaliastudios.cameraview.CameraListener; import com.otaliastudios.cameraview.CameraView; import com.otaliastudios.cameraview.Facing; +import com.otaliastudios.cameraview.Grid; import com.otaliastudios.cameraview.Size; import java.io.File; @@ -256,12 +257,12 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan RadioGroup.OnCheckedChangeListener gridModeChangedListener = new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { - int grid = CameraConstants.GRID_OFF; + Grid grid = Grid.OFF; switch (checkedId) { - case R.id.gridModeOff: grid = CameraConstants.GRID_OFF; break; - case R.id.gridMode3x3: grid = CameraConstants.GRID_3X3; break; - case R.id.gridMode4x4: grid = CameraConstants.GRID_4X4; break; - case R.id.gridModeGolden: grid = CameraConstants.GRID_PHI; break; + case R.id.gridModeOff: grid = Grid.OFF; break; + case R.id.gridMode3x3: grid = Grid.DRAW_3X3; break; + case R.id.gridMode4x4: grid = Grid.DRAW_4X4; break; + case R.id.gridModeGolden: grid = Grid.DRAW_PHI; break; } camera.setGrid(grid); message("Grid mode changed!", false);