diff --git a/Application/src/main/java/com/xypower/mppreview/AutoFitTextureView.java b/Application/src/main/java/com/xypower/mppreview/AutoFitTextureView.java index f3940f3..d5bf540 100644 --- a/Application/src/main/java/com/xypower/mppreview/AutoFitTextureView.java +++ b/Application/src/main/java/com/xypower/mppreview/AutoFitTextureView.java @@ -18,6 +18,7 @@ package com.xypower.mppreview; import android.content.Context; import android.util.AttributeSet; +import android.util.Log; import android.view.TextureView; /** @@ -52,11 +53,15 @@ public class AutoFitTextureView extends TextureView { if (width < 0 || height < 0) { throw new IllegalArgumentException("Size cannot be negative."); } + + Log.i("AutoFit", "setAspectRatio Width=" + width + " Height=" + height); + if (mRatioWidth == width && mRatioHeight == height) { return; } mRatioWidth = width; mRatioHeight = height; + requestLayout(); } @@ -65,12 +70,17 @@ public class AutoFitTextureView extends TextureView { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); + + if (0 == mRatioWidth || 0 == mRatioHeight) { + Log.i("AutoFit", "0 Width=" + width + " Height=" + height); setMeasuredDimension(width, height); } else { if (width < height * mRatioWidth / mRatioHeight) { + Log.i("AutoFit", "Width=" + width + " Height=" + ((int)(width * mRatioHeight / mRatioWidth))); setMeasuredDimension(width, width * mRatioHeight / mRatioWidth); } else { + Log.i("AutoFit", "Width=" + ((int)height * mRatioWidth / mRatioHeight) + " Height=" + height); setMeasuredDimension(height * mRatioWidth / mRatioHeight, height); } } diff --git a/Application/src/main/java/com/xypower/mppreview/Camera2RawFragment.java b/Application/src/main/java/com/xypower/mppreview/Camera2RawFragment.java index 55628d2..7ca0a19 100644 --- a/Application/src/main/java/com/xypower/mppreview/Camera2RawFragment.java +++ b/Application/src/main/java/com/xypower/mppreview/Camera2RawFragment.java @@ -177,7 +177,7 @@ public class Camera2RawFragment extends Fragment /** * Tag for the {@link Log}. */ - private static final String TAG = "Camera2RawFragment"; + private static final String TAG = "MpPreview"; /** * Camera state: Device is closed. @@ -626,6 +626,7 @@ public class Camera2RawFragment extends Fragment } else { mTextureView.setSurfaceTextureListener(mSurfaceTextureListener); } + if (mOrientationListener != null && mOrientationListener.canDetectOrientation()) { mOrientationListener.enable(); } @@ -1033,7 +1034,7 @@ public class Camera2RawFragment extends Fragment // Swap the view dimensions for calculation as needed if they are rotated relative to // the sensor. - boolean swappedDimensions = totalRotation == 90 || totalRotation == 270; + final boolean swappedDimensions = totalRotation == 90 || totalRotation == 270; int rotatedViewWidth = viewWidth; int rotatedViewHeight = viewHeight; int maxPreviewWidth = displaySize.x; @@ -1056,17 +1057,24 @@ public class Camera2RawFragment extends Fragment } // Find the best preview size for these view dimensions and configured JPEG size. - Size previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), + final Size previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedViewWidth, rotatedViewHeight, maxPreviewWidth, maxPreviewHeight, largestJpeg); - if (swappedDimensions) { - mTextureView.setAspectRatio( - previewSize.getHeight(), previewSize.getWidth()); - } else { - mTextureView.setAspectRatio( - previewSize.getWidth(), previewSize.getHeight()); - } + mMessageHandler.postDelayed(new Runnable() { + @Override + public void run() { + if (swappedDimensions) { + mTextureView.setAspectRatio( + previewSize.getHeight(), previewSize.getWidth()); + } else { + mTextureView.setAspectRatio( + previewSize.getWidth(), previewSize.getHeight()); + } + } + }, 0); + + // Find rotation of device in degrees (reverse device orientation for front-facing // cameras). diff --git a/Application/src/main/res/layout-land/fragment_camera2_basic.xml b/Application/src/main/res/layout-land/fragment_camera2_basic.xml index 76e78de..7ccd3cf 100644 --- a/Application/src/main/res/layout-land/fragment_camera2_basic.xml +++ b/Application/src/main/res/layout-land/fragment_camera2_basic.xml @@ -19,49 +19,48 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + android:background="#000" > - + + - - - - +