From f15488e22cd80cbe4048d51717e752610e1e355f Mon Sep 17 00:00:00 2001 From: panpf Date: Tue, 23 Jul 2024 17:01:08 +0800 Subject: [PATCH] improve: Tiles loaded from memory now also animate when displayed --- CHANGELOG.md | 1 + CHANGELOG_zh.md | 1 + .../zoomimage/subsampling/internal/Tile.kt | 8 ++++++-- .../subsampling/internal/TileManager.kt | 17 ++++++++--------- .../test/subsampling/internal/TileTest.kt | 10 +++++----- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6e81527b..85157b183 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ subsampling: * improve: Improve BitmapRegionDecoderDecodeHelper. It no longer closes the input stream. It will now close the input stream when BitmapRegionDecoderDecodeHelper is destroyed. [#29](https://github.com/panpf/zoomimage/issues/29) +* improve: Tiles loaded from memory now also animate when displayed ## 1.1.0-alpha02 diff --git a/CHANGELOG_zh.md b/CHANGELOG_zh.md index b5f410b80..545283956 100644 --- a/CHANGELOG_zh.md +++ b/CHANGELOG_zh.md @@ -11,6 +11,7 @@ subsampling: * improve: GlideModeToImageSource 和 PicassoDataToImageSource 现在是用户注册的优先 * improve: 改进 BitmapRegionDecoderDecodeHelper,不再关闭输入流,现在会在销毁 BitmapRegionDecoderDecodeHelper 时关闭输入流。 [#29](https://github.com/panpf/zoomimage/issues/29) +* improve: 现在从内存中加载的图块在显示时也会有动画效果 ## 1.1.0-alpha02 diff --git a/zoomimage-core/src/commonMain/kotlin/com/github/panpf/zoomimage/subsampling/internal/Tile.kt b/zoomimage-core/src/commonMain/kotlin/com/github/panpf/zoomimage/subsampling/internal/Tile.kt index 3e82d1f66..83fcd04d5 100644 --- a/zoomimage-core/src/commonMain/kotlin/com/github/panpf/zoomimage/subsampling/internal/Tile.kt +++ b/zoomimage-core/src/commonMain/kotlin/com/github/panpf/zoomimage/subsampling/internal/Tile.kt @@ -61,19 +61,23 @@ class Tile constructor( val animationState = AnimationState() - fun setTileBitmap(tileBitmap: TileBitmap?, fromCache: Boolean) { + fun setTileBitmap(tileBitmap: TileBitmap?, allowAnimate: Boolean) { val oldTileBitmap = this.tileBitmap if (tileBitmap == oldTileBitmap) return oldTileBitmap?.setIsDisplayed(false) this.tileBitmap = tileBitmap tileBitmap?.setIsDisplayed(true) - if (tileBitmap != null && !fromCache) { + if (tileBitmap != null && allowAnimate) { animationState.restart() } else { animationState.stop() } } + fun cleanTileBitmap() { + setTileBitmap(null, allowAnimate = false) + } + override fun equals(other: Any?): Boolean { if (this === other) return true if (other !is Tile) return false diff --git a/zoomimage-core/src/commonMain/kotlin/com/github/panpf/zoomimage/subsampling/internal/TileManager.kt b/zoomimage-core/src/commonMain/kotlin/com/github/panpf/zoomimage/subsampling/internal/TileManager.kt index 06cc2ab8d..9193e34a3 100644 --- a/zoomimage-core/src/commonMain/kotlin/com/github/panpf/zoomimage/subsampling/internal/TileManager.kt +++ b/zoomimage-core/src/commonMain/kotlin/com/github/panpf/zoomimage/subsampling/internal/TileManager.kt @@ -168,7 +168,6 @@ class TileManager constructor( @ContinuousTransformType continuousTransformType: Int, caller: String, ): Int { - // TODO Tile changes due to scaling, always use transition animation /* * If the following detections fail, simply skip the refresh and keep the current state */ @@ -405,12 +404,12 @@ class TileManager constructor( if (cachedValue != null) { val convertedTileBitmap: TileBitmap = tileBitmapConvertor?.convert(cachedValue) ?: cachedValue - tile.setTileBitmap(convertedTileBitmap, fromCache = true) + tile.setTileBitmap(convertedTileBitmap, allowAnimate = true) tile.state = TileState.STATE_LOADED logger.d { "TileManager. loadTile. successful, fromMemory. $tile. '${imageSource.key}'" } updateTileSnapshotList("loadTile:fromMemory") } else { - tile.setTileBitmap(null, fromCache = false) + tile.cleanTileBitmap() tile.state = TileState.STATE_LOADING updateTileSnapshotList("loadTile:loading") @@ -423,21 +422,21 @@ class TileManager constructor( val tileBitmap = decodeResult.getOrNull() when { decodeResult.isFailure -> { - tile.setTileBitmap(null, fromCache = false) + tile.cleanTileBitmap() tile.state = TileState.STATE_ERROR logger.e("TileManager. loadTile. failed, ${decodeResult.exceptionOrNull()?.message}. $tile. '${imageSource.key}'") updateTileSnapshotList("loadTile:failed") } tileBitmap == null -> { - tile.setTileBitmap(null, fromCache = false) + tile.cleanTileBitmap() tile.state = TileState.STATE_ERROR logger.e("TileManager. loadTile. failed, bitmap null. $tile. '${imageSource.key}'") updateTileSnapshotList("loadTile:failed") } tile.sampleSize == 1 && (tile.srcRect.width != tileBitmap.width || tile.srcRect.height != tileBitmap.height) -> { - tile.setTileBitmap(null, fromCache = false) + tile.cleanTileBitmap() tile.state = TileState.STATE_ERROR logger.e("TileManager. loadTile. failed, size is different. $tile. $tileBitmap. '${imageSource.key}'") tileBitmap.recycle() @@ -453,7 +452,7 @@ class TileManager constructor( ) ?: tileBitmap val convertedTileBitmap: TileBitmap = tileBitmapConvertor?.convert(cacheTileBitmap) ?: cacheTileBitmap - tile.setTileBitmap(convertedTileBitmap, fromCache = false) + tile.setTileBitmap(convertedTileBitmap, allowAnimate = true) tile.state = TileState.STATE_LOADED logger.d { "TileManager. loadTile. successful. $tile. '${imageSource.key}'" } updateTileSnapshotList("loadTile:successful") @@ -463,7 +462,7 @@ class TileManager constructor( logger.d { "TileManager. loadTile. canceled. bitmap=${tileBitmap}, $tile. '${imageSource.key}'" } - tile.setTileBitmap(null, fromCache = false) + tile.cleanTileBitmap() tile.state = TileState.STATE_ERROR tileBitmap.recycle() updateTileSnapshotList("loadTile:canceled") @@ -492,7 +491,7 @@ class TileManager constructor( val tileBitmap = tile.tileBitmap if (tileBitmap != null) { logger.d { "TileManager. freeTile. $tile. '${imageSource.key}'" } - tile.setTileBitmap(null, fromCache = false) + tile.cleanTileBitmap() } if (!skipNotify) { diff --git a/zoomimage-core/src/commonTest/kotlin/com/github/panpf/zoomimage/core/common/test/subsampling/internal/TileTest.kt b/zoomimage-core/src/commonTest/kotlin/com/github/panpf/zoomimage/core/common/test/subsampling/internal/TileTest.kt index cc6105a39..4cfbee654 100644 --- a/zoomimage-core/src/commonTest/kotlin/com/github/panpf/zoomimage/core/common/test/subsampling/internal/TileTest.kt +++ b/zoomimage-core/src/commonTest/kotlin/com/github/panpf/zoomimage/core/common/test/subsampling/internal/TileTest.kt @@ -27,7 +27,7 @@ class TileTest { val tileBitmap = TestTileBitmap("key") assertFalse(tileBitmap.displayed) - tile.setTileBitmap(tileBitmap, false) + tile.setTileBitmap(tileBitmap, allowAnimate = true) assertTrue(tileBitmap.displayed) tile.apply { assertNotNull(this.tileBitmap) @@ -68,7 +68,7 @@ class TileTest { assertEquals(255, animationState.alpha) } - tile.setTileBitmap(tileBitmap, false) + tile.setTileBitmap(tileBitmap, allowAnimate = true) assertTrue(tileBitmap.displayed) tile.apply { assertNotNull(this.tileBitmap) @@ -79,7 +79,7 @@ class TileTest { val tileBitmap2 = TestTileBitmap("key") assertTrue(tileBitmap.displayed) assertFalse(tileBitmap2.displayed) - tile.setTileBitmap(tileBitmap2, true) + tile.setTileBitmap(tileBitmap2, allowAnimate = false) assertFalse(tileBitmap.displayed) assertTrue(tileBitmap2.displayed) tile.apply { @@ -89,7 +89,7 @@ class TileTest { } val tileBitmap3 = TestTileBitmap("key") - tile.setTileBitmap(tileBitmap3, false) + tile.setTileBitmap(tileBitmap3, allowAnimate = true) tile.apply { assertNotNull(this.tileBitmap) assertTrue(animationState.running) @@ -97,7 +97,7 @@ class TileTest { } val tileBitmap4 = TestTileBitmap("key") - tile.setTileBitmap(tileBitmap4, true) + tile.setTileBitmap(tileBitmap4, allowAnimate = false) tile.apply { assertNotNull(this.tileBitmap) assertFalse(animationState.running)