From f10c9a9ae776b2f7c0c0dfab2937a12f57f88c04 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Wed, 14 Aug 2024 23:16:42 +0930 Subject: [PATCH] Add buttons to toggle zoom and change number of pages on-screen --- app/src/main/java/com/lhw/pdf/MainActivity.kt | 86 ++++++++++++------- app/src/main/java/com/lhw/pdf/PdfDocument.kt | 8 +- app/src/main/res/layout/app_bar_main.xml | 30 ++++++- 3 files changed, 86 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/com/lhw/pdf/MainActivity.kt b/app/src/main/java/com/lhw/pdf/MainActivity.kt index 7c13b6b..5b39bdb 100644 --- a/app/src/main/java/com/lhw/pdf/MainActivity.kt +++ b/app/src/main/java/com/lhw/pdf/MainActivity.kt @@ -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 { diff --git a/app/src/main/java/com/lhw/pdf/PdfDocument.kt b/app/src/main/java/com/lhw/pdf/PdfDocument.kt index d0a776f..ff93a90 100644 --- a/app/src/main/java/com/lhw/pdf/PdfDocument.kt +++ b/app/src/main/java/com/lhw/pdf/PdfDocument.kt @@ -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) } } \ No newline at end of file diff --git a/app/src/main/res/layout/app_bar_main.xml b/app/src/main/res/layout/app_bar_main.xml index e1c24c3..9153179 100644 --- a/app/src/main/res/layout/app_bar_main.xml +++ b/app/src/main/res/layout/app_bar_main.xml @@ -23,12 +23,38 @@ + android:importantForAccessibility="no" + app:maxImageSize="48dp" + app:srcCompat="@android:drawable/ic_menu_crop" /> + + + + \ No newline at end of file