Google 默认图标在标记上显示一毫秒

问题描述 投票:0回答:1

我在具有自定义视图的地图上有很多标记,当在地图上添加标记时,大约一毫秒,标记的默认图标在我的自定义标记上可见,然后我的自定义标记变得可见。我也有一个 alpha 动画,附上下面的代码:

    private fun setIconToMarkerWithAnimation(m: Marker?, bitmap: Bitmap? = null) {
        val mapIcon = bitmap ?: getBitmapFromMarker(m)
        m?.setIcon(mapIcon?.let { BitmapDescriptorFactory.fromBitmap(it) })
        val animation = ValueAnimator.ofFloat(0.01F, 1.0F)
        animation.duration = MapConstants.DEFAULT_ANIMATION_DURATION
        animation.interpolator = AnticipateOvershootInterpolator()
        animation.addUpdateListener { animationState ->
            val alpha = animationState.animatedValue as Float
            m?.alpha = alpha
        }
        animation.start()
    }

此函数在此代码之后调用:

`val m = mMap?.addMarker(markerOptions)`

该问题并非在所有手机上都可见,但在某些手机上每次都会出现。另外,在添加新标记之前,我将删除所有以前的标记。

我尝试过使用

        m?.isVisible = false m?.setIcon(null)

但这也没有帮助

android kotlin google-maps
1个回答
0
投票

您遇到的问题是,默认标记图标在自定义标记出现之前闪烁了一会儿,这可能是由于计时问题造成的。您可以尝试以下一些方法来修复它:

预加载您的自定义图标:

不要在 setIconToMarkerWithAnimation 中生成位图,而是考虑预先加载它并将其存储在变量中。这确保了设置图标时位图随时可用。

动画开始后设置图标:

修改代码以在动画开始后在 addUpdateListener 内设置图标。这样,图标变化就会在动画期间发生,从而可能掩盖闪烁。

private fun setIconToMarkerWithAnimation(m: Marker?, bitmap: Bitmap? = null) {
    val mapIcon = bitmap ?: getBitmapFromMarker(m)
    val animation = ValueAnimator.ofFloat(0.01F, 1.0F)
    animation.duration = MapConstants.DEFAULT_animation_duration
    animation.interpolator = AnticipateOvershootInterpolator()
    animation.addUpdateListener { animationState ->
        val alpha = animationState.animatedValue as Float
        m?.alpha = alpha
        
        // Set icon after animation starts
        if (animationState.animatedFraction > 0f) {
            m?.setIcon(mapIcon?.let { BitmapDescriptorFactory.fromBitmap(it) })
        }
    }
    animation.start()
}
© www.soinside.com 2019 - 2024. All rights reserved.