我正在从布局文件创建自定义标记图像并使用该文件中的图像。我使用Picasso在图像视图上设置图像,这是第一次不起作用,但第二次工作,图像加载成功。我已经阅读了Piccaso所持有的弱引用,并尝试了在各种堆栈溢出帖子中给出的解决方案,但似乎都没有。分享我的代码,任何帮助将不胜感激。
//adding marker method
protected Marker createMarker(double latitude, double longitude,String markerText,String imageUrl) {
Log.e("Adding","Marker");
LatLng marker = new LatLng(latitude, longitude);
CameraPosition cameraPosition = new CameraPosition.Builder().target(marker).zoom(16).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
return googleMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(markerText)
.snippet("Salesman Info").icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(markerText,imageUrl))));
}
// making drawable from layout file
private Bitmap getMarkerBitmapFromView(String markerText,String imageUrl) {
View customMarkerView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker, null);
final ImageView markerImageView = (ImageView) customMarkerView.findViewById(R.id.profile_image);
TextView markerTextView = (TextView)customMarkerView.findViewById(R.id.marker_text);
markerTextView.setText(markerText);
final Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
markerImageView.setImageBitmap(bitmap);
Log.e("Loading marker","Image loaded");
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.e("Loading marker","Image failed");
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.e("Loading marker","Image prepared");
}
};
markerImageView.setTag(target);
Picasso.with(getContext())
.load(imageUrl)
.into(target);
customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
customMarkerView.buildDrawingCache();
Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
Drawable drawable = customMarkerView.getBackground();
if (drawable != null)
drawable.draw(canvas);
customMarkerView.draw(canvas);
return returnedBitmap;
}
答案 0 :(得分:1)
我通过使用带有简单目标选项的Glide库解决了这个问题。
Glide.with(getContext())
.asBitmap()
.load("imageurl")
.into(new SimpleTarget<Bitmap>() {
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
Marker m =googleMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(markerText)
.snippet("").icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(markerText,null,mCustomMarkerView))));
m.setTag(info);
super.onLoadFailed(errorDrawable);
}
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
Marker m =googleMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(markerText)
.snippet("").icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(markerText,resource,mCustomMarkerView))));
m.setTag(info);
}
}
);