从互联网下载的图片作为注释显示 - 使用Picasso

10

我无法将从互联网下载的图像作为注释显示。 我正在使用以下代码和Picasso库进行实现。 但是,如果我使用本地图像,则可以正常工作。 感谢任何帮助。

private void createAnnotation(int id, double lat, double lon, String caption, String photoUrl) {

    SKAnnotation annotation = new SKAnnotation(id);

    SKCoordinate coordinate = new SKCoordinate(lat, lon);
    annotation.setLocation(coordinate);
    annotation.setMininumZoomLevel(5);

    SKAnnotationView annotationView = new SKAnnotationView();
    View customView =
            (LinearLayout) ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.annotation_photo_and_text, null, false);
    //  If width and height of the view  are not power of 2 the actual size of the image will be the next power of 2 of max(width,height).
    //annotationView.setView(findViewById(R.id.customView));

    TextView tvCaption = (TextView) customView.findViewById(R.id.annotation_photo_caption);
    tvCaption.setText(caption);

    ImageView ivPhoto = (ImageView) customView.findViewById(R.id.annotation_photo);
    Picasso.with(getApplicationContext())
            .load(photoUrl)
            .resize(96, 96)
            //.centerCrop()
            .into(ivPhoto);
    //ivPhoto.setImageResource(R.drawable.hurricanerain);

    annotationView.setView(customView);
    annotation.setAnnotationView(annotationView);

    mapView.addAnnotation(annotation, SKAnimationSettings.ANIMATION_NONE);
}

有没有人使用从URL下载的图像进行注释的经验? - burakk
3个回答

1

尝试在活动上下文中使用而不是应用程序上下文。这可能适合您。


1
Picasso可以异步从互联网加载图像。尝试在下载完成后将图像添加到注释中。您可以使用Target来监听图像下载完成事件:
ImageView ivPhoto = (ImageView) customView.findViewById(R.id.annotation_photo);  
Target target = new Target() {  
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        ivPhoto.setImageBitmap(bitmap);
        annotationView.setView(customView);
        annotation.setAnnotationView(annotationView);
        mapView.addAnnotation(annotation, SKAnimationSettings.ANIMATION_NONE);
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {}

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
ivPhoto.setTag(target);
Picasso.with(getApplicationContext())
    .load(photoUrl)
    .resize(96, 96)
    .into(target);

我已经通过使用ImageRequest(在Volley中)解决了这个问题。我没有测试过你的代码,但它看起来应该可以工作。谢谢。 - burakk

0

如果您尝试使用Target对象加载图像,然后将下载的位图设置到您的ImageView中,会发生什么?

Target target = new Target() {  
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        // loading of the bitmap was a success
        // TODO do some action with the bitmap
        ivPhoto.setImageBitmap(bitmap);
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        // loading of the bitmap failed
        // TODO do some action/warning/error message
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {

    }
};
ivPhoto.setTag(target);
Picasso.with(getApplicationContext())
    .load(photoUrl)
    .resize(96, 96)
    .into(target);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接