fix: Fix PreviewView
stretching on Android (now finally a real fix) (#2564)
* fix: Only resolve once SurfaceHolder actually resized * fix: Fix onMeasure not being called for `PreviewView` * fix: Auto-trigger layout computation on Surface Change * fix: Add proper LayoutParams to `PreviewView` * Format
This commit is contained in:
@@ -6,32 +6,36 @@ import androidx.annotation.UiThread
|
||||
import kotlin.coroutines.resume
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
|
||||
private const val TAG = "SurfaceHolder"
|
||||
|
||||
@UiThread
|
||||
suspend fun SurfaceHolder.resize(width: Int, height: Int) {
|
||||
suspend fun SurfaceHolder.resize(targetWidth: Int, targetHeight: Int) {
|
||||
return suspendCancellableCoroutine { continuation ->
|
||||
val currentSize = this.surfaceFrame
|
||||
if (currentSize.width() == width && currentSize.height() == height) {
|
||||
if (currentSize.width() == targetWidth && currentSize.height() == targetHeight) {
|
||||
// Already in target size
|
||||
continuation.resume(Unit)
|
||||
return@suspendCancellableCoroutine
|
||||
}
|
||||
|
||||
Log.i("SurfaceHolder", "Resizing SurfaceHolder to $width x $height...")
|
||||
Log.i(TAG, "Resizing SurfaceHolder to $targetWidth x $targetHeight...")
|
||||
|
||||
val callback = object : SurfaceHolder.Callback {
|
||||
override fun surfaceCreated(holder: SurfaceHolder) = Unit
|
||||
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
|
||||
holder.removeCallback(this)
|
||||
Log.i("SurfaceHolder", "Resized SurfaceHolder to $width x $height!")
|
||||
continuation.resume(Unit)
|
||||
if (width == targetWidth && height == targetHeight) {
|
||||
holder.removeCallback(this)
|
||||
Log.i(TAG, "Resized SurfaceHolder to $width x $height!")
|
||||
continuation.resume(Unit)
|
||||
}
|
||||
}
|
||||
override fun surfaceDestroyed(holder: SurfaceHolder) {
|
||||
holder.removeCallback(this)
|
||||
Log.e("SurfaceHolder", "Failed to resize SurfaceHolder to $width x $height!")
|
||||
Log.e(TAG, "Failed to resize SurfaceHolder to $targetWidth x $targetHeight!")
|
||||
continuation.cancel(Error("Tried to resize SurfaceView, but Surface has been destroyed!"))
|
||||
}
|
||||
}
|
||||
this.addCallback(callback)
|
||||
this.setFixedSize(width, height)
|
||||
this.setFixedSize(targetWidth, targetHeight)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user