fix: adapt phone share QR to window size
This commit is contained in:
@@ -228,7 +228,7 @@ class MainActivity : ComponentActivity() {
|
||||
) { showReceivedFiles() })
|
||||
root.addView(space(22))
|
||||
root.addView(text("本机设备:$receiverDeviceLabel", 13f, MUTED))
|
||||
root.addView(text("QFT v0.5.0-dev1 · Android 7+ 到最新", 12f, MUTED))
|
||||
root.addView(text("QFT v0.5.0-dev2 · Android 7+ 到最新", 12f, MUTED))
|
||||
setContentView(ScrollView(this).apply { setBackgroundColor(BG); addView(root) })
|
||||
}
|
||||
|
||||
@@ -993,17 +993,27 @@ class MainActivity : ComponentActivity() {
|
||||
phoneShareStatusText?.text = "无法生成配对二维码:${e.message}"
|
||||
return
|
||||
}
|
||||
val qr = PhoneQr.render(encoded, 720)
|
||||
// Use the measured app content area. `resources.displayMetrics` is only
|
||||
// a fallback for the short interval before the decor has completed a
|
||||
// layout pass; using the physical display alone breaks tablets and
|
||||
// split-screen windows because it ignores the actual app bounds.
|
||||
val content = findViewById<View>(android.R.id.content)
|
||||
val windowWidth = content?.width?.takeIf { it > 0 } ?: resources.displayMetrics.widthPixels
|
||||
val windowHeight = content?.height?.takeIf { it > 0 } ?: resources.displayMetrics.heightPixels
|
||||
val qrSide = phoneQrSidePx(windowWidth, windowHeight, resources.displayMetrics.density)
|
||||
val qr = PhoneQr.render(encoded, qrSide)
|
||||
val root = pageColumn()
|
||||
root.addView(text("等待另一台 Android 扫码", 27f, Color.WHITE, true))
|
||||
root.addView(text("$receiverDeviceLabel · ${selectedFiles.size} 个文件", 15f, ACCENT))
|
||||
root.addView(space(12))
|
||||
root.addView(ImageView(this).apply {
|
||||
setImageBitmap(qr)
|
||||
adjustViewBounds = true
|
||||
adjustViewBounds = false
|
||||
scaleType = ImageView.ScaleType.CENTER
|
||||
setBackgroundColor(Color.WHITE)
|
||||
setPadding(dp(8), dp(8), dp(8), dp(8))
|
||||
}, LinearLayout.LayoutParams(-1, -2))
|
||||
}, LinearLayout.LayoutParams(qrSide, qrSide).apply {
|
||||
gravity = Gravity.CENTER_HORIZONTAL
|
||||
})
|
||||
root.addView(space(12))
|
||||
phoneShareStatusText = text(
|
||||
note ?: "QFT 临时网络已就绪;另一台手机扫码后会自动连接并开始接收。",
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.qft.optical
|
||||
|
||||
import kotlin.math.min
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/**
|
||||
* Size a square pairing QR against the *current app window*, not a fixed phone
|
||||
* resolution. The vertical reserve covers page padding, titles, connection
|
||||
* details and the stop button, keeping the whole sender page visible on phones,
|
||||
* tablets and split-window layouts.
|
||||
*/
|
||||
internal fun phoneQrSidePx(windowWidthPx: Int, windowHeightPx: Int, density: Float): Int {
|
||||
val safeDensity = density.takeIf { it.isFinite() && it > 0f } ?: 1f
|
||||
val horizontalPagePadding = (40f * safeDensity).roundToInt()
|
||||
val nonQrVerticalUi = (300f * safeDensity).roundToInt()
|
||||
val widthLimit = (windowWidthPx - horizontalPagePadding).coerceAtLeast(1)
|
||||
val heightLimit = (windowHeightPx - nonQrVerticalUi).coerceAtLeast(1)
|
||||
return min(widthLimit, heightLimit)
|
||||
}
|
||||
Reference in New Issue
Block a user