Skip to content

Commit 65dd840

Browse files
author
Leshoraa
committed
re-design AddItemActivity and PreviewItemActivity
1 parent dd6855f commit 65dd840

File tree

6 files changed

+586
-282
lines changed

6 files changed

+586
-282
lines changed

app/src/main/java/com/leshoraa/listshop/AddItemActivity.java

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.view.SurfaceHolder;
2020
import android.view.SurfaceView;
2121
import android.view.View;
22+
import android.view.ViewGroup;
2223
import android.view.Window;
2324
import android.view.WindowInsetsController;
2425
import android.view.WindowManager;
@@ -651,34 +652,41 @@ private void openCameraPreview(SurfaceHolder holder) {
651652
if (camera != null) {
652653
releaseCamera();
653654
}
655+
654656
camera = android.hardware.Camera.open();
655657
android.hardware.Camera.Parameters parameters = camera.getParameters();
656-
DisplayMetrics metrics = getResources().getDisplayMetrics();
657-
int targetSize = Math.min(metrics.widthPixels, metrics.heightPixels);
658-
android.hardware.Camera.Size optimalSize = getOptimalPreviewSize(parameters.getSupportedPreviewSizes(), targetSize, targetSize);
658+
659+
android.hardware.Camera.Size optimalSize = getOptimalPreviewSize(parameters.getSupportedPreviewSizes(), 4.0 / 4.0);
659660
if (optimalSize != null) {
660661
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
661662
}
663+
662664
camera.setParameters(parameters);
663-
android.hardware.Camera.CameraInfo camInfo = new android.hardware.Camera.CameraInfo();
664-
android.hardware.Camera.getCameraInfo(android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK, camInfo);
665+
665666
setCameraDisplayOrientation(android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK, camera);
667+
666668
camera.setPreviewDisplay(holder);
667669
camera.startPreview();
668-
android.hardware.Camera.Size previewSize = parameters.getPreviewSize();
669-
float ratio = (float) previewSize.width / previewSize.height;
670-
int surfaceWidth = binding.cameraPreview.getWidth();
671-
int surfaceHeight = binding.cameraPreview.getHeight();
672670

673-
if (surfaceWidth / (float) surfaceHeight > ratio) {
674-
surfaceWidth = (int) (surfaceHeight * ratio);
671+
float targetRatio = 4f / 3f;
672+
int parentWidth = binding.cameraPreview.getWidth();
673+
int parentHeight = binding.cameraPreview.getHeight();
674+
675+
int newWidth, newHeight;
676+
677+
if ((float) parentWidth / parentHeight > targetRatio) {
678+
newHeight = parentHeight;
679+
newWidth = (int) (parentHeight * targetRatio);
675680
} else {
676-
surfaceHeight = (int) (surfaceWidth / ratio);
681+
newWidth = parentWidth;
682+
newHeight = (int) (parentWidth / targetRatio);
677683
}
678-
binding.cameraPreview.getLayoutParams().width = surfaceWidth;
679-
binding.cameraPreview.getLayoutParams().height = surfaceHeight;
680-
binding.cameraPreview.requestLayout();
681684

685+
ViewGroup.LayoutParams params = binding.cameraPreview.getLayoutParams();
686+
params.width = newWidth;
687+
params.height = newHeight;
688+
binding.cameraPreview.setLayoutParams(params);
689+
binding.cameraPreview.requestLayout();
682690

683691
} catch (Exception e) {
684692
Toast.makeText(this, "Camera error: " + e.getMessage(), Toast.LENGTH_LONG).show();
@@ -702,56 +710,42 @@ private void setCameraDisplayOrientation(int cameraId, android.hardware.Camera c
702710
int rotation = displayRotation;
703711
int degrees = 0;
704712
switch (rotation) {
705-
case Surface.ROTATION_0:
706-
degrees = 0;
707-
break;
708-
case Surface.ROTATION_90:
709-
degrees = 90;
710-
break;
711-
case Surface.ROTATION_180:
712-
degrees = 180;
713-
break;
714-
case Surface.ROTATION_270:
715-
degrees = 270;
716-
break;
713+
case Surface.ROTATION_0: degrees = 0; break;
714+
case Surface.ROTATION_90: degrees = 90; break;
715+
case Surface.ROTATION_180: degrees = 180; break;
716+
case Surface.ROTATION_270: degrees = 270; break;
717717
}
718+
718719
int result;
719720
if (info.facing == android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT) {
720721
result = (info.orientation + degrees) % 360;
721722
result = (360 - result) % 360;
722723
} else {
723724
result = (info.orientation - degrees + 360) % 360;
724725
}
726+
725727
camera.setDisplayOrientation(result);
726728
}
727729

728-
private android.hardware.Camera.Size getOptimalPreviewSize(List<android.hardware.Camera.Size> sizes, int targetWidth, int targetHeight) {
730+
private android.hardware.Camera.Size getOptimalPreviewSize(List<android.hardware.Camera.Size> sizes, double targetRatio) {
729731
final double ASPECT_TOLERANCE = 0.1;
730-
double targetRatio = (double) targetWidth / targetHeight;
731-
if (sizes == null) return null;
732732
android.hardware.Camera.Size optimalSize = null;
733733
double minDiff = Double.MAX_VALUE;
734-
int targetDim = Math.min(targetWidth, targetHeight);
735734

736735
for (android.hardware.Camera.Size size : sizes) {
737736
double ratio = (double) size.width / size.height;
738737
if (Math.abs(ratio - targetRatio) < ASPECT_TOLERANCE) {
739-
if (Math.abs(size.height - targetDim) < minDiff) {
738+
if (Math.abs(size.height - 720) < minDiff) {
740739
optimalSize = size;
741-
minDiff = Math.abs(size.height - targetDim);
740+
minDiff = Math.abs(size.height - 720);
742741
}
743742
}
744743
}
745744

746-
if (optimalSize == null) {
747-
minDiff = Double.MAX_VALUE;
748-
for (android.hardware.Camera.Size size : sizes) {
749-
if (Math.abs(size.height - targetDim) < minDiff) {
750-
optimalSize = size;
751-
minDiff = Math.abs(size.height - targetDim);
752-
}
753-
}
745+
if (optimalSize == null && !sizes.isEmpty()) {
746+
optimalSize = sizes.get(0);
754747
}
748+
755749
return optimalSize;
756750
}
757751

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<shape xmlns:android="http://schemas.android.com/apk/res/android"
22
android:shape="rectangle">
33

4-
<solid android:color="#A9E4E4E4" />
4+
<solid android:color="#FFFFFF" />
55

6-
<corners android:radius="14dp" />
6+
<corners android:radius="12dp" />
7+
8+
<stroke android:width="1.5dp" android:color="@color/colorSecondary"/>
79

810
</shape>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:shape="rectangle">
3+
4+
<solid android:color="#FFFFFF" />
5+
6+
<corners android:radius="12dp" />
7+
8+
<stroke android:width="1.5dp" android:color="@color/colorSecPrimary"/>
9+
10+
</shape>

0 commit comments

Comments
 (0)