如何在标记上设置图像而图像存储在 Firebase 存储中
How to set image on marker Whereas image is stored in Firebase storage
我想在标记上设置图像。图片存储在 firerebase 存储中,我下载了 link 图片。现在如何在标记
上设置该图像
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
reference = reference.child("Profile");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds:dataSnapshot.getChildren()){
latitude = (Double) ds.child("Latitude").getValue();
longitude = (Double) ds.child("Longitude").getValue();
String name=(String) ds.child("Name").getValue();
String imgUrl = (String) ds.child("imageURL").getValue();//Here is image Url of marker
LatLng latLng = new LatLng(latitude,longitude);
mMap.addMarker(new MarkerOptions().position(latLng).title(name));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
经过大量研究,我找到了一种方法,但您需要 Glide 才能工作。此代码在 Kotlin 中。
一旦你在存储 firebase 中获得了带有图像的 url。这样调用 Glide 可以将位图中的 url link 进行变换。感谢 glide 这很容易:
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?
我想在标记上设置图像。图片存储在 firerebase 存储中,我下载了 link 图片。现在如何在标记
上设置该图像public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
reference = reference.child("Profile");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds:dataSnapshot.getChildren()){
latitude = (Double) ds.child("Latitude").getValue();
longitude = (Double) ds.child("Longitude").getValue();
String name=(String) ds.child("Name").getValue();
String imgUrl = (String) ds.child("imageURL").getValue();//Here is image Url of marker
LatLng latLng = new LatLng(latitude,longitude);
mMap.addMarker(new MarkerOptions().position(latLng).title(name));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
经过大量研究,我找到了一种方法,但您需要 Glide 才能工作。此代码在 Kotlin 中。 一旦你在存储 firebase 中获得了带有图像的 url。这样调用 Glide 可以将位图中的 url link 进行变换。感谢 glide 这很容易:
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?