I’m having a hard time getting the right ratio to display in a
SurfaceView. At the moment the
SurfaceView is full screen.
I am using
com.google.android.gms.vision.CameraSource and it has a method
setRequestedPreviewSize(width, height). I am not using and would rather not use
CameraX as it’s still in beta.
My problem is that on different devices the preview is stretched. I’ve tried some approaches but didn’t manage to get it right.
My layout looks like this (with some other views that are not relevant):
<SurfaceView android:id="@+id/surfaceView_camera" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> .......
At the moment the best results I managed to get were using something like this:
- use an initial width x height of 1920 x 1080
- calculate the screen ratio using screen height/screen width, resulting in ratio X
- use that ratio, ratio X, to calculate a resized width/height
This works on some of the devices, that have a bigger height/width ratio, but not on devices that have a smaller height/width ratio.
Any tips would help. I’m missing something, but I cannot figure it out. Thank you in advance!