gpt4 book ai didi

Android map 实用程序 : images from URL

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:30:28 27 4
gpt4 key购买 nike

我正在使用优秀的 Android Maps Utility library在 map 上显示带有集群的自定义标记。它工作得很好,而且很容易定制。

我的问题是如何将本地资源从演示 (R.drawable.image_demo) 更改为来自 URL 的图像。我正在使用 Universal Image Loader 将此图像加载到 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());
}

最佳答案

我在使用 UIL 时遇到了类似的问题,并非所有图像都已加载。每次我运行 map Activity 并放大集群时,都会有不同的标记/集群没有图像。仅显示标准标记/群集图标。

经过两天的尝试,终于可以正常使用了。暂时我已经切换到 Picasso 库,看看它是否可以工作,但我在那里遇到了类似的问题并且无法完成。除了毕加索,所有从网络 (URL) 加载的图像都旋转了 90 度。我不知道为什么。根据我在网上找到的一些帖子,它可能需要对设备进行一些操作,我正在使用 Samsung Galaxy S5 设备进行测试。但不确定....

这里记录了我是如何做到的:我创建了一个带有 ImageView 的 HardRefSimpleImageLoadingListener,它将存在直到 UIL 加载过程完成。如果没有它的硬引用 ImageView,onLoadingCancelled 将被调用并且加载将无法完成。我还创建了对象 ClusterMarkerTargetClusterItemMarkerTarget,其中包含图标生成器的标记、ImageView 和位图。我还将它们放入 HashSets myClusterItemMarkerTargetsmyClusterMarkerTargets 以防止它们被垃圾回收。

也许它对某些人有用。

这里是一些代码:

        @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;
}
}

关于Android map 实用程序 : images from URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23013608/

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