delay loading the zoomable image at fullscreen mode to avoid laggy swipes

This commit is contained in:
tibbi 2018-07-18 17:26:37 +02:00
parent 2091c8bf4b
commit addae2045e

View file

@ -47,7 +47,9 @@ import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
class PhotoFragment : ViewPagerFragment() { class PhotoFragment : ViewPagerFragment() {
private var DEFAULT_DOUBLE_TAP_ZOOM = 2f private val DEFAULT_DOUBLE_TAP_ZOOM = 2f
private val ZOOMABLE_VIEW_LOAD_DELAY = 1500L
private var isFragmentVisible = false private var isFragmentVisible = false
private var isFullscreen = false private var isFullscreen = false
private var wasInit = false private var wasInit = false
@ -55,6 +57,7 @@ class PhotoFragment : ViewPagerFragment() {
private var isPanorama = false private var isPanorama = false
private var imageOrientation = -1 private var imageOrientation = -1
private var gifDrawable: GifDrawable? = null private var gifDrawable: GifDrawable? = null
private var loadZoomableViewHandler = Handler()
private var storedShowExtendedDetails = false private var storedShowExtendedDetails = false
private var storedHideExtendedDetails = false private var storedHideExtendedDetails = false
@ -184,7 +187,9 @@ class PhotoFragment : ViewPagerFragment() {
private fun photoFragmentVisibilityChanged(isVisible: Boolean) { private fun photoFragmentVisibilityChanged(isVisible: Boolean) {
if (isVisible) { if (isVisible) {
addZoomableView() scheduleZoomableView()
} else {
loadZoomableViewHandler.removeCallbacksAndMessages(null)
} }
} }
@ -276,8 +281,9 @@ class PhotoFragment : ViewPagerFragment() {
} }
override fun onResourceReady(resource: Bitmap?, model: Any?, target: Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean { override fun onResourceReady(resource: Bitmap?, model: Any?, target: Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
if (isFragmentVisible) if (isFragmentVisible) {
addZoomableView() scheduleZoomableView()
}
return false return false
} }
}).into(view.photo_view) }).into(view.photo_view)
@ -302,46 +308,53 @@ class PhotoFragment : ViewPagerFragment() {
} }
} }
private fun addZoomableView() { private fun scheduleZoomableView() {
if (!context!!.config.replaceZoomableImages && medium.isImage() && isFragmentVisible && view.subsampling_view.isGone()) { loadZoomableViewHandler.removeCallbacksAndMessages(null)
ViewPagerActivity.wasDecodedByGlide = false loadZoomableViewHandler.postDelayed({
view.subsampling_view.apply { if (isFragmentVisible && !context!!.config.replaceZoomableImages && medium.isImage() && view.subsampling_view.isGone()) {
maxScale = 10f addZoomableView()
beVisible()
isQuickScaleEnabled = context.config.oneFingerZoom
setResetScaleOnSizeChange(context.config.screenRotation != ROTATE_BY_ASPECT_RATIO)
setImage(ImageSource.uri(getPathToLoad(medium)))
orientation = if (imageOrientation == -1) SubsamplingScaleImageView.ORIENTATION_USE_EXIF else degreesForRotation(imageOrientation)
setEagerLoadingEnabled(false)
setExecutor(AsyncTask.SERIAL_EXECUTOR)
setOnImageEventListener(object : SubsamplingScaleImageView.OnImageEventListener {
override fun onImageLoaded() {
}
override fun onReady() {
background = ColorDrawable(if (context.config.blackBackground) Color.BLACK else context.config.backgroundColor)
val useWidth = if (imageOrientation == ORIENTATION_ROTATE_90 || imageOrientation == ORIENTATION_ROTATE_270) sHeight else sWidth
val useHeight = if (imageOrientation == ORIENTATION_ROTATE_90 || imageOrientation == ORIENTATION_ROTATE_270) sWidth else sHeight
setDoubleTapZoomScale(getDoubleTapZoomScale(useWidth, useHeight))
}
override fun onTileLoadError(e: Exception?) {
}
override fun onPreviewReleased() {
}
override fun onImageLoadError(e: Exception) {
background = ColorDrawable(Color.TRANSPARENT)
beGone()
}
override fun onPreviewLoadError(e: Exception?) {
background = ColorDrawable(Color.TRANSPARENT)
beGone()
}
})
} }
}, ZOOMABLE_VIEW_LOAD_DELAY)
}
private fun addZoomableView() {
ViewPagerActivity.wasDecodedByGlide = false
view.subsampling_view.apply {
maxScale = 10f
beVisible()
isQuickScaleEnabled = context.config.oneFingerZoom
setResetScaleOnSizeChange(context.config.screenRotation != ROTATE_BY_ASPECT_RATIO)
setImage(ImageSource.uri(getPathToLoad(medium)))
orientation = if (imageOrientation == -1) SubsamplingScaleImageView.ORIENTATION_USE_EXIF else degreesForRotation(imageOrientation)
setEagerLoadingEnabled(false)
setExecutor(AsyncTask.SERIAL_EXECUTOR)
setOnImageEventListener(object : SubsamplingScaleImageView.OnImageEventListener {
override fun onImageLoaded() {
}
override fun onReady() {
background = ColorDrawable(if (context.config.blackBackground) Color.BLACK else context.config.backgroundColor)
val useWidth = if (imageOrientation == ORIENTATION_ROTATE_90 || imageOrientation == ORIENTATION_ROTATE_270) sHeight else sWidth
val useHeight = if (imageOrientation == ORIENTATION_ROTATE_90 || imageOrientation == ORIENTATION_ROTATE_270) sWidth else sHeight
setDoubleTapZoomScale(getDoubleTapZoomScale(useWidth, useHeight))
}
override fun onTileLoadError(e: Exception?) {
}
override fun onPreviewReleased() {
}
override fun onImageLoadError(e: Exception) {
background = ColorDrawable(Color.TRANSPARENT)
beGone()
}
override fun onPreviewLoadError(e: Exception?) {
background = ColorDrawable(Color.TRANSPARENT)
beGone()
}
})
} }
} }
@ -431,6 +444,7 @@ class PhotoFragment : ViewPagerFragment() {
Glide.with(context!!).clear(view.photo_view) Glide.with(context!!).clear(view.photo_view)
view.subsampling_view.recycle() view.subsampling_view.recycle()
} }
loadZoomableViewHandler.removeCallbacksAndMessages(null)
} }
override fun onConfigurationChanged(newConfig: Configuration) { override fun onConfigurationChanged(newConfig: Configuration) {