代码之家  ›  专栏  ›  技术社区  ›  pligosv

使用Glide-to-Google Maps标记加载图像

  •  9
  • pligosv  · 技术社区  · 6 年前

    我有个问题。我使用Glide 3.8.0。

    我需要从服务器下载图像,并将其放在谷歌地图上的标记上。

        Glide.with(getBaseActivity())
                    .load(place.getIconUrl())
                    .asBitmap()
                    .fitCenter()
                    .into(new SimpleTarget<Bitmap>(50,50) {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                            map.addMarker(new MarkerOptions()
                                    .icon(BitmapDescriptorFactory.fromBitmap(resource))
                                    .position(place.getLatLng()));
                        }
    
                        @Override
                        public void onLoadFailed(Exception e, Drawable errorDrawable) {
                            map.addMarker(new MarkerOptions()
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_marker_default_logo))
                            .position(place.getLatLng()));
                        }
                    });
    

    此外,我有默认的标志,如果一些加载错误,其大小50x50。

    我在两台设备上测试加载-nexus 5和无名设备(我不知道屏幕分辨率和屏幕大小,但大小几乎与nexus 5相同)

    在不同的设备上,我有不同大小的marker徽标,我用

    .into(new SimpleTarget<Bitmap>(50,50) 尺寸

    Nexus 5,50x50与默认徽标相比非常小,75x75比默认徽标小,150x150与默认徽标相同

    无名称设备:75x75与默认徽标相同,比默认徽标小50x50

    那么,如何使用Glide使其在不同的设备上与默认徽标50x50相同(默认徽标在不同的设备上看起来相同)

    Nexus 5 50x50

    Nexus 5  75x75

    Nexus 5 150x150

    3 回复  |  直到 5 年前
        1
  •  3
  •   nickfrancis.me    6 年前
     Glide.with(this).load("url").listener(object : RequestListener<Drawable> {
            override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
    
    
              return true
            }
    
            override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                callmethod(resource)     //pass drawable to your method and set the drawable for marker there
                imageSource=resource
                return true
            }
    
        })
    

    使用

      BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(convertToBitmap(d,100,100));
      MarkerOptions markerOptions = new MarkerOptions()
                .position(latLng).icon(icon)
                .title(getString(titleResId))
                .draggable(true);
    

    也用于从drawable获取位图

     public Bitmap convertToBitmap(Drawable drawable, int widthPixels, int heightPixels) {
        Bitmap mutableBitmap = Bitmap.createBitmap(widthPixels, heightPixels, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mutableBitmap);
        drawable.setBounds(0, 0, widthPixels, heightPixels);
        drawable.draw(canvas);
    
        return mutableBitmap;
    }
    

    或者你可以使用

    public Bitmap getBitmapFromURL(String strURL) {
        try {
            URL url = new URL(strURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
        2
  •  2
  •   pligosv    6 年前

    决定重新创建位图

    Glide.with(getBaseActivity())
                    .load(place.getIconUrl())
                    .asBitmap()
                    .dontTransform()
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                            final float scale = getContext().getResources().getDisplayMetrics().density;
                            int pixels = (int) (50 * scale + 0.5f);
                            Bitmap bitmap = Bitmap.createScaledBitmap(resource, pixels, pixels, true);
                            markerImageView.setImageBitmap(bitmap);
                            addMarker(place.getLatLng());
                        }
    
                        @Override
                        public void onLoadFailed(Exception e, Drawable errorDrawable) {
                            markerImageView.setImageResource(R.drawable.ic_marker_default_logo);
                            addMarker(place.getLatLng());
                        }
                    });
    
        3
  •  0
  •   Lakpriya Senevirathna    5 年前

    您可以使用此方法!

    Glide.with(getApplicationContext()).asBitmap()
                        .load(dataSnapshot.child("dpURL").getValue(String.class))
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
    
                                mMap.addMarker(new MarkerOptions().position(origin).title(pno).icon(BitmapDescriptorFactory.fromBitmap(bitmappros(resource))));
    
                                if(firstload) {
                                    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(origin,16));
                                    firstload = false;
                                }else{
                                    mMap.animateCamera(CameraUpdateFactory.newLatLng(origin));
                                }
                            }
                        });
    

    用于bitmappros功能!

    private Bitmap bitmappros(Bitmap res){
        View marker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout, null);
        markerImage  = marker.findViewById(R.id.user_dp);
        markerImage.setImageBitmap(res);
    
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        marker.setLayoutParams(new ViewGroup.LayoutParams(52, ViewGroup.LayoutParams.WRAP_CONTENT));
        marker.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
        marker.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
        marker.buildDrawingCache();
        Bitmap bitmap2 = Bitmap.createBitmap(marker.getMeasuredWidth(), marker.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap2);
        marker.draw(canvas);
    
        return bitmap2;
    }