Android 集群渲染器不';第一次打开时不显示标记

Android 集群渲染器不';第一次打开时不显示标记,android,google-maps,kotlin,markerclusterer,Android,Google Maps,Kotlin,Markerclusterer,我有一个地图搜索屏幕,它有两个自定义背景的标记,当我第一次运行屏幕时,看起来像是通过滑动加载到标记的图像没有正确显示在标记上,当我单击“上一步”并再次打开搜索屏幕时,一些标记的图像显示正确,而另一些则没有。 我有一个名为MarkerClusterRenderSearch的类,下面是代码 类MarkerClusterRenderSearch( 私有变量上下文:上下文, 地图:谷歌地图, clusterManager:clusterManager ) : DefaultClusterRendere

我有一个地图搜索屏幕,它有两个自定义背景的标记,当我第一次运行屏幕时,看起来像是通过滑动加载到标记的图像没有正确显示在标记上,当我单击“上一步”并再次打开搜索屏幕时,一些标记的图像显示正确,而另一些则没有。 我有一个名为MarkerClusterRenderSearch的类,下面是代码

类MarkerClusterRenderSearch( 私有变量上下文:上下文, 地图:谷歌地图, clusterManager:clusterManager ) : DefaultClusterRenderer(上下文、映射、clusterManager){

val-iconGenerator:iconGenerator=iconGenerator(上下文)
private val markerImageView:ImageView=ImageView(上下文)
private var options=DisplayImageOptions.Builder()
.cacheOnDisk(真)
.showImageOnLoading(R.drawable.app_徽标)
.ResetViewBeforeLoad(真)。considerExifParams(真)
.displayer(FadeInBitmapDisplayer(300)).build()
初始化{
val clusterIconBig=context.getDrawable(R.drawable.ic\u marker\u big\u new)
ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(上下文))
iconGenerator.setContentView(markerImageView)
setupIconGen(iconGenerator,clusterIconBig!!,context)
}
重写fun onBeforeClusterItemRendered(
项:ApplicationModel.items,
标记选项:标记选项
) {
val background=ContextCompat.getDrawable(this.context,R.drawable.ic\u marker\u big\u new)
背景!!.setBounds(0,0,background.intrinsicWidth,background.intrinsicHeight)
val bitmap=bitmap.createBitmap(
background.intrinsicWidth,
background.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val画布=画布(位图)
背景。绘制(画布)
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(位图))
标记选项.标题(项.标题)
试一试{
设置配置文件ICONGEN(
i发电机,
背景!!,
item.profileimgeurl!!,
标记选项
)
}捕获(e:例外){
e、 printStackTrace()
}
}
覆盖fun shouldRenderAsCluster(cluster:cluster?):布尔值{
返回集群!!.getSize()>1;
}
在ClusterRendered之前覆盖乐趣(
集群:集群?,
标记选项:标记选项?
) {
/*val icon=iconGenerator.makeIcon(“+cluster!!.size”)
markerOptions!!.icon(BitmapDescriptorFactory.fromBitmap(icon))*/
val clusterIconBig=context.getDrawable(R.drawable.ic\u marker\u big\u new)
val icon=iconGenerator.makeIcon(“+cluster!!.size”)
markerOptions!!.icon(BitmapDescriptorFactory.fromBitmap(icon))
setupIconGen(iconGenerator,clusterIconBig!!,context)
}
私人娱乐设置IConGen(生成器:IconGenerator,可绘制:可绘制,上下文:上下文){
val textView=textView(上下文)
textView.id=com.google.maps.android.R.id.amu_text
textView.gravity=android.view.gravity.CENTER
textView.setTextColor(ContextCompat.getColor(context,R.color.color\u bluedark\u new))
textView.textSize=16F
setPadding(0,0,0,20)
textView.layoutParams=
FrameLayout.LayoutParams(drawable.intrinsicWidth,drawable.intrinsicHeight)
generator.setContentView(文本视图)
发电机.立根台(可牵引)
}
私人娱乐设施简介(
发电机:i发电机,
可提取的,可提取的,
profileUrl:字符串?,
标记选项:标记选项
) {
val relativeLayout=relativeLayout(上下文)
relativeLayout.layoutParams=
FrameLayout.LayoutParams(drawable.intrinsicWidth,drawable.intrinsicHeight)
相对长度设置填充(5,5,5,8)
相对重力=重力中心
val imageView=CircleImageView(上下文)
imageView.id=com.google.maps.android.R.id.amu_text
imageView.layoutParams=FrameLayout.layoutParams(drawable.intrinsicWidth,drawable.intrinsicHeight)
relativeLayout.addView(图像视图)
ImageLoader.getInstance().displayImage(profileUrl、imageView、选项、对象:
SimpleImageLoadingListener(){
覆盖乐趣onLoadingComplete(imageUri:String?、view:view?、LoadeImage:Bitmap?){
//super.onLoadingComplete(imageUri、视图、LoadeImage)
generator.setContentView(relativeLayout)
发电机.立根台(可牵引)
val bitmap=iconGenerator.makeIcon()
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(位图))
}
})
Glide.with(context).load(profileUrl.skipMemoryCache(false).into(imageView)
generator.setContentView(relativeLayout)
发电机.立根台(可牵引)
val bitmap=iconGenerator.makeIcon()
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(位图))
}

}

