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.
This commit is contained in:
Luke Hubmayer-Werner 2024-08-24 12:45:58 +09:30
parent 96404488f1
commit df3b9ac93b
3 changed files with 56 additions and 9 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="KotlinJpsPluginSettings">
<option name="version" value="1.9.0" />
<option name="version" value="2.0.0" />
</component>
</project>

View File

@ -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<Bitmap>()
private val bitmapCumWidths = mutableListOf<Int>(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..<idxEnd).map(bitmaps::get).forEach(::layoutBitmap)
lazyIdxStart = idxStart
lazyIdxEnd = idxEnd
println("Laying out pages $idxStart up to $idxEnd - scrollable width ${layout.width}")
}
fun setScrollProgress(progress: Float) {
scroll.scrollTo((progress * (layout.width - scroll.width)).toInt(), 0)
scrollProgress = progress
var progressPx = (progress * (bitmapCumWidths.last() - scroll.width)).toInt()
if (lazyScroll) {
layoutVisibleBitmaps()
progressPx -= bitmapCumWidths[lazyIdxStart]
}
println("Scrolling view to x=$progressPx of scrollable ${layout.width} (visible width ${scroll.width})")
scroll.scrollTo(progressPx, 0)
}
}

View File

@ -1,6 +1,6 @@
[versions]
agp = "8.5.2"
kotlin = "1.9.0"
kotlin = "2.0.0"
coreKtx = "1.13.1"
junit = "4.13.2"
junitVersion = "1.2.1"