Skip to content

Commit

Permalink
improve: Tiles loaded from memory now also animate when displayed
Browse files Browse the repository at this point in the history
  • Loading branch information
panpf committed Jul 23, 2024
1 parent 1bd98f2 commit f15488e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ subsampling:
* improve: GlideModeToImageSource 和 PicassoDataToImageSource 现在是用户注册的优先
* improve: 改进 BitmapRegionDecoderDecodeHelper,不再关闭输入流,现在会在销毁
BitmapRegionDecoderDecodeHelper 时关闭输入流。 [#29](https://github.com/panpf/zoomimage/issues/29)
* improve: 现在从内存中加载的图块在显示时也会有动画效果

## 1.1.0-alpha02

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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")

Expand All @@ -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()
Expand All @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -89,15 +89,15 @@ class TileTest {
}

val tileBitmap3 = TestTileBitmap("key")
tile.setTileBitmap(tileBitmap3, false)
tile.setTileBitmap(tileBitmap3, allowAnimate = true)
tile.apply {
assertNotNull(this.tileBitmap)
assertTrue(animationState.running)
assertEquals(0, animationState.alpha)
}

val tileBitmap4 = TestTileBitmap("key")
tile.setTileBitmap(tileBitmap4, true)
tile.setTileBitmap(tileBitmap4, allowAnimate = false)
tile.apply {
assertNotNull(this.tileBitmap)
assertFalse(animationState.running)
Expand Down

0 comments on commit f15488e

Please sign in to comment.