A
A
Alexander Polyankin2014-08-29 06:43:52
Android
Alexander Polyankin, 2014-08-29 06:43:52

How to set the size of a TextureView for camera preview on Android 4.0+ while maintaining the aspect ratio of the image?

Hello. The goal is to set the dimensions of the TextureView for the camera preview so that the aspect ratio of the TextureView matches the aspect ratio of the best camera preview size. Now I do this:
1. In onResume() I get a camera instance
2. Using DisplayMetrics, I find out the size of the available space on the screen
3. I select the size of the preview image from the camera so that its aspect ratio is closest to the aspect ratio of the phone screen
4. C using the aspect ratio of the preview and the size of the phone screen, I find out the largest size of the TextureView
5. Through setLayoutParams I set the size of the TextureView
6. TextureView with layout_gravity="center" occupies the entire screen of the smartphone, empty spaces remain black.
I wonder if I'm doing everything right, and if there are other (easier) ways?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question