Add buttons to toggle zoom and change number of pages on-screen

This commit is contained in:
Luke Hubmayer-Werner 2024-08-14 23:16:42 +09:30
parent 3c40669bad
commit f10c9a9ae7
3 changed files with 86 additions and 38 deletions

View File

@ -2,11 +2,8 @@ package com.lhw.pdf
import android.app.Presentation
import android.content.Context
import android.graphics.Bitmap
import android.graphics.pdf.PdfRenderer
import android.media.MediaRouter
import android.os.Bundle
import android.os.ParcelFileDescriptor
import android.util.DisplayMetrics
import android.view.Display
import android.view.Menu
@ -29,14 +26,17 @@ import com.lhw.pdf.databinding.ActivityMainBinding
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import kotlin.math.max
import kotlin.math.min
class MainActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityMainBinding
private val thumbnailWidth = 500
private val thumbnailHeight = 700
private var renderAutoCrop = true
private var pagesPerLandscape = 3F
private val presentationDisplayMetrics = DisplayMetrics()
private lateinit var pdfDocument: PdfDocument
private lateinit var presentationScroll: HorizontalScrollView
private lateinit var presentationLayout: LinearLayout
@ -61,6 +61,18 @@ class MainActivity : AppCompatActivity() {
}
private fun updatePresentationImages() {
val maxHeight: Int = presentationDisplayMetrics.heightPixels
val maxWidth: Int = min(maxHeight*5/7, (presentationDisplayMetrics.widthPixels/pagesPerLandscape).toInt())
pdfDocument.renderPagesPresentation(maxWidth, maxHeight, renderAutoCrop)
presentationLayout.removeAllViewsInLayout()
for (bitmap in pdfDocument.bitmapPagesPresentation.values) {
val img = ImageView(this)
img.setImageBitmap(bitmap)
presentationLayout.addView(img)
}
}
private fun presentationView() {
val mediaRouter = getSystemService(Context.MEDIA_ROUTER_SERVICE) as MediaRouter
val presentationDisplay = mediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO)?.presentationDisplay
@ -73,21 +85,14 @@ class MainActivity : AppCompatActivity() {
}
}
val metrics = DisplayMetrics()
presentationDisplay.getMetrics(metrics)
val maxHeight: Int = metrics.heightPixels
val maxWidth: Int = min(maxHeight*5/7, metrics.widthPixels)
pdfDocument.renderPagesPresentation(maxWidth, maxHeight)
@Suppress("DEPRECATION")
presentationDisplay.getMetrics(presentationDisplayMetrics)
presentationLayout = LinearLayout(presentation.context)
for (bitmap in pdfDocument.bitmapPagesPresentation.values) {
val img = ImageView(this)
img.setImageBitmap(bitmap)
presentationLayout.addView(img)
}
presentationScroll = HorizontalScrollView(presentation.context)
presentationScroll.addView(presentationLayout)
val layoutParams = WindowManager.LayoutParams()
presentation.addContentView(presentationScroll, layoutParams)
updatePresentationImages()
presentation.show()
}
}
@ -102,24 +107,6 @@ class MainActivity : AppCompatActivity() {
setSupportActionBar(binding.appBarMain.toolbar)
binding.appBarMain.fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null)
.setAnchorView(R.id.fab).show()
}
val drawerLayout: DrawerLayout = binding.drawerLayout
val navView: NavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_content_main)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
initializePdfDocument()
pdfDocument.renderThumbnails(thumbnailWidth, thumbnailHeight)
val container = binding.appBarMain.contentMain.thumbnailsLayout
@ -137,6 +124,41 @@ class MainActivity : AppCompatActivity() {
0
)
}
binding.appBarMain.crop.setOnClickListener { view ->
renderAutoCrop = !renderAutoCrop
val s = if (renderAutoCrop) "On" else "Off"
Snackbar.make(view, "Toggling Auto Crop $s", Snackbar.LENGTH_LONG)
.setAction("Action", null)
.setAnchorView(R.id.crop).show()
updatePresentationImages()
}
binding.appBarMain.zoomIn.setOnClickListener { view ->
pagesPerLandscape = max(pagesPerLandscape - 0.5F, 1F)
Snackbar.make(view, "Aiming for $pagesPerLandscape pages at a time", Snackbar.LENGTH_LONG)
.setAction("Action", null)
.setAnchorView(R.id.zoom_in).show()
updatePresentationImages()
}
binding.appBarMain.zoomOut.setOnClickListener { view ->
pagesPerLandscape = min(pagesPerLandscape + 0.5F, 10F)
Snackbar.make(view, "Aiming for $pagesPerLandscape pages at a time", Snackbar.LENGTH_LONG)
.setAction("Action", null)
.setAnchorView(R.id.zoom_out).show()
updatePresentationImages()
}
val drawerLayout: DrawerLayout = binding.drawerLayout
val navView: NavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_content_main)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {

View File

@ -134,11 +134,11 @@ class PdfDocument(private val fileCached: File, private val autoCrop: Boolean =
}
}
fun renderPagesMain(width: Int, height: Int) {
renderPagesToMap(bitmapPagesMain, width, height, true)
fun renderPagesMain(width: Int, height: Int, crop: Boolean = true) {
renderPagesToMap(bitmapPagesMain, width, height, crop)
}
fun renderPagesPresentation(width: Int, height: Int) {
renderPagesToMap(bitmapPagesPresentation, width, height, true)
fun renderPagesPresentation(width: Int, height: Int, crop: Boolean = true) {
renderPagesToMap(bitmapPagesPresentation, width, height, crop)
}
}

View File

@ -23,12 +23,38 @@
<include android:id="@+id/content_main" layout="@layout/content_main" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:id="@+id/crop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="@dimen/fab_margin"
android:layout_marginBottom="16dp"
app:srcCompat="@android:drawable/ic_dialog_email" />
android:importantForAccessibility="no"
app:maxImageSize="48dp"
app:srcCompat="@android:drawable/ic_menu_crop" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/zoom_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|start"
android:layout_marginStart="96dp"
android:layout_marginBottom="16dp"
android:importantForAccessibility="no"
app:maxImageSize="48dp"
app:srcCompat="@android:drawable/ic_menu_zoom" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/zoom_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|start"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:importantForAccessibility="no"
android:rotationY="180"
android:rotationX="180"
app:maxImageSize="48dp"
app:srcCompat="@android:drawable/ic_menu_zoom" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>