经过大量研究,我找到了解决方案。正如MauriF所说,您必须存储从url加载的图像的位图版本,然后调用mClusterManager.cluster();
val iconGenerator: IconGenerator = IconGenerator(context)
private val markerImageView: ImageView = ImageView(context)
private var options = DisplayImageOptions.Builder()
    .cacheOnDisk(true)
    .showImageOnLoading(R.drawable.app_logo)
    .resetViewBeforeLoading(true).considerExifParams(true)
    .displayer(FadeInBitmapDisplayer(300)).build()

init {
    val clusterIconBig = context.getDrawable(R.drawable.ic_marker_big_new)
    ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context))
    iconGenerator.setContentView(markerImageView)
    setupIconGen(iconGenerator, clusterIconBig!!,context)

}
override fun onBeforeClusterItemRendered(
    item: ApplicationModel.items,
    markerOptions: MarkerOptions
) {
    val background = ContextCompat.getDrawable(this.context, R.drawable.ic_marker_big_new)
    background!!.setBounds(0, 0, background.intrinsicWidth, background.intrinsicHeight)
    val bitmap = Bitmap.createBitmap(
        background.intrinsicWidth,
        background.intrinsicHeight,
        Bitmap.Config.ARGB_8888
    )
    val canvas = Canvas(bitmap)
    background.draw(canvas)
    markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
    markerOptions.title(item.title)

    try{
        setupProfileIconGen(
            iconGenerator,
            background!!,
            item.profileimgeurl!!,
            markerOptions
        )
}catch (e:Exception){
        e.printStackTrace()
    }
}

override fun shouldRenderAsCluster(cluster: Cluster<ApplicationModel.items>?): Boolean {
    return cluster!!.getSize() > 1;

}
override fun onBeforeClusterRendered(
    cluster: Cluster<ApplicationModel.items>?,
    markerOptions: MarkerOptions?
) {
   /* val icon = iconGenerator.makeIcon("" + cluster!!.size)
    markerOptions!!.icon(BitmapDescriptorFactory.fromBitmap(icon))*/
    val clusterIconBig = context.getDrawable(R.drawable.ic_marker_big_new)
    val icon = iconGenerator.makeIcon("" + cluster!!.size)
    markerOptions!!.icon(BitmapDescriptorFactory.fromBitmap(icon))
    setupIconGen(iconGenerator, clusterIconBig!!,context)

}


private fun setupIconGen(generator: IconGenerator, drawable: Drawable, context: Context) {
    val textView = TextView(context)
    textView.id = com.google.maps.android.R.id.amu_text
    textView.gravity = android.view.Gravity.CENTER
    textView.setTextColor(ContextCompat.getColor(context, R.color.color_bluedark_new))
    textView.textSize = 16F
    textView.setPadding(0, 0, 0, 20)
    textView.layoutParams =
        FrameLayout.LayoutParams(drawable.intrinsicWidth, drawable.intrinsicHeight)
    generator.setContentView(textView)
    generator.setBackground(drawable)
}

private fun setupProfileIconGen(
    generator: IconGenerator,
    drawable: Drawable,
    profileUrl: String?,
    markerOptions: MarkerOptions
) {
    val relativeLayout = RelativeLayout(context)
    relativeLayout.layoutParams =
        FrameLayout.LayoutParams(drawable.intrinsicWidth, drawable.intrinsicHeight)
    relativeLayout.setPadding(5, 5, 5, 8)
    relativeLayout.gravity = Gravity.CENTER
    val imageView = CircleImageView(context)
    imageView.id = com.google.maps.android.R.id.amu_text
    imageView.layoutParams = FrameLayout.LayoutParams(drawable.intrinsicWidth, drawable.intrinsicHeight)
    relativeLayout.addView(imageView)

    ImageLoader.getInstance().displayImage(profileUrl, imageView, options, object :
        SimpleImageLoadingListener() {
        override fun onLoadingComplete(imageUri: String?, view: View?, loadedImage: Bitmap?) {
            //super.onLoadingComplete(imageUri, view, loadedImage)
            generator.setContentView(relativeLayout)
            generator.setBackground(drawable)
            val bitmap = iconGenerator.makeIcon()
            markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
        }
    })
    Glide.with(context).load(profileUrl).skipMemoryCache(false).into(imageView)
    generator.setContentView(relativeLayout)
    generator.setBackground(drawable)
    val bitmap = iconGenerator.makeIcon()
    markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap))
}