为什么无法在自定义infoWindow中加载毕加索图像? [英] picasso image not loading in custom infoWindow why?
问题描述
我正在尝试以编程方式布局自定义infoWindow.我想使用Picasso加载streetView预览图像,但是该图像没有显示,知道为什么吗?
I'm trying to layout a custom infoWindow programmatically. I want to load a streetView preview image using Picasso but the image isn't showing up, any idea why?
private View prepareInfoView(Marker marker){
//prepare InfoView programmatically
LinearLayout infoView = new LinearLayout(EarthquakeActivity.this);
LinearLayout.LayoutParams infoViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
infoView.setOrientation(LinearLayout.VERTICAL);
// attach the above layout to the infoView
infoView.setLayoutParams(infoViewParams);
//create street view preview @ top
ImageView streetViewPreviewIV = new ImageView(EarthquakeActivity.this);
// this scales the image to match parents WIDTH?, but retain image's height??
LinearLayout.LayoutParams streetViewImageViewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
streetViewPreviewIV.setLayoutParams(streetViewImageViewParams);
String imageURL = "https://maps.googleapis.com/maps/api/streetview?size=200x200&location=";
String markerLongitude = Double.toString(marker.getPosition().longitude);
String markerLatitude = Double.toString(marker.getPosition().latitude);
imageURL += markerLatitude + "," + markerLongitude + "&fov=120&heading=0&pitch=0";
Log.wtf("prepareInfoView", imageURL);
Picasso.with(this).load(imageURL).into(streetViewPreviewIV);
infoView.addView(streetViewPreviewIV);
我尝试过在是否将api键附加到网址的情况下
I've tried with and without the api key appending the url.
在没有按键的情况下,单击几下确实可以正常工作,但此后不管是否有按键都没有.是因为获取它的速度太慢,所以Android放弃了该窗口并加载了没有它的信息窗口?有没有最好的方法可以做到这一点?
It did work for a few clicks without the key, but hasn't since, with or without. Is the because it's too slow fetching it so Android gives up and loads the info window without it? Is there a best in class way to do this?
另一个图像加载库是否可以更好地工作? Google的凌空抽射?
Would another image loading library work better? Google's volley?
也
LinearLayout.LayoutParams
LinearLayout.LayoutParams
我希望图像在信息窗口(即match_parent)的宽度上伸展,并垂直缩放以保持原始的宽高比,该怎么做?
I'd like the image to stretch across the width of the info windows, i.e. match_parent, and to scale vertically to maintain original aspect ratio, how do I do this?
这是我的答案
在commonsWare新类中,我添加以下标志:
In commonsWare new class I add this flag:
@Override
public void onSuccess() {
Log.i(TAG, "image got, should rebuild window");
if (marker != null && marker.isInfoWindowShown()) {
Log.i(TAG, "conditions met, redrawing window");
marker.setTag(new Boolean("True"));
marker.showInfoWindow();
}
}
然后在prepareInfoView中,测试是否缺少标志.
And in prepareInfoView, I test for the flags absence.
if (marker.getTag() == null ) {
Log.i("prepareInfoView", "fetching image");
Picasso.with(this).load(imageURL).fetch(new MarkerCallback(marker));
}
else {
Log.wtf("prepareInfoView", "building info window");
参加! :)
推荐答案
是因为获取它太慢了,所以Android放弃并加载了没有它的信息窗口吗?
Is the because it's too slow fetching it so Android gives up and loads the info window without it?
除非对图像进行缓存,否则毕加索将异步加载. Maps V2的工作方式是将您返回的View
转换为Bitmap
,这就是要渲染的内容.结果,您在Picasso和Maps V2之间处于竞争状态(是否在创建Bitmap
之前加载了图像?),因此不确定任何给定的信息窗口是否可以工作.
Picasso loads asynchronously unless the image is cached. And the way Maps V2 works is that the View
you return is converted into a Bitmap
, and that is what gets rendered. As a result, you have a race condition between Picasso and Maps V2 (does the image get loaded before the Bitmap
gets created?), and so it is indeterminate as to whether or not any given info window will work.
您可以在毕加索加载图像后在Marker
上调用showInfoWindow()
,以便可以从毕加索的缓存中填充ImageView
. showInfoWindow()
(在Marker
上调用)触发Maps V2重新生成信息窗口.
You can call showInfoWindow()
on the Marker
after Picasso has loaded the image, so you can populate the ImageView
from Picasso's cache. showInfoWindow()
, called on a Marker
, triggers Maps V2 to regenerate the info window.
例如,您可以使用MarkerCallback
将现有的into()
调用更改为into(streetViewPreviewIV, new MarkerCallback(marker))
,
For example, you could change your existing into()
call into into(streetViewPreviewIV, new MarkerCallback(marker))
, with a MarkerCallback
like:
static class MarkerCallback implements Callback {
Marker marker=null;
MarkerCallback(Marker marker) {
this.marker=marker;
}
@Override
public void onError() {
Log.e(getClass().getSimpleName(), "Error loading thumbnail!");
}
@Override
public void onSuccess() {
if (marker != null && marker.isInfoWindowShown()) {
marker.showInfoWindow();
}
}
}
另一个图像加载库是否可以更好地工作? Google的凌空抽射?
Would another image loading library work better? Google's volley?
他们都会遭受相同的问题.
They will all suffer from the same issue.
这篇关于为什么无法在自定义infoWindow中加载毕加索图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!