Skip to content

Commit 2fe7257

Browse files
authored
Fix bug with measuring behavior (#8)
* Code regions * Fix bug with measuring behavior
1 parent f4868ae commit 2fe7257

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public CameraView(@NonNull Context context, @Nullable AttributeSet attrs) {
9494
init(context, attrs);
9595
}
9696

97+
//region Init
98+
9799
@SuppressWarnings("WrongConstant")
98100
private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
99101
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CameraView, 0, 0);
@@ -200,10 +202,9 @@ protected void onDetachedFromWindow() {
200202
super.onDetachedFromWindow();
201203
}
202204

205+
//endregion
203206

204-
// Smart measuring behavior
205-
// ------------------------
206-
207+
//region Measuring behavior
207208

208209
private String ms(int mode) {
209210
switch (mode) {
@@ -249,7 +250,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
249250
final float previewHeight = flip ? previewSize.getWidth() : previewSize.getHeight();
250251

251252
// If MATCH_PARENT is interpreted as AT_MOST, transform to EXACTLY
252-
// to be consistent with our semantics.
253+
// to be consistent with our semantics (and our docs).
253254
final ViewGroup.LayoutParams lp = getLayoutParams();
254255
if (widthMode == AT_MOST && lp.width == MATCH_PARENT) widthMode = EXACTLY;
255256
if (heightMode == AT_MOST && lp.height == MATCH_PARENT) heightMode = EXACTLY;
@@ -261,7 +262,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
261262

262263
// If we have fixed dimensions (either 300dp or MATCH_PARENT), there's nothing we should do,
263264
// other than respect it. The preview will eventually be cropped at the sides (by PreviewImpl scaling)
264-
// except the case in which these fixed dimensions somehow fit exactly the preview aspect ratio.
265+
// except the case in which these fixed dimensions manage to fit exactly the preview aspect ratio.
265266
if (widthMode == EXACTLY && heightMode == EXACTLY) {
266267
Log.e(TAG, "onMeasure, both are MATCH_PARENT or fixed value. We adapt. This means CROP_INSIDE. " +
267268
"(" + widthValue + "x" + heightValue + ")");
@@ -272,17 +273,20 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
272273
// If both dimensions are free, with no limits, then our size will be exactly the
273274
// preview size. This can happen rarely, for example in scrollable containers.
274275
if (widthMode == UNSPECIFIED && heightMode == UNSPECIFIED) {
275-
Log.e(TAG, "onMeasure, both are completely free. We respect that and extend to the whole preview size. " +
276+
Log.e(TAG, "onMeasure, both are completely free. " +
277+
"We respect that and extend to the whole preview size. " +
276278
"(" + previewWidth + "x" + previewHeight + ")");
277-
super.onMeasure(MeasureSpec.makeMeasureSpec((int) previewWidth, EXACTLY),
279+
super.onMeasure(
280+
MeasureSpec.makeMeasureSpec((int) previewWidth, EXACTLY),
278281
MeasureSpec.makeMeasureSpec((int) previewHeight, EXACTLY));
279282
return;
280283
}
281284

282-
// It sure now that at least one dimension can be determined (either because EXACTLY or AT_MOST).
285+
// It's sure now that at least one dimension can be determined (either because EXACTLY or AT_MOST).
283286
// This starts to seem a pleasant situation.
284287

285-
// If one of the dimension is completely free, take the other and fit the ratio.
288+
// If one of the dimension is completely free (e.g. in a scrollable container),
289+
// take the other and fit the ratio.
286290
// One of the two might be AT_MOST, but we use the value anyway.
287291
float ratio = previewHeight / previewWidth;
288292
if (widthMode == UNSPECIFIED || heightMode == UNSPECIFIED) {
@@ -325,7 +329,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
325329
// Last case, AT_MOST and AT_MOST. Here we can SURELY fit the aspect ratio by filling one
326330
// dimension and adapting the other.
327331
int height, width;
328-
float atMostRatio = heightValue / widthValue;
332+
float atMostRatio = (float) heightValue / (float) widthValue;
329333
if (atMostRatio >= ratio) {
330334
// We must reduce height.
331335
width = widthValue;
@@ -340,10 +344,9 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
340344
MeasureSpec.makeMeasureSpec(height, EXACTLY));
341345
}
342346

347+
//endregion
343348

344-
// Gesture APIs and touch control
345-
// ------------------------------
346-
349+
//region Gesture APIs
347350

348351
/**
349352
* Maps a {@link Gesture} to a certain gesture action.
@@ -468,10 +471,9 @@ private boolean onGesture(GestureLayout source, @NonNull CameraOptions options)
468471
return false;
469472
}
470473

474+
//endregion
471475

472-
// Lifecycle APIs
473-
// --------------
474-
476+
//region Lifecycle APIs
475477

476478
/**
477479
* Returns whether the camera has started showing its preview.
@@ -586,10 +588,9 @@ public void destroy() {
586588
mWorkerHandler = null;
587589
}
588590

591+
//endregion
589592

590-
// Public APIs for parameters and controls
591-
// ---------------------------------------
592-
593+
//region Public APIs for controls
593594

594595
/**
595596
* Returns a {@link CameraOptions} instance holding supported options for this camera
@@ -1158,10 +1159,9 @@ private void requestPermissions(boolean requestCamera, boolean requestAudio) {
11581159
}
11591160
}
11601161

1162+
//endregion
11611163

1162-
// Callbacks and dispatch
1163-
// ----------------------
1164-
1164+
//region Callbacks and dispatching
11651165

11661166
private MediaActionSound mSound;
11671167
private final boolean mUseSounds = Build.VERSION.SDK_INT >= 16;
@@ -1394,10 +1394,9 @@ private void clearListeners() {
13941394
}
13951395
}
13961396

1397+
//endregion
13971398

1398-
// Deprecated stuff
1399-
// ----------------
1400-
1399+
//region Deprecated
14011400

14021401
/**
14031402
* This does nothing.
@@ -1459,4 +1458,5 @@ public int getZoomMode() {
14591458
return 0;
14601459
}
14611460

1461+
//endregion
14621462
}

0 commit comments

Comments
 (0)