我想在 Google 地图上缩放标记,我的代码可以工作,但在某些设备中标记会消失几次,有人知道如何平滑缩放吗?
我尝试使用 ValueAnimator 来设置标记的图标:
val choreographer = Choreographer.getInstance()
val desiredFrameRate = 20
val frameInterval = 1000L / desiredFrameRate
var lastFrameTime = 0L
val widthAnimator = ValueAnimator.ofFloat(anim.fromX, anim.toX)
widthAnimator.duration = anim.duration
widthAnimator.repeatMode = anim.repeatMode
widthAnimator.repeatCount = anim.repeatCount
val heightAnimator = ValueAnimator.ofFloat(anim.fromY, anim.toY)
heightAnimator.duration = anim.duration
heightAnimator.repeatMode = anim.repeatMode
heightAnimator.repeatCount = anim.repeatCount
val animatorUpdateListener = ValueAnimator.AnimatorUpdateListener {
val currentTime = System.currentTimeMillis()
if (currentTime - lastFrameTime < frameInterval) {
return@AnimatorUpdateListener
}
lastFrameTime = currentTime
choreographer.postFrameCallback { choreographerTime ->
val scaleX =
BigDecimal((widthAnimator.animatedValue as Float).toDouble()).setScale(
3,
RoundingMode.HALF_EVEN
).toFloat()
val scaleY =
BigDecimal((heightAnimator.animatedValue as Float).toDouble()).setScale(
3,
RoundingMode.HALF_EVEN
).toFloat()
marker.setIcon(
createScaledBitmapDescriptor(
viewToDrawable(target), scaleX, scaleY
)
)
}
}
heightAnimator.addUpdateListener(animatorUpdateListener)
animations.apply {
add(widthAnimator)
add(heightAnimator)
}
使用 ObjectAnimator 而不添加额外的时间检查 使用 AnimatorSet 获得更好的帧渲染 避免频繁更改图标 位图缓存