我如何使用毕加索向Marker添加图标? [英] How can I use Picasso to add icon to Marker?
问题描述
我想使用毕加索检索位图图像作为标记图标使用,但我不确定如何执行此操作。如果我使用毕加索将图像插入图像视图,我知道我可以使用:
Picasso.with(MainActivity。此).load(URL).into(photo_imageview);
当然,如果我将它传递给 .icon()
有没有一种简单的方法可以实现这一目标?
看看这个!
毕加索提供了一个通用的 Target
界面,您可以使用它来实现自己的图像目的地。具体来说,您需要覆盖 onBitmapLoaded
来填充标记。
下面给出了一个基本的实现。
公共类PicassoMarker实现了Target {
Marker mMarker;
PicassoMarker(标记标记){
mMarker = marker;
@Override
public int hashCode(){
return mMarker.hashCode();
$ b @Override
public boolean equals(Object o){
if(o instanceof PicassoMarker){
Marker marker =((PicassoMarker)o ).mMarker;
return mMarker.equals(marker);
} else {
return false;
}
}
@Override
public void onBitmapLoaded(位图位图,Picasso.LoadedFrom from){
mMarker.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap ));
$ b @Override
public void onBitmapFailed(Drawable errorDrawable){
}
$ b $ @Override
public void onPrepareLoad( Drawable placeHolderDrawable){
}
}
您可以使用它像这样 -
marker = new PicassoMarker(myMarker);
Picasso.with(MainActivity.this).load(URL).into(marker);
注意毕加索仅持有一周参考目标
传递给到
。因此,在加载图像之前,标记
引用需要存在,以避免垃圾回收器清理回调。
I would like to use Picasso to retrieve the Bitmap image to use as a marker icon but I am not sure how to do so. If I am using Picasso to insert an image into an image view, I know I can use:
Picasso.with(MainActivity.this).load(URL).into(photo_imageview);
Of course this will not work if I pass it to .icon()
Is there a simple way to achieve this?
Thanks to anyone taking a look at this!
Picasso provides a generic Target
interface you can use to implement your own image destination. Specifically, you will want to override onBitmapLoaded
to populate your marker.
A basic implementation is given below.
public class PicassoMarker implements Target {
Marker mMarker;
PicassoMarker(Marker marker) {
mMarker = marker;
}
@Override
public int hashCode() {
return mMarker.hashCode();
}
@Override
public boolean equals(Object o) {
if(o instanceof PicassoMarker) {
Marker marker = ((PicassoMarker) o).mMarker;
return mMarker.equals(marker);
} else {
return false;
}
}
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
mMarker.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
}
You would use it like this --
marker = new PicassoMarker(myMarker);
Picasso.with(MainActivity.this).load(URL).into(marker);
Note Picasso only holds a week reference to the Target
passed to into
. Therefore, the marker
reference needs to exist until the image is loaded to avoid have the callbacks being cleaned up by the garbage collector.
这篇关于我如何使用毕加索向Marker添加图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!