Skip to content

Commit

Permalink
feat: 优化缩放体验
Browse files Browse the repository at this point in the history
  • Loading branch information
SherlockGougou committed Dec 30, 2024
1 parent b10e867 commit b731a42
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 249 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import cc.shinichi.library.ImagePreview
import cc.shinichi.library.R
import cc.shinichi.library.glide.FileTarget
import cc.shinichi.library.tool.common.HttpUtil.downloadFile
import cc.shinichi.library.tool.common.ToastUtil
import cc.shinichi.library.tool.file.FileUtil.Companion.copyFile
import cc.shinichi.library.tool.file.FileUtil.Companion.createFileByDeleteOldFile
import cc.shinichi.library.tool.file.FileUtil.Companion.getAvailableCacheDir
import cc.shinichi.library.tool.file.SingleMediaScanner
import cc.shinichi.library.tool.image.ImageUtil.refresh
import cc.shinichi.library.tool.common.ToastUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.request.transition.Transition
import java.io.*
Expand Down
33 changes: 17 additions & 16 deletions library/src/main/java/cc/shinichi/library/tool/image/ImageUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import android.provider.MediaStore
import android.text.TextUtils
import androidx.annotation.RequiresApi
import androidx.exifinterface.media.ExifInterface
import cc.shinichi.library.tool.common.SLog
import cc.shinichi.library.tool.common.PhoneUtil
import cc.shinichi.library.tool.common.SLog
import java.io.*
import java.util.*
import kotlin.math.max
Expand Down Expand Up @@ -168,13 +168,12 @@ object ImageUtil {
return isTablet(context) or isLandscape(context)
}

fun isLongImage(context: Context, imagePath: String): Boolean {
fun isLongImage(imagePath: String): Boolean {
val wh = getWidthHeight(imagePath)
val w = wh[0].toFloat()
val h = wh[1].toFloat()
val imageRatio = h / w
val phoneRatio = PhoneUtil.getPhoneRatio(context)
val isLongImage = h > w && imageRatio > phoneRatio
val isLongImage = h > w && imageRatio >= 3
SLog.d(TAG, "isLongImage = $isLongImage")
return isLongImage
}
Expand All @@ -184,27 +183,29 @@ object ImageUtil {
val w = wh[0].toFloat()
val h = wh[1].toFloat()
val imageRatio = w / h
val isWideImage = w > h && imageRatio >= 2
val isWideImage = w > h && imageRatio >= 3
SLog.d(TAG, "isWideImage = $isWideImage")
return isWideImage
}

fun getImageMaxZoomScale(context: Context, imagePath: String): Float {
fun getStandardImageMaxZoomScale(context: Context, imagePath: String): Float {
val wh = getWidthHeight(imagePath)
val imageWid = wh[0].toFloat()
val imageHei = wh[1].toFloat()
val phoneHei = PhoneUtil.getPhoneHei(context.applicationContext).toFloat()
if (imageWid >= 2560) {
return phoneHei * 4f / imageHei
}
return phoneHei * 2f / imageHei
val phoneWid = PhoneUtil.getPhoneWid(context.applicationContext).toFloat()
return (phoneWid * 4f / imageWid).coerceAtLeast(4f)
}

fun getImageDoubleScale(context: Context, imagePath: String): Float {
val wh = getWidthHeight(imagePath)
val imageHei = wh[1].toFloat()
fun getStandardImageDoubleScale(context: Context, imagePath: String): Float {
val widthHeight = getWidthHeight(imagePath)
val imageWid = widthHeight[0].toFloat()
val imageHei = widthHeight[1].toFloat()
val phoneHei = PhoneUtil.getPhoneHei(context.applicationContext).toFloat()
return phoneHei / imageHei
if (imageWid > imageHei) {
// 宽图,双击放大到高度铺满
return phoneHei / imageHei
} else {
return getStandardImageMaxZoomScale(context, imagePath) / 2f
}
}

fun getLongImageMaxZoomScale(context: Context, imagePath: String): Float {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package cc.shinichi.library.view

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Color
import android.os.Build
import android.os.Build.VERSION_CODES.P
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.Button
import android.widget.FrameLayout
Expand Down Expand Up @@ -40,19 +39,20 @@ import cc.shinichi.library.tool.common.DeviceUtil
import cc.shinichi.library.tool.common.HandlerHolder
import cc.shinichi.library.tool.common.HttpUtil
import cc.shinichi.library.tool.common.NetworkUtil
import cc.shinichi.library.tool.common.SLog
import cc.shinichi.library.tool.image.DownloadUtil
import cc.shinichi.library.tool.common.PhoneUtil
import cc.shinichi.library.tool.common.SLog
import cc.shinichi.library.tool.common.ToastUtil
import cc.shinichi.library.tool.common.UIUtil
import cc.shinichi.library.tool.image.DownloadUtil
import cc.shinichi.library.view.listener.OnFinishListener
import com.bumptech.glide.Glide

/**
* @author 工藤
* @email [email protected]
*/
class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClickListener, OnFinishListener {
class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClickListener,
OnFinishListener {

private lateinit var context: Activity
private lateinit var handlerHolder: HandlerHolder
Expand All @@ -61,6 +61,7 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick

lateinit var parentView: View
lateinit var viewPager: HackyViewPager
lateinit var consControllerOverlay: ConstraintLayout
private lateinit var tvIndicator: TextView
private lateinit var consBottomController: ConstraintLayout

Expand All @@ -72,7 +73,7 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick
private lateinit var rootView: View
private lateinit var progressParentLayout: View

private lateinit var imagePreviewAdapter: ImagePreviewAdapter2
private lateinit var imagePreviewAdapter: ImagePreviewAdapter

private var isShowDownButton = false
private var isShowCloseButton = false
Expand All @@ -90,12 +91,18 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
overrideActivityTransition(OVERRIDE_TRANSITION_OPEN, R.anim.fade_in, R.anim.fade_out)
} else {
overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
}

parentView = View.inflate(this, ImagePreview.instance.previewLayoutResId, null)
setContentView(parentView)
ImagePreview.instance.onCustomLayoutCallback?.onLayout(this, parentView)

transparentStatusBar(this)
transparentNavBar(this)
transparentStatusBar()
transparentNavBar()

context = this
handlerHolder = HandlerHolder(this)
Expand All @@ -106,6 +113,11 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick
return
}

// 设置共享元素过渡
supportPostponeEnterTransition()
// 启动共享元素过渡
supportStartPostponedEnterTransition()

// 回调
ImagePreview.instance.setOnFinishListener(this)

Expand All @@ -122,13 +134,24 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick
// 检查缓存是否存在
checkCache(currentItemOriginPathUrl)
}

rootView = findViewById(R.id.rootView)
viewPager = findViewById(R.id.viewPager)
consControllerOverlay = findViewById(R.id.consControllerOverlay)
tvIndicator = findViewById(R.id.tv_indicator)
consBottomController = findViewById<ConstraintLayout>(R.id.consBottomController)
fmImageShowOriginContainer = findViewById(R.id.fm_image_show_origin_container)
fmCenterProgressContainer = findViewById(R.id.fm_center_progress_container)

setAlpha(0f)
// setAlpha,从0到1
handlerHolder.postDelayed({
setAlpha(1f)
}, 300)
handlerHolder.postDelayed({
consControllerOverlay.visibility = View.VISIBLE
}, 600)

// 顶部和底部margin
refreshUIMargin()

Expand Down Expand Up @@ -215,7 +238,7 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick
)
)
}
imagePreviewAdapter = ImagePreviewAdapter2(supportFragmentManager, fragmentList)
imagePreviewAdapter = ImagePreviewAdapter(supportFragmentManager, fragmentList)
viewPager.adapter = imagePreviewAdapter
viewPager.offscreenPageLimit = 1
viewPager.setCurrentItem(currentItem, false)
Expand Down Expand Up @@ -343,7 +366,6 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick
}
ImagePreview.instance.onPageFinishListener?.onFinish(this)
ImagePreview.instance.reset()
context.overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
}

