2017-01-22 56 views
10

我無法將從互聯網下載的圖像顯示爲註釋。我正在實施下面的代碼和畢加索圖書館。但是,如果我使用本地圖像,它將起作用。預先感謝您的幫助。顯示作爲註釋從互聯網下載的圖像 - 使用畢加索

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); 
} 
+0

沒有人使用從URL下載的圖像進行註釋嗎? – 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); 
1

畢加索異步加載來自互聯網的圖像。嘗試在下載後將圖像添加到註釋中。您可以使用目標來偵聽圖像下載完成:

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); 
+0

我已經通過使用ImageRequest(在Volley中)解決了這個問題。我沒有測試你的代碼,但它看起來可以工作。謝謝。 – burakk

+0

複製粘貼另一個答案不是一個好方法,男人。 – rom4ek

1

嘗試使用活動上下文來代替applicationcontext。它可能適合你。