From df3b9ac93bfb8b85f413181547aa1d38c779a5be Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Sat, 24 Aug 2024 12:45:58 +0930 Subject: [PATCH] Only layout visible pages on presentation on Android 13 and under Android 14 seems to not have offscreen culling problems, but my Android 13 tablet gets very laggy with a medium-sized pdf having everything laid out at once. --- .idea/kotlinc.xml | 2 +- .../main/java/com/lhw/pdf/MyPresentation.kt | 61 ++++++++++++++++--- gradle/libs.versions.toml | 2 +- 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml index fdf8d99..6d0ee1c 100644 --- a/.idea/kotlinc.xml +++ b/.idea/kotlinc.xml @@ -1,6 +1,6 @@ - \ No newline at end of file diff --git a/app/src/main/java/com/lhw/pdf/MyPresentation.kt b/app/src/main/java/com/lhw/pdf/MyPresentation.kt index 6499a85..4fb0292 100644 --- a/app/src/main/java/com/lhw/pdf/MyPresentation.kt +++ b/app/src/main/java/com/lhw/pdf/MyPresentation.kt @@ -14,8 +14,15 @@ import androidx.core.view.WindowInsetsCompat class MyPresentation(outerContext: Context, display: Display) : Presentation(outerContext, display) { val displayMetrics = DisplayMetrics() + private val lazyScroll = android.os.Build.VERSION.SDK_INT < 34 //android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE + private var lazyIdxStart = -1 + private var lazyIdxEnd = -1 + + private val bitmaps = mutableListOf() + private val bitmapCumWidths = mutableListOf(0) private val layout = LinearLayout(this.context) private val scroll = HorizontalScrollView(this.context) + private var scrollProgress = 0f private val layoutParams = WindowManager.LayoutParams() init { @Suppress("DEPRECATION") @@ -33,18 +40,58 @@ class MyPresentation(outerContext: Context, display: Display) : Presentation(out println("Updating presentation images") println("Layout has scale ${layout.scaleX} ${layout.scaleY}") layout.removeAllViewsInLayout() + this.bitmaps.clear() + bitmapCumWidths.clear() + bitmapCumWidths.add(0) bitmaps.forEach { - val img = ImageView(this.context) - img.setImageBitmap(it) - layout.addView(img) - img.minimumWidth = it.width // No idea what broke when I refactored this class - img.minimumHeight = it.height - println("${it.width}x${it.height}") + this.bitmaps.add(it) + bitmapCumWidths.add(it.width + bitmapCumWidths.last()) } + if (lazyScroll) { + lazyIdxStart = -1 + lazyIdxEnd = -1 + layoutVisibleBitmaps() + } else { + layoutAllBitmaps() + } + } + + private fun layoutBitmap(bitmap: Bitmap) { + val img = ImageView(this.context) + img.setImageBitmap(bitmap) + img.minimumWidth = bitmap.width // No idea what broke when I refactored this class + img.minimumHeight = bitmap.height + println("${bitmap.width}x${bitmap.height}") + layout.addView(img) + } + + private fun layoutAllBitmaps() { + bitmaps.forEach(::layoutBitmap) println("Presentation heights ${scroll.height} ${layout.height}") } + private fun layoutVisibleBitmaps() { + val xStart = (scrollProgress * (bitmapCumWidths.last() - scroll.width)).toInt() + val xEnd = xStart + scroll.width + // bitmapCumWidths has a prepended 0 + val idxStart = bitmapCumWidths.indexOfFirst { it > xStart }.let { if (it >= 1) it-1 else 0 } + val idxEnd = bitmapCumWidths.indexOfFirst { it > xEnd }.let { if (it >= 0) it else bitmaps.size} + + layout.removeAllViewsInLayout() + (idxStart..