override fun onDestroy() {
Expand Down Expand Up @@ -408,7 +430,10 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick
if (isUserCustomProgressView) {
fmCenterProgressContainer.visibility = View.GONE
progressParentLayout.visibility = View.GONE
ImagePreview.instance.onOriginProgressListener?.finish(this, progressParentLayout)
ImagePreview.instance.onOriginProgressListener?.finish(
this,
progressParentLayout
)
}
fragmentList[currentItem].onOriginal()
}
Expand Down Expand Up @@ -608,33 +633,23 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick
})
}

private fun transparentStatusBar(activity: Activity) {
transparentStatusBar(activity.window)
}

private fun transparentStatusBar(window: Window) {
private fun transparentStatusBar() {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
val option = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
val vis = window.decorView.systemUiVisibility
window.decorView.systemUiVisibility = option or vis
val option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
window.decorView.systemUiVisibility = option
window.statusBarColor = Color.TRANSPARENT
}

private fun transparentNavBar(activity: Activity) {
transparentNavBar(activity.window)
}

private fun transparentNavBar(window: Window) {
private fun transparentNavBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
window.isNavigationBarContrastEnforced = false
}
window.navigationBarColor = Color.TRANSPARENT
val decorView = window.decorView
val vis = decorView.systemUiVisibility
val option =
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
decorView.systemUiVisibility = vis or option
decorView.systemUiVisibility = option
}

override fun onFinish() {
Expand All @@ -646,7 +661,6 @@ class ImagePreviewActivity : AppCompatActivity(), Handler.Callback, View.OnClick
val intent = Intent()
intent.setClass(context, ImagePreviewActivity::class.java)
context.startActivity(intent)
context.overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import androidx.fragment.app.FragmentStatePagerAdapter
* 描述: ViewPager适配器
* 创建时间: 2024/11/25
*/
class ImagePreviewAdapter2(
class ImagePreviewAdapter(
fragmentManager: FragmentManager,
private val fragmentList: MutableList<ImagePreviewFragment>
) : FragmentStatePagerAdapter(fragmentManager) {
Expand Down
Loading

0 comments on commit b731a42

Please sign in to comment.