gpt4 book ai didi

android - Android Kotlin-使用带有Glide的Bitmap实现自定义 map 标记

转载 作者:行者123 更新时间:2023-12-02 13:36:33 25 4
gpt4 key购买 nike

我正在尝试使用XML布局实现Google map 中的自定义 map 标记。

然而;当我使用createCustomMarker方法将自定义标记打印到Google Maps中时,它不会加载/显示来自云/服务器的图像。

我认为这是因为Glide(或此事中的应用)在布局转换成Bitmap之前没有完成更改以完成下载和渲染图像,或者我的实现是错误的。谁能帮我这个?

@SuppressLint("InflateParams")
private fun createCustomMarker(urlImageFromCloud: String): Bitmap{

val marker: LayoutInflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view: View = marker.inflate(R.layout.map_marker, null)

val messageIcon: Int = resources.getIdentifier("local_image_from_drawable", "drawable", this.packageName)
val markerPhoto: CircleImageView = view.findViewById(R.id.map_photo)
val markerContainer: CircleImageView = view.findViewById(R.id.map_photo)
val options: RequestOptions = RequestOptions()
.centerCrop()
.placeholder(R.color.colorGrayWhite)
.error(R.color.colorGrayWhite)
.diskCacheStrategy(DiskCacheStrategy.ALL)
Glide.with(this)
.load(urlImageFromCloud) // String image URL from my server/cloud - this is the part that does not show up.
.apply(options).into(markerPhoto)
Glide.with(this)
.load(messageIcon)
.apply(options).into(markerContainer)


val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)

view.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels)
view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels)
view.buildDrawingCache()
val bitmap = Bitmap.createBitmap(view.measuredWidth, view.measuredHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
view.draw(canvas)

return bitmap
}

我的map_marker XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_marker_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/map_container"
android:layout_width="75dp"
android:layout_height="75dp"/>

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/map_photo"
android:src="@color/colorGray"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
style="@style/MapMarkerCircleImage"/>


</RelativeLayout>

这就是我所谓的 createCustomMarker方法的方式。
val defaultLocation = LatLng(locationLatitude, locationLongitude)
googleMap.addMarker(MarkerOptions().position(defaultLocation).icon(BitmapDescriptorFactory.fromBitmap(createCustomMarker("my_image_url_from_cloud"))))

最佳答案

我遇到过同样的问题。我的解决方案是使用Glide下载位图,并在完成下载后使用侦听器创建标记。

private fun getMarkerIcon(context: Context, url: String, listener: (BitmapDescriptor) -> Unit) {
val markerView = View.inflate(context, R.layout.view_marker, null)
Glide.with(context)
.asBitmap()
.load(url)
.into(object : SimpleTarget<Bitmap>() {

override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
markerView.avatarImageView.setImageBitmap(resource)
listener.invoke(BitmapDescriptorFactory.fromBitmap(getBitmapFromView(markerView)))
}
})
}

private fun getBitmapFromView(view: View): Bitmap {
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
val bitmap = Bitmap.createBitmap(view.measuredWidth, view.measuredHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
view.layout(0, 0, view.measuredWidth, view.measuredHeight)
view.draw(canvas)
return bitmap
}

和电话
getMarkerIcon(context, "http://....") {
val options = MarkerOptions()
.position(LatLng(latitude, longitude))
.icon(it)
googleMap.addMarker(options)
}

关于android - Android Kotlin-使用带有Glide的Bitmap实现自定义 map 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55359248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com