我正在尝试以编程方式布局自定义信息窗口。我想使用 Picasso 加载 streetView 预览图像,但图像没有显示,知道为什么吗?
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 key 附加 url。
它确实可以在没有 key 的情况下点击几下,但从那以后,有或没有。是因为获取它太慢所以 Android 放弃并在没有它的情况下加载信息窗口吗?是否有一流的方法来做到这一点?
另一个图像加载库会更好吗?谷歌的 Volley 抽射?
还有
LinearLayout.LayoutParams
我希望图像在信息窗口的宽度上拉伸(stretch),即 match_parent,并垂直缩放以保持原始纵横比,我该怎么做?
这是我的答案
在 commonsWare 新类中,我添加了这个标志:
@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 中,我测试是否缺少标志。
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");
派对开始! :)
最佳答案
Is the because it's too slow fetching it so Android gives up and loads the info window without it?
Picasso 异步加载,除非图像被缓存。 Maps V2 的工作方式是将您返回的 View
转换为 Bitmap
,然后渲染。因此,您在 Picasso 和 Maps V2 之间存在竞争条件(图像是否在创建 Bitmap
之前加载?),因此不确定任何给定的信息窗口是否会加载工作。
在 Picasso 加载图像后,您可以在 Marker
上调用 showInfoWindow()
,这样您就可以从 Picasso 的缓存中填充 ImageView
。 showInfoWindow()
,在 Marker
上调用,触发 Maps V2 重新生成信息窗口。
例如,您可以将现有的 into()
调用更改为 into(streetViewPreviewIV, new MarkerCallback(marker))
,使用 MarkerCallback
喜欢:
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();
}
}
}
Would another image loading library work better? Google's volley?
他们都会遇到同样的问题。
关于android - picasso 图像未在自定义信息窗口中加载为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39551774/