Commit 4e4c14f5 authored by wanglei's avatar wanglei

...

parent 6f361265
......@@ -4,5 +4,7 @@ import android.graphics.drawable.Drawable
data class PdfPageBean(
val pageIndex: Int = 0,
val pageDrawable: Drawable? = null
)
\ No newline at end of file
var pageDrawable: Drawable? = null
) {
var isSelect: Boolean = false
}
\ No newline at end of file
package com.base.pdfviewerscannerwhite.ui.document.pdf
import android.annotation.SuppressLint
import androidx.activity.addCallback
import com.base.pdfviewerscannerwhite.bean.PdfPageBean
import com.base.pdfviewerscannerwhite.databinding.ActivityPdfBinding
import com.base.pdfviewerscannerwhite.helper.BaseActivity
......@@ -20,6 +22,7 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
override val binding: ActivityPdfBinding by lazy {
ActivityPdfBinding.inflate(layoutInflater)
}
private lateinit var adapter: PdfPagerAdapter
var pafPath = ""
var pageNumber = 0
......@@ -31,18 +34,39 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
pafPath = intent.extras?.getString("path", "") ?: ""
LogEx.logDebug(TAG, "pafPath=$pafPath")
initAdapter()
if (checkStorePermission()) {
pdfPresenter.splitPdf("")
pdfPresenter.iniPdfPage(pafPath)
loadPdf()
} else {
requestStorePermission(launcher) {
pdfPresenter.splitPdf("")
pdfPresenter.iniPdfPage(pafPath)
loadPdf()
}
}
}
override fun initListener() {
super.initListener()
onBackPressedDispatcher.addCallback {
finishToMain()
}
binding.flFanhui.setOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
}
private fun initAdapter() {
adapter = PdfPagerAdapter(pafPath)
binding.rvPager.adapter = adapter
}
private fun loadPdf() {
try {
val file = File(pafPath)
......@@ -56,11 +80,15 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
}
}
@SuppressLint("SetTextI18n")
private fun loadPdfView(file: File, password: String = "") {
binding.pdfview.fromFile(file)
.defaultPage(pageNumber)
.enableAnnotationRendering(true)
.scrollHandle(object : DefaultScrollHandle(this) {})
.onPageChange { page, pageCount ->
binding.tvPageCount.text = "${page + 1}/$pageCount"
}
.spacing(10)
.onPageError { page, t ->
}.password(password).load()
......@@ -72,7 +100,7 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>(), PdfView {
}
override fun initPdfPageRv(items: List<PdfPageBean>) {
adapter.submitList(items)
}
......
package com.base.pdfviewerscannerwhite.ui.document.pdf
import android.annotation.SuppressLint
import android.content.Context
import android.os.Handler
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.pdfviewerscannerwhite.R
import com.base.pdfviewerscannerwhite.bean.PdfPageBean
import com.base.pdfviewerscannerwhite.databinding.ItemPdfPagerBinding
import com.base.pdfviewerscannerwhite.utils.PdfUtils
import com.base.pdfviewerscannerwhite.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
class PdfPagerAdapter(val pdfPath: String) : BaseQuickAdapter<PdfPageBean, PdfPagerAdapter.PdfPagerViewHolder>() {
inner class PdfPagerViewHolder(view: View) : ViewHolder(view)
var corePoolSize = 4 // 核心线程数
var maximumPoolSize = 10 // 最大线程数
var keepAliveTime: Long = 120 // 非核心线程空闲存活时间
var threadPoolExecutor = ThreadPoolExecutor(
corePoolSize,
maximumPoolSize,
keepAliveTime,
TimeUnit.SECONDS,
LinkedBlockingQueue()
)
@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: PdfPagerViewHolder, position: Int, item: PdfPageBean?) {
if (item == null) return
val binding = ItemPdfPagerBinding.bind(holder.itemView)
val context = holder.itemView.context
binding.tvPagerIndex.isSelected = item.isSelect
binding.tvPagerIndex.text = (item.pageIndex + 1).toString()
binding.flBorder.isSelected = item.isSelect
item.pageDrawable?.let {
binding.ivPager.setImageDrawable(it)
}
loadPagerDrawable(context, binding, item)
}
private fun loadPagerDrawable(context: Context, binding: ItemPdfPagerBinding, item: PdfPageBean) {
threadPoolExecutor.execute {
runCatching {
val drawable = PdfUtils.getPdfDrawablePage(context, pdfPath, item.pageIndex)
item.pageDrawable = drawable
// 任务代码
binding.root.post {
item.pageDrawable?.let {
binding.ivPager.setImageDrawable(it)
}
}
}
}
}
override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): PdfPagerViewHolder {
return PdfPagerViewHolder(R.layout.item_pdf_pager.inflate(parent))
}
}
\ No newline at end of file
......@@ -15,8 +15,7 @@ class PdfPresenter(val context: Context, val pdfView: PdfView) {
fun splitPdf(pdfPath: String) = Thread {
val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "DEMO.pdf")
val drawableList = PdfUtils.getPdfDrawables(context, file.absolutePath)
val drawableList = PdfUtils.getPdfDrawables(context, pdfPath)
handler?.post {
context.toast("size=${drawableList.size}")
......
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00B8DE"/>
<corners android:radius="4dp"/>
</shape>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp" />
<solid android:color="#54585B" />
</shape>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#9699A2" />
<corners android:radius="4dp" />
</shape>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_stoke_00bbde_5" android:state_selected="true"/>
<item android:drawable="@drawable/bg_stoke_cfcfcf_5" android:state_selected="false"/>
</selector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/bg_00b8de_4"/>
<item android:state_selected="false" android:drawable="@drawable/bg_9699a2_4"/>
</selector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="#00B8DE" />
<corners android:radius="5dp" />
</shape>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="#CFCFCF" />
<corners android:radius="5dp" />
</shape>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
......@@ -7,26 +7,114 @@
android:fitsSystemWindows="true"
tools:context=".ui.document.pdf.PdfActivity">
<com.google.android.material.appbar.AppBarLayout
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cl_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
android:layout_height="60dp"
app:layout_constraintBottom_toTopOf="@id/pdfview"
app:layout_constraintTop_toTopOf="parent">
<FrameLayout
android:id="@+id/fl_fanhui"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/tv_pdf_name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="15dp"
android:src="@mipmap/fanhui_b"
tools:ignore="ContentDescription" />
</FrameLayout>
<TextView
android:id="@+id/tv_pdf_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="19sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/fl_fanhui"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText"
tools:text="DEMO.pdf" />
<ImageView
android:id="@+id/iv_xuanzhuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:src="@mipmap/hengping"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/iv_search"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
<ImageView
android:id="@+id/iv_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:src="@mipmap/h_sousuo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/iv_more"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
</com.google.android.material.appbar.AppBarLayout>
<ImageView
android:id="@+id/iv_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:src="@mipmap/x_genduo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0E0E0"
app:layout_constraintBottom_toBottomOf="parent" />
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/rv_pager"
app:layout_constraintTop_toBottomOf="@id/cl_top" />
<TextView
android:id="@+id/tv_pageCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginTop="26dp"
android:background="@drawable/bg_54585b_5"
android:includeFontPadding="false"
android:paddingHorizontal="2dp"
android:paddingVertical="2dp"
android:textColor="@color/white"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="@id/pdfview"
app:layout_constraintTop_toTopOf="@id/pdfview"
tools:text="1/3" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_pager"
android:layout_width="match_parent"
android:layout_height="88dp"
android:orientation="horizontal"
android:paddingHorizontal="4dp"
android:paddingTop="5dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/pdfview"
tools:listitem="@layout/item_pdf_pager" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
\ No newline at end of file
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fl_border"
android:layout_width="53dp"
android:layout_height="75dp"
android:layout_margin="4dp"
android:background="@drawable/bg_selector_pager_border">
<ImageView
android:id="@+id/iv_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:importantForAccessibility="no"
android:scaleType="fitXY" />
<TextView
android:id="@+id/tv_pager_index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="4dp"
android:background="@drawable/bg_selector_pager_text"
android:padding="1dp"
android:text="1"
android:textSize="8sp"
tools:ignore="HardcodedText,SmallSp" />
</FrameLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment