Monday, 15 August 2022

Stop camera preview from resizing

I have a camera preview with a TextView at the bottom of it that looks like this: screenshot before. Code:

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/preview_container"
            android:layout_height="match_parent"
            android:layout_width="match_parent">

            <androidx.camera.view.PreviewView
                android:id="@+id/preview"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintDimensionRatio="W,4:3"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"/>

            <TextView
                android:id="@+id/test"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="@+id/preview"
                app:layout_constraintStart_toStartOf="@id/preview"
                android:text="TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST" />

        </androidx.constraintlayout.widget.ConstraintLayout>

The above is perfect in portrait mode. But, when the device is rotated, I want the TextView (or any other views) to be at the "new bottom" of the preview. Here's my attempt at it: screenshot after. I simply add the android:rotation values programmatically after rotation. The TextView is successfully moved to the "new bottom" but the problem is that the camera preview's size changed even though I want it to remain intact. How can I fix this? Code:

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/preview_container"
            android:layout_height="match_parent"
            android:layout_width="match_parent"

            android:rotation="90">

            <androidx.camera.view.PreviewView
                android:id="@+id/preview"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintDimensionRatio="W,4:3"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"

                android:rotation="-90"/>

            <TextView
                android:id="@+id/test"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintBottom_toBottomOf="@+id/preview"
                app:layout_constraintStart_toStartOf="@id/preview"
                android:text="TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST" />

        </androidx.constraintlayout.widget.ConstraintLayout>


from Stop camera preview from resizing

No comments:

Post a Comment