经过太多的研究,我找到了一种方法,但你需要滑翔才能工作。此代码在Kotlin中。
var bitmapFinal : Bitmap?
Glide.with(this)
.asBitmap()
.load(link)
.into(object : CustomTarget<Bitmap>(){
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
bitmapFinal = createUserBitmapFinal(resource) //here we will insert the bitmap we got with the link in a placehold with white border.
val mark1 = mMap.addMarker(MarkerOptions().position(latLng).title("Você está aqui").icon(BitmapDescriptorFactory.fromBitmap(bitmapFinal)))
mark1.tag=0
mMap.setOnMarkerClickListener (this@MapsActivity)
}
override fun onLoadCleared(placeholder: Drawable?) {
// this is called when imageView is cleared on lifecycle call or for
// some other reason.
// if you are referencing the bitmap somewhere else too other than this imageView
// clear it here as you can no longer have the bitmap
}
})
此时,您可以直接将位图(资源)插入到标记中,只需插入上面的代码即可。但是,如果您想使用带有白色边框的占位符,请使用以下代码,而bitmapInicial是您刚刚在上面的Glide中获得的位图:
private fun createUserBitmapFinal(bitmapInicial: Bitmap?): Bitmap? {
var result: Bitmap? = null
try {
result = Bitmap.createBitmap(dp(62f), dp(76f), Bitmap.Config.ARGB_8888) //change the size of the placeholder
result.eraseColor(Color.TRANSPARENT)
val canvas = Canvas(result)
val drawable: Drawable = resources.getDrawable(R.drawable.placeholder)
drawable.setBounds(0, 0, dp(62f), dp(76f)) //change the size of the placeholder, but you need to maintain the same proportion of the first line
drawable.draw(canvas)
val roundPaint = Paint(Paint.ANTI_ALIAS_FLAG)
val bitmapRect = RectF()
canvas.save()
if (bitmapInicial != null) {
val shader =
BitmapShader(bitmapInicial, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
val matrix = Matrix()
val scale: Float = dp(104f) / bitmapInicial.width.toFloat() //reduce or augment here change the size of the original bitmap inside the placehoder. But you need to adjust the line bitmapRect with the same proportion
matrix.postTranslate(5f, 5f)
matrix.postScale(scale, scale)
roundPaint.shader = shader
shader.setLocalMatrix(matrix)
bitmapRect[10f, 10f, 104f+10f]=104f+10f //change here too to change the size
canvas.drawRoundRect(bitmapRect, 56f, 56f, roundPaint)
}
canvas.restore()
try {
canvas.setBitmap(null)
} catch (e: java.lang.Exception) {
}
} catch (t: Throwable) {
t.printStackTrace()
}
return result
}
fun dp(value: Float): Int {
return if (value == 0f) {
0
} else Math.ceil(resources.displayMetrics.density * value.toDouble()).toInt()
}
How does one use glide to download an image into a bitmap?