feat(activity): 新增本地图片预览功能及大图查看入口
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -210,6 +210,11 @@ class FoodCollectionActivity : BaseActivity() {
|
||||
|
||||
loadEmptyView()
|
||||
|
||||
// 点击"图片"按钮,跳转至本地图片预览页
|
||||
binding.tvImagePreview.setOnClickListener {
|
||||
startActivity<LocalImagePreviewActivity> { }
|
||||
}
|
||||
|
||||
binding.tvTitle.setOnClickListener {
|
||||
// startActivity<LocalImagePreviewActivity> { }
|
||||
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
package com.sw.inbound.activity
|
||||
|
||||
import android.app.Dialog
|
||||
import android.content.Context
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import android.view.WindowManager
|
||||
import androidx.core.graphics.drawable.toDrawable
|
||||
import com.sw.inbound.base.BaseActivity
|
||||
import androidx.recyclerview.widget.GridLayoutManager
|
||||
import coil.load
|
||||
import com.chad.library.adapter4.BaseQuickAdapter
|
||||
import com.chad.library.adapter4.viewholder.QuickViewHolder
|
||||
import com.sw.inbound.databinding.ActivityLocalImagePreviewBinding
|
||||
import com.sw.inbound.databinding.DialogImageFullPreviewBinding
|
||||
import com.sw.inbound.databinding.ListItemImagePreviewBinding
|
||||
import com.sw.inbound.dialog.Loading
|
||||
import com.sw.inbound.utils.ext.copyText
|
||||
import java.io.File
|
||||
|
||||
class LocalImagePreviewActivity : BaseActivity() {
|
||||
@@ -32,7 +36,7 @@ class LocalImagePreviewActivity : BaseActivity() {
|
||||
loadData()
|
||||
}
|
||||
|
||||
|
||||
/** 从缓存 crop 目录加载所有图片文件 */
|
||||
fun loadData() {
|
||||
Thread {
|
||||
runOnUiThread {
|
||||
@@ -40,17 +44,21 @@ class LocalImagePreviewActivity : BaseActivity() {
|
||||
}
|
||||
val cropFile = File(this.cacheDir, "crop")
|
||||
cropFile.listFiles()?.forEach {
|
||||
if (it.isDirectory) {
|
||||
return@forEach
|
||||
}
|
||||
val start = "IMG_CROP_".length
|
||||
val end = it.name.indexOf(".jpg")
|
||||
val time = it.name.substring(start, end).toLong()
|
||||
if (time >= 1764086400L) {
|
||||
imageList.add(ImageBean(file = it, name = "$time"))
|
||||
// 跳过子目录,只处理文件
|
||||
if (it.isDirectory) return@forEach
|
||||
// 跳过非 jpg 图片
|
||||
if (!it.name.endsWith(".jpg", ignoreCase = true)) return@forEach
|
||||
val nameWithoutExt = it.nameWithoutExtension
|
||||
// 兼容 IMG_CROP_{时间戳} 和其他命名格式
|
||||
val displayName = if (nameWithoutExt.startsWith("IMG_CROP_")) {
|
||||
nameWithoutExt.removePrefix("IMG_CROP_")
|
||||
} else {
|
||||
nameWithoutExt
|
||||
}
|
||||
imageList.add(ImageBean(file = it, name = displayName))
|
||||
}
|
||||
imageList.sortBy { it.name.toLong() }
|
||||
// 按文件名升序排列
|
||||
imageList.sortBy { it.name }
|
||||
runOnUiThread {
|
||||
imageAdapter.notifyDataSetChanged()
|
||||
Loading.dismiss()
|
||||
@@ -58,13 +66,39 @@ class LocalImagePreviewActivity : BaseActivity() {
|
||||
}.start()
|
||||
}
|
||||
|
||||
private var isCycle = true
|
||||
/** 弹出全屏大图预览 Dialog */
|
||||
private fun showFullImagePreview(item: ImageBean) {
|
||||
val dialogBinding = DialogImageFullPreviewBinding.inflate(layoutInflater)
|
||||
val dialog = Dialog(this, android.R.style.Theme_Black_NoTitleBar_Fullscreen)
|
||||
dialog.setContentView(dialogBinding.root)
|
||||
dialog.setCancelable(true)
|
||||
dialog.setCanceledOnTouchOutside(true)
|
||||
dialog.window?.apply {
|
||||
setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
|
||||
setLayout(
|
||||
WindowManager.LayoutParams.MATCH_PARENT,
|
||||
WindowManager.LayoutParams.MATCH_PARENT
|
||||
)
|
||||
}
|
||||
|
||||
// 加载大图
|
||||
dialogBinding.ivFullImage.load(item.file)
|
||||
// 显示文件名
|
||||
dialogBinding.tvFileName.text = item.file.name
|
||||
// 点击关闭按钮关闭弹窗
|
||||
dialogBinding.btnClose.setOnClickListener { dialog.dismiss() }
|
||||
// 点击图片本身也关闭弹窗
|
||||
dialogBinding.ivFullImage.setOnClickListener { dialog.dismiss() }
|
||||
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
private val imageList = mutableListOf<ImageBean>()
|
||||
private val imageAdapter: ImageAdapter by lazy {
|
||||
ImageAdapter(imageList).apply {
|
||||
setOnItemClickListener { _, _, position ->
|
||||
imageList[position].name.copyText(this@LocalImagePreviewActivity)
|
||||
// 点击缩略图,弹出大图预览
|
||||
showFullImagePreview(imageList[position])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,4 +128,4 @@ class LocalImagePreviewActivity : BaseActivity() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user