Skip to content

Instantly share code, notes, and snippets.

@voghDev
Last active December 19, 2018 06:47
Show Gist options
  • Save voghDev/8942ae2eb0904aa2572180fa1f74546b to your computer and use it in GitHub Desktop.
Save voghDev/8942ae2eb0904aa2572180fa1f74546b to your computer and use it in GitHub Desktop.
[Deprecated - see the newer version] PDFPagerAdapter subclass that allows the user to zoom in and out with more image quality. Conversion to Kotlin was very quick and code is not optimal. Suitable for old versions of the library
import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.pdf.PdfRenderer
import android.support.v4.view.ViewPager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.davemorrissey.labs.subscaleview.ImageSource
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
import com.patrimonioglobalgra.audioguiasgranada.R
import es.voghdev.pdfviewpager.library.adapter.BasePDFPagerAdapter
import es.voghdev.pdfviewpager.library.adapter.BitmapContainer
import es.voghdev.pdfviewpager.library.adapter.PdfRendererParams
import es.voghdev.pdfviewpager.library.adapter.SimpleBitmapPool
/**
* Suitable for libraries PdfViewPager v1.0.3 and SubsamplingScaleImageView 3.10.0
* implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.10.0'
* implementation 'es.voghdev.pdfviewpager:library:1.0.3'
*/
class SubsamplingPdfPagerAdapter : BasePDFPagerAdapter {
var inflater: LayoutInflater? = null
var renderer: PdfRenderer? = null
var bitmapContainer: BitmapContainer? = null
var context: Context? = null
constructor(context: Context?, pdfPath: String) : super(context, pdfPath, 1) {
inflater = context?.getSystemService(Activity.LAYOUT_INFLATER_SERVICE) as LayoutInflater
renderer = PdfRenderer(getSeekableFileDescriptor(pdfPath))
val params = extractPdfParams(renderer)
bitmapContainer = SimpleBitmapPool(params)
this.context = context
}
constructor(context: Context?, pdfPath: String, offScreenSize: Int) : super(context, pdfPath, offScreenSize) {
inflater = context?.getSystemService(Activity.LAYOUT_INFLATER_SERVICE) as LayoutInflater
renderer = PdfRenderer(getSeekableFileDescriptor(pdfPath))
val params = extractPdfParams(renderer)
bitmapContainer = SimpleBitmapPool(params)
this.context = context
}
fun extractPdfParams(renderer: PdfRenderer?): PdfRendererParams {
val samplePage: PdfRenderer.Page = getPDFPage(renderer, FIRST_PAGE);
val params = PdfRendererParams()
params.renderQuality = renderQuality
params.offScreenSize = offScreenSize
params.width = (samplePage.width * renderQuality).toInt()
params.height = (samplePage.height * renderQuality).toInt()
samplePage.close()
return params
}
override fun getCount(): Int = 1
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val v = inflater?.inflate(R.layout.view_custom_pdf_page, container, false)
val ssiv: SubsamplingScaleImageView? =
v?.findViewById(R.id.subsamplingImageView) as SubsamplingScaleImageView?
if (renderer == null || getCount() < position) {
return v ?: View(context)
}
val page: PdfRenderer.Page = getPDFPage(renderer, position)
val bitmap: Bitmap? = bitmapContainer?.get(position)
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY)
page.close()
ssiv?.setImage(ImageSource.bitmap(bitmap!!))
(container as ViewPager).addView(v, 0)
return v ?: View(context)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment