Android : how to reload custom markers once the image is downloaded via Picasso?

前端 未结 3 1118
被撕碎了的回忆
被撕碎了的回忆 2020-12-18 08:22

I\'m trying to load dynamically users avatars as custom markers. I based my code on the google maps utils demo, but somehow it doesn\'t work, it loads only one image and all

相关标签:
3条回答
  • 2020-12-18 09:05

    Regarding about @Stas Parshin answer, there might be some problems with Glide if you put the request within onBeforeClusterItemRendered().

    I placed it on onClusterItemRendered() and it worked beautifully. It is because Glide is using asynchronous process and might not able to load it into ImageView in time.

    Hope it helps.

    0 讨论(0)
  • 2020-12-18 09:07

    We'll load raw bitmap from Picasso and then pass it to marker representing current user

    Picasso.with(getApplicationContext())
            .load(user.getAvatar_url())
            .into(new com.squareup.picasso.Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    // let's find marker for this user
                    Marker markerToChange = null;
                    for (Marker marker : mClusterManager.getMarkerCollection().getMarkers()) {
                        if (marker.getPosition().equals(user.getPosition())) {
                            markerToChange = marker;
                            break;
                        }
                    }
                    // if found - change icon
                    if (markerToChange != null) {
                        markerToChange.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
                    }
                }
                @Override
                public void onBitmapFailed(Drawable errorDrawable) {
                }
                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {
                }
            });
    

    I have some troubles with Picasso too. So, i recommend to use Glide

    compile 'com.github.bumptech.glide:glide:3.5.2'
    
    
    
    Glide.with(getApplicationContext()).
                load(user.getAvatar_url())
                .asBitmap()
                .fitCenter()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                        // let's find marker for this user
                        Marker markerToChange = null;
                        for (Marker marker : mClusterManager.getMarkerCollection().getMarkers()) {
                            if (marker.getPosition().equals(user.getPosition())) {
                                markerToChange = marker;
                                break;
                            }
                        }
                        // if found - change icon
                        if (markerToChange != null) {
                            markerToChange.setIcon(BitmapDescriptorFactory.fromBitmap(bitmap));
                        }
                    }
                });
    
    0 讨论(0)
  • 2020-12-18 09:23

    hello man the previous answer is working with with libs and here is another answer with native code

    private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
    
        ImageView image;
        LatLng latLng;
    
        public DownloadImage (double lat, double lng) {
            this.latLng = new LatLng(lat, lng);
        }
    
        protected Bitmap doInBackground(String... urls) {
    
            String url = urls[0];
            Bitmap bmpImg = null;
    
            try {
                InputStream in = new java.net.URL(url).openStream();
                bmpImg = BitmapFactory.decodeStream(in);
    
            } catch (Exception e) {
    
                Log.e("image", e.toString());
            }
    
            return bmpImg;
        }
    
        protected void onPostExecute(Bitmap bmpImg) {
    
            View viewLocationMarker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.marker_custom_view, null);
            ImageView imageView = (ImageView) viewLocationMarker.findViewById(R.id.imgPic);
    
            try {
                imageView.setImageBitmap(bmpImg);
            } catch (Exception e) {
                imageView.setImageResource(R.drawable.place_holder);
            }
    
            Marker locationMarker = mMap.addMarker(new MarkerOptions()
                    .position(latLng).title("Sattar").anchor(0.33f, 1)
                    .icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(MapsActivity.this, viewLocationMarker))));
        }
    }
    
    public static Bitmap createDrawableFromView(Context context, View view) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.buildDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
    
        return bitmap;
    }
    
    0 讨论(0)
提交回复
热议问题