title: How to mirror the PreviewView in CameraX?
body: <p><strong>Disclaimer: I'm aware of the existence of <a href="https://stackoverflow.com/questions/64999859/mirroring-in-camerax">this question</a>, but it currently stands unresolved and I'm trying to provide extra information without polluting that one with useless answers that won't solve the problem anyway.</strong></p>
  <p>I have a custom device with a front camera that is mirrored by default, so I want to display the preview normally and I need to horizontally flip the content of PreviewView, but I'm stuck. Other people in the past have suggested using <code>PreviewView#setScaleX(-1)</code> but it either doesn't work at all or it needs to be called at a very specific point in the code, which I haven't found yet.</p>
  <p>The code below is a simplified version of <code>CameraFragment.kt</code> in the official <a href="https://github.com/android/camera-samples/blob/main/CameraXBasic/app/src/main/java/com/android/example/cameraxbasic/fragments/CameraFragment.kt" rel="nofollow noreferrer">CameraXBasic</a> example; I've added comments where I've already tired calling <code>viewFinder.scaleX = -1f</code> with no success. Honestly I don't really think that the place makes a difference because if I call it with any value other than 1 it works fine with both <code>scaleX</code> and <code>scaleY</code>, but <strong>it always ignores the negative sign</strong> so it never flips.</p>
  <pre><code>private lateinit var viewFinder: PreviewView
  
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
      ...
      viewFinder = view.findViewById(R.id.view_finder)
      // HERE
      viewFinder.post {
          // HERE
          setupCamera()
      }
  }
  
  private fun setupCamera() {
      val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())
      cameraProviderFuture.addListener(Runnable {
          val cameraProvider = cameraProviderFuture.get()
  
          val cameraSelector = CameraSelector.Builder()
              .requireLensFacing(CameraSelector.LENS_FACING_FRONT)
              .build()
  
         val preview = Preview.Builder()
              .build()
              .also {
                  // HERE
                  it.setSurfaceProvider(viewFinder.surfaceProvider)
              }
  
         cameraProvider.unbindAll()
  
          try {
              cameraProvider.bindToLifecycle(this, cameraSelector, preview)
              // HERE
          } catch (exc: Exception) {
              Log.e(TAG, &quot;Use case binding failed&quot;, exc)
          }
          // HERE
      }, ContextCompat.getMainExecutor(requireContext()))
      // HERE
  }
  </code></pre>