我使用优秀的Android Maps Utility library在地图上显示带有群集的自定义标记。它工作得很好,很容易定制。
我的问题是如何将本地资源从演示(R.drawable.image_demo)更改为URL中的图像。我正在使用通用图像加载器在imageView异步上加载这些图像,但问题是图像没有加载到对应的标记上。
任何人都知道任何一个例子吗?
这是在DefaultClusterRender类中下载和加载图像的代码。提前谢谢。
@Override
protected void onBeforeClusterItemRendered(MapFoto mapFoto, MarkerOptions markerOptions) {
// Draw a single person.
// Set the info window to show their name.
// mImageView.setImageResource(R.drawable.barcelona);
// mImageView.setScaleType(ScaleType.CENTER_CROP);
Log.d("", "--- url: " + mapFoto.getPictureUrl());
ImageLoader.getInstance().displayImage(mapFoto.getPictureUrl(), mImageView, BlipointApp.optionsAvatarImage, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
mImageView.setImageBitmap(loadedImage);
mImageView.setScaleType(ScaleType.CENTER_CROP);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
Bitmap icon = mIconGenerator.makeIcon();
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon)).title(mapFoto.getName());
}
答案 0 :(得分:11)
我在使用UIL时遇到了类似的问题,并非所有图像都已加载。每次我运行地图活动并放大到群集时,都会有不同的标记/群集没有图像。仅显示标准标记/群集图标。
经过两天的尝试和尝试后,它工作正常。暂时我已经切换到Picasso库,看看它是否可行,但我遇到了类似的问题并且无法完成。除了毕加索之外,从网络(URL)加载的所有图像都旋转了90度。我无法弄清楚原因。根据我在网络上发现的一些帖子,可能需要对设备做些什么,我使用三星Galaxy S5设备进行测试。但不确定......
这里有一些注释我是如何做到的:我创建了一个带有ImageView的HardRefSimpleImageLoadingListener
,它将一直存在,直到UIL加载过程完成。如果没有硬引用ImageView
,onLoadingCancelled将被调用并且加载将无法完成。我还创建了对象ClusterMarkerTarget
和ClusterItemMarkerTarget
,其中包含Icon生成器的Marker,ImageView和位图。我还将它们放入HashSets myClusterItemMarkerTargets
和myClusterMarkerTargets
中,以防止它们被垃圾收集。
也许它对某些人有用。
这里有一些代码:
@Override
protected void onClusterItemRendered(ReportItem clusterItem, Marker marker) {
final ClusterItemMarkerTarget pm_ClusterItem = new ClusterItemMarkerTarget(marker);
myClusterItemMarkerTargets.add(pm_ClusterItem);
HardRefSimpleImageLoadingListener loadingListener = new HardRefSimpleImageLoadingListener() {
@Override
public void onLoadingFailed(String s, View view, FailReason failReason) {
myClusterItemMarkerTargets.remove(pm_ClusterItem);
}
@Override
public void onLoadingComplete(String s, View view, Bitmap bitmap) {
mClusterItemImageView.setImageBitmap(bitmap);
pm_ClusterItem.myIcon_cluster = mClusterItemIconGenerator.makeIcon();
pm_ClusterItem.mMarker.setIcon(BitmapDescriptorFactory.fromBitmap(pm_ClusterItem.myIcon_cluster));
myClusterItemMarkerTargets.remove(pm_ClusterItem);
}
@Override
public void onLoadingCancelled(String s, View view) {
myClusterItemMarkerTargets.remove(pm_ClusterItem);
}
};
ImageLoader.getInstance().displayImage(clusterItem.picPath, pm_ClusterItem.myClusterItemImageView , loadingListener );
}
@Override
protected void onClusterRendered(Cluster<ReportItem> cluster, Marker marker) {
int i=0;
clustersize = cluster.getSize();
final ClusterMarkerTarget pm_Cluster = new ClusterMarkerTarget(marker, cluster);
myClusterMarkerTargets.add(pm_Cluster);
for (ReportItem r : cluster.getItems()) {
// Draw 1 at most.
if (i == 1 ) {
break;
}
HardRefSimpleImageLoadingListener loadingListener = new HardRefSimpleImageLoadingListener() {
@Override
public void onLoadingFailed(String s, View view, FailReason failReason) {
myClusterMarkerTargets.remove(pm_Cluster);
}
@Override
public void onLoadingComplete(String s, View view, Bitmap bitmap) {
mClusterImageView.setImageBitmap(bitmap); // = (ImageView) view; //pm.myClusterImageView;
pm_Cluster.myIcon_clusterItem = mClusterIconGenerator.makeIcon(String.valueOf(clustersize));
pm_Cluster.myMarker.setIcon(BitmapDescriptorFactory.fromBitmap(pm_Cluster.myIcon_clusterItem));
myClusterMarkerTargets.remove(pm_Cluster);
}
@Override
public void onLoadingCancelled(String s, View view) {
myClusterMarkerTargets.remove(pm_Cluster);
}
};
ImageLoader.getInstance().displayImage(r.picPath, pm_Cluster.myClusterImageView, loadingListener);
i++;
}
}
更多:
//Set for holding a reference to marker targets --> targets won't get carbage collected during looping and loading images
Set<ReportRenderer.ClusterItemMarkerTarget> myClusterItemMarkerTargets = new HashSet<>();
Set<ReportRenderer.ClusterMarkerTarget> myClusterMarkerTargets = new HashSet<>();
更多:
public ReportRenderer(ClusterManager<ReportItem> mClusterManager, GoogleMap map) {
super(MyApplication.getContext(), map, mClusterManager);
mDimension = (int) MyApplication.getContext().getResources().getDimension(R.dimen.custom_report_image);
mPadding = (int) MyApplication.getContext().getResources().getDimension(R.dimen.custom_report_padding);
// initialize cluster icon generator
View multiReport = inflater.inflate(R.layout.multi_report, null);
mClusterImageView = (ImageView) multiReport.findViewById(R.id.image_report);
mClusterIconGenerator = new IconGenerator(MyApplication.getContext());
mClusterIconGenerator.setContentView(multiReport);
// initialize cluster item icon generator
mClusterItemImageView = new ImageView(MyApplication.getContext());
mClusterItemImageView.setLayoutParams(new ViewGroup.LayoutParams(mDimension, mDimension));
mClusterItemImageView.setPadding(mPadding, mPadding, mPadding, mPadding);
mClusterItemIconGenerator = new IconGenerator(MyApplication.getContext());
mClusterItemIconGenerator.setContentView(mClusterItemImageView);
// initialize image loader
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.loading)
.showImageForEmptyUri(R.drawable.warning)
.showImageOnFail(R.drawable.fail)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
//.bitmapConfig(Bitmap.Config.RGB_565)
.build();
config = new ImageLoaderConfiguration.Builder(MyApplication.getContext())
.defaultDisplayImageOptions(options)
.build();
ImageLoader.getInstance().init(config);
}
更多:
//cluster marker with image loaded by Ultimate Image Loader
class ClusterMarkerTarget {
Marker myMarker;
ImageView myClusterImageView;
public ClusterMarkerTarget(Marker marker, Cluster<ReportItem> cluster) {
myMarker = marker;
View multiReport = inflater.inflate(R.layout.multi_report, null);
myClusterImageView = (ImageView) multiReport.findViewById(R.id.image_report);
}
}
//cluster item marker with image loaded by Ultimate Image Loader
class ClusterItemMarkerTarget {
Marker mMarker;
ImageView myClusterItemImageView;
public ClusterItemMarkerTarget(Marker marker) {
mMarker = marker;
myClusterItemImageView = new ImageView(MyApplication.getContext());//mClusterItemImageView;
myClusterItemImageView.setLayoutParams(new ViewGroup.LayoutParams(mDimension, mDimension));
int padding = (int) MyApplication.getContext().getResources().getDimension(R.dimen.custom_report_padding);
myClusterItemImageView.setPadding(padding, padding, padding, padding);
}
}
最后:
class HardRefSimpleImageLoadingListener extends SimpleImageLoadingListener {
public ImageView mView;
@Override
public void onLoadingStarted(String imageUri, View view) {
mView = (ImageView) view;
}
}