2

我正在尝试在 BottomSheet 中实现我的预览视图。一切正常,除了预览大小发生变化,如下图所示。 在此处查看图片

正如您所看到的,当我将预览视图高度设置为 175dp 时,预览正在离开底部工作表。它在运行时更改为 469 dp。下面是我的底页 XML。


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <androidx.camera.view.PreviewView
        android:id="@+id/camera"
        android:layout_width="match_parent"
        android:layout_height="175dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="12dp"
        android:contentDescription="nothing"

        app:scaleType="fitCenter">

    </androidx.camera.view.PreviewView>

    <Button
        android:id="@+id/capture"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="@android:drawable/ic_menu_camera" />
</LinearLayout>

下面是我的 java startCamera 和 bindPreview 函数。

private void startCamera() {

        final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(AddNotesActivity.this);

        cameraProviderFuture.addListener(new Runnable() {
            @Override
            public void run() {
                try {

                    ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
                    bindPreview(cameraProvider);

                } catch (ExecutionException | InterruptedException e) {
                    // No errors need to be handled for this Future.
                    // This should never be reached.
                }
            }
        }, ContextCompat.getMainExecutor(AddNotesActivity.this));
    }

 void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {

        Preview preview = new Preview.Builder()
                .build();

        CameraSelector cameraSelector = new CameraSelector.Builder()
                .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                .build();

        ImageAnalysis imageAnalysis = new ImageAnalysis.Builder()
                .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                .build();

        ImageCapture.Builder builder = new ImageCapture.Builder();

        //Vendor-Extensions (The CameraX extensions dependency in build.gradle)
        HdrImageCaptureExtender hdrImageCaptureExtender = HdrImageCaptureExtender.create(builder);

        // Query if extension is available (optional).
        if (hdrImageCaptureExtender.isExtensionAvailable(cameraSelector)) {
            // Enable the extension if available.
            hdrImageCaptureExtender.enableExtension(cameraSelector);
        }

        final ImageCapture imageCapture = builder
                .setTargetRotation(Surface.ROTATION_0)
                .build();

        mPreviewView.setScaleType(PreviewView.ScaleType.FILL_CENTER);
      
        preview.setSurfaceProvider(mPreviewView.getSurfaceProvider());

        Toast.makeText(this, ""+mPreviewView.getHeight()+"---"+mPreviewView.getWidth(), Toast.LENGTH_SHORT).show();
        Camera camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageAnalysis, imageCapture);

        capture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap image = mPreviewView.getBitmap();
                Toast.makeText(AddNotesActivity.this, "Captured", Toast.LENGTH_SHORT).show();

            }
        });

    }

我需要该预览视图留在底页内。

4

0 回答